-
Notifications
You must be signed in to change notification settings - Fork 146
Add configurable role case sensitivity #2192
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
Open
sdelamo
wants to merge
1
commit into
5.1.x
Choose a base branch
from
paperclip/mng-226-roles-case-sensitive
base: 5.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...y/io/micronaut/security/authorization/SecuredRolesCaseInsensitiveConfigurationSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package io.micronaut.security.authorization | ||
|
|
||
| import io.micronaut.context.annotation.Requires | ||
| import io.micronaut.http.HttpRequest | ||
| import io.micronaut.http.MediaType | ||
| import io.micronaut.http.annotation.Controller | ||
| import io.micronaut.http.annotation.Get | ||
| import io.micronaut.http.annotation.Produces | ||
| import io.micronaut.security.MockAuthenticationProvider | ||
| import io.micronaut.security.SuccessAuthenticationScenario | ||
| import io.micronaut.security.annotation.Secured | ||
| import io.micronaut.security.authentication.Authentication | ||
| import io.micronaut.security.testutils.EmbeddedServerSpecification | ||
| import io.micronaut.security.token.RolesFinder | ||
| import io.micronaut.security.utils.DefaultSecurityService | ||
| import io.micronaut.security.utils.SecurityService | ||
| import jakarta.inject.Singleton | ||
|
|
||
| import java.security.Principal | ||
|
|
||
| class SecuredRolesCaseInsensitiveConfigurationSpec extends EmbeddedServerSpecification { | ||
|
|
||
| @Override | ||
| String getSpecName() { | ||
| 'SecuredRolesCaseInsensitiveConfigurationSpec' | ||
| } | ||
|
|
||
| @Override | ||
| Map<String, Object> getConfiguration() { | ||
| super.configuration + ['micronaut.security.roles-case-sensitive': false] | ||
| } | ||
|
|
||
| void "@Secured annotation value can be case insensitive"() { | ||
| when: | ||
| client.exchange(HttpRequest.GET("/uppercase").basicAuth('user', 'password'), String) | ||
|
|
||
| then: | ||
| noExceptionThrown() | ||
|
|
||
| when: | ||
| client.exchange(HttpRequest.GET("/lowercase").basicAuth('user', 'password'), String) | ||
|
|
||
| then: | ||
| noExceptionThrown() | ||
| } | ||
|
|
||
| void "SecurityService::hasRole can be case insensitive"() { | ||
| when: | ||
| Authentication authentication = Authentication.build("sherlock", ["ROLE_DETECTIVE"]) | ||
| SecurityService securityService = new CustomSecurityService(applicationContext.getBean(RolesFinder), authentication) | ||
|
|
||
| then: | ||
| securityService.hasRole('ROLE_DETECTIVE') | ||
|
|
||
| and: | ||
| securityService.hasRole('role_detective') | ||
| } | ||
|
|
||
| @Requires(property = 'spec.name', value = 'SecuredRolesCaseInsensitiveConfigurationSpec') | ||
| @Controller | ||
| static class RolesCaseInsensitiveController { | ||
|
|
||
| @Produces(MediaType.TEXT_PLAIN) | ||
| @Secured(["role_user"]) | ||
| @Get("/lowercase") | ||
| String lowercase(Principal principal) { | ||
| principal.name | ||
| } | ||
|
|
||
| @Produces(MediaType.TEXT_PLAIN) | ||
| @Secured(["ROLE_USER"]) | ||
| @Get("/uppercase") | ||
| String uppercase(Principal principal) { | ||
| principal.name | ||
| } | ||
| } | ||
|
|
||
| @Singleton | ||
| @Requires(property = 'spec.name', value = 'SecuredRolesCaseInsensitiveConfigurationSpec') | ||
| static class AuthenticationProviderUserPassword extends MockAuthenticationProvider { | ||
| AuthenticationProviderUserPassword() { | ||
| super([new SuccessAuthenticationScenario('user', ['ROLE_USER'])]) | ||
| } | ||
| } | ||
|
|
||
| @Requires(property = 'spec.name', value = 'SecuredRolesCaseInsensitiveConfigurationSpec') | ||
| static class CustomSecurityService extends DefaultSecurityService { | ||
|
|
||
| Authentication authentication | ||
|
|
||
| CustomSecurityService(RolesFinder rolesFinder, Authentication authentication) { | ||
| super(rolesFinder) | ||
| this.authentication = authentication | ||
| } | ||
|
|
||
| @Override | ||
| Optional<Authentication> getAuthentication() { | ||
| Optional.of(authentication) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
security/src/test/groovy/io/micronaut/security/token/DefaultRolesFinderSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package io.micronaut.security.token | ||
|
|
||
| import io.micronaut.security.config.SecurityConfiguration | ||
| import io.micronaut.security.authentication.AuthenticationMode | ||
| import io.micronaut.security.token.config.TokenConfiguration | ||
| import spock.lang.Specification | ||
| import spock.lang.Unroll | ||
|
|
||
| class DefaultRolesFinderSpec extends Specification { | ||
|
|
||
| TokenConfiguration tokenConfiguration = new TokenConfiguration() {} | ||
|
|
||
| void "default constructor keeps role comparison case sensitive"() { | ||
| given: | ||
| DefaultRolesFinder rolesFinder = new DefaultRolesFinder(tokenConfiguration) | ||
|
|
||
| expect: | ||
| rolesFinder.hasAnyRequiredRoles(['ROLE_ADMIN'], ['ROLE_ADMIN']) | ||
| !rolesFinder.hasAnyRequiredRoles(['role_admin'], ['ROLE_ADMIN']) | ||
| } | ||
|
|
||
| @Unroll | ||
| void "configured case insensitive role comparison for #requiredRoles and #grantedRoles is #expected"() { | ||
| given: | ||
| SecurityConfiguration securityConfiguration = Stub(SecurityConfiguration) { | ||
| isRolesCaseSensitive() >> false | ||
| } | ||
| DefaultRolesFinder rolesFinder = new DefaultRolesFinder(tokenConfiguration, securityConfiguration) | ||
|
|
||
| expect: | ||
| rolesFinder.hasAnyRequiredRoles(requiredRoles, grantedRoles) == expected | ||
|
|
||
| where: | ||
| requiredRoles | grantedRoles || expected | ||
| ['role_admin'] | ['ROLE_ADMIN'] || true | ||
| ['role_admin'] | ['ROLE_USER'] || false | ||
| [] | ['ROLE_ADMIN'] || false | ||
| ['role_admin'] | [] || false | ||
| [null] | [null] || true | ||
| [null, 'role_admin'] | ['ROLE_ADMIN'] || true | ||
| } | ||
|
|
||
| void "security configuration roles case-sensitive default is true"() { | ||
| given: | ||
| SecurityConfiguration securityConfiguration = new SecurityConfiguration() { | ||
| @Override | ||
| List<String> getIpPatterns() { | ||
| [] | ||
| } | ||
|
|
||
| @Override | ||
| List getInterceptUrlMap() { | ||
| [] | ||
| } | ||
|
|
||
| @Override | ||
| boolean isInterceptUrlMapPrependPatternWithContextPath() { | ||
| true | ||
| } | ||
|
|
||
| @Override | ||
| AuthenticationMode getAuthentication() { | ||
| null | ||
| } | ||
| } | ||
|
|
||
| expect: | ||
| securityConfiguration.rolesCaseSensitive | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.