-
Notifications
You must be signed in to change notification settings - Fork 146
Validate OAuth and JWKS endpoint URLs #2252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 5.3.x
Are you sure you want to change the base?
Changes from all commits
0747f5a
bc4ce1b
6695e1f
7cd9237
fdacdec
cbe7b85
f85982f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package io.micronaut.security.token.jwt.signature.jwks; | ||
|
|
||
| import io.micronaut.context.ApplicationContext; | ||
| import io.micronaut.context.exceptions.BeanInstantiationException; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class JwksSignatureConfigurationPropertiesTest { | ||
|
|
||
| @Test | ||
| void validJwksUrlIsAccepted() { | ||
| try (ApplicationContext context = ApplicationContext.run(Map.of( | ||
| "micronaut.security.token.jwt.signatures.jwks.test.url", "https://example.com/.well-known/jwks.json"))) { | ||
| JwksSignatureConfiguration configuration = context.getBean(JwksSignatureConfiguration.class); | ||
|
|
||
| assertEquals("https://example.com/.well-known/jwks.json", configuration.getUrl()); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "example.com/.well-known/jwks.json", | ||
| "/.well-known/jwks.json" | ||
| }) | ||
| void jwksUrlMustBeValid(String url) { | ||
| BeanInstantiationException exception = assertThrows(BeanInstantiationException.class, () -> { | ||
|
Check warning on line 33 in security-jwt/src/test/java/io/micronaut/security/token/jwt/signature/jwks/JwksSignatureConfigurationPropertiesTest.java
|
||
| try (ApplicationContext context = ApplicationContext.run(Map.of( | ||
| "micronaut.security.token.jwt.signatures.jwks.test.url", url))) { | ||
| context.getBean(JwksSignatureConfiguration.class); | ||
| } | ||
| }); | ||
|
|
||
| assertTrue(exception.getMessage().contains("url - must be a valid URL")); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| import io.micronaut.security.oauth2.configuration.endpoints.RevocationEndpointConfiguration; | ||
| import io.micronaut.security.oauth2.configuration.endpoints.SecureEndpointConfiguration; | ||
| import io.micronaut.security.oauth2.configuration.endpoints.TokenEndpointConfiguration; | ||
| import jakarta.validation.Valid; | ||
| import org.jspecify.annotations.NonNull; | ||
| import org.jspecify.annotations.Nullable; | ||
| import io.micronaut.core.convert.format.MapFormat; | ||
|
|
@@ -54,6 +55,8 @@ | |
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import static io.micronaut.security.oauth2.configuration.endpoints.EndpointConfiguration.HTTP_OR_HTTPS_URL_REGEX; | ||
|
|
||
| /** | ||
| * Stores configuration of each configured OAuth 2.0 client. | ||
| * | ||
|
|
@@ -83,11 +86,11 @@ public class OauthClientConfigurationProperties implements OauthClientConfigurat | |
| private List<String> scopes; | ||
| private boolean enabled = DEFAULT_ENABLED; | ||
| private GrantType grantType = GrantType.AUTHORIZATION_CODE; | ||
| private AuthorizationEndpointConfigurationProperties authorization; | ||
| private TokenEndpointConfigurationProperties token; | ||
| private IntrospectionEndpointConfigurationProperties introspection; | ||
| private RevocationEndpointConfigurationProperties revocation; | ||
| private OpenIdClientConfigurationProperties openid; | ||
| private @Valid AuthorizationEndpointConfigurationProperties authorization; | ||
| private @Valid TokenEndpointConfigurationProperties token; | ||
| private @Valid IntrospectionEndpointConfigurationProperties introspection; | ||
| private @Valid RevocationEndpointConfigurationProperties revocation; | ||
| private @Valid OpenIdClientConfigurationProperties openid; | ||
| private ClientCredentialsConfigurationProperties clientCredentials; | ||
| private AuthorizationServer authorizationServer; | ||
| private boolean proxyWellKnownOauthAuthorizationServer; | ||
|
|
@@ -524,12 +527,14 @@ public static class OpenIdClientConfigurationProperties implements OpenIdClientC | |
| private boolean fetchConfiguration = DEFAULT_FETCH_CONFIGURATION; | ||
| private URL issuer; | ||
| private String configurationPath = DEFAULT_CONFIG_PATH; | ||
|
|
||
| @io.micronaut.validation.annotation.URL(regexp = HTTP_OR_HTTPS_URL_REGEX) | ||
| private String jwksUri; | ||
| private RegistrationEndpointConfigurationProperties registration; | ||
| private UserInfoEndpointConfigurationProperties userInfo; | ||
| private AuthorizationEndpointConfigurationProperties authorization; | ||
| private TokenEndpointConfigurationProperties token; | ||
| private EndSessionConfigurationProperties endSession = new EndSessionConfigurationProperties(); | ||
| private @Valid RegistrationEndpointConfigurationProperties registration; | ||
| private @Valid UserInfoEndpointConfigurationProperties userInfo; | ||
| private @Valid AuthorizationEndpointConfigurationProperties authorization; | ||
| private @Valid TokenEndpointConfigurationProperties token; | ||
| private @Valid EndSessionConfigurationProperties endSession = new EndSessionConfigurationProperties(); | ||
|
Comment on lines
+533
to
+537
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this cascades validation down, maybe you want to add more tests for different validation use cases |
||
| private boolean protectedResourceMetadata = DEFAULT_PROTECTED_RESOURCE_METADATA; | ||
|
|
||
| /** | ||
|
|
@@ -603,7 +608,7 @@ public void setConfigurationPath(@NonNull String configurationPath) { | |
| } | ||
|
|
||
| @Override | ||
| public Optional<String> getJwksUri() { | ||
| public Optional<@io.micronaut.validation.annotation.URL(regexp = HTTP_OR_HTTPS_URL_REGEX) String> getJwksUri() { | ||
| return Optional.ofNullable(jwksUri); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| */ | ||
| package io.micronaut.security.oauth2.configuration.endpoints; | ||
|
|
||
| import io.micronaut.validation.annotation.URL; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
|
|
@@ -24,9 +25,10 @@ | |
| * @since 1.2.0 | ||
| */ | ||
| public interface EndpointConfiguration { | ||
| String HTTP_OR_HTTPS_URL_REGEX = "(?i)https?://.*"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably worth adding javadoc to this public constant |
||
|
|
||
| /** | ||
| * @return The optional endpoint url | ||
| */ | ||
| Optional<String> getUrl(); | ||
| Optional<@URL(regexp = HTTP_OR_HTTPS_URL_REGEX) String> getUrl(); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.