|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.security.oauth2.resource; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.UUID; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Named; |
| 24 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 25 | +import org.junit.jupiter.params.provider.Arguments; |
| 26 | +import org.junit.jupiter.params.provider.ArgumentsProvider; |
| 27 | + |
| 28 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 29 | + |
| 30 | +/** |
| 31 | + * {@link ArgumentsProvider Arguments provider} supplying different Spring Boot properties |
| 32 | + * to customize JWT converter behavior, JWT token for conversion, expected principal name |
| 33 | + * and expected authorities. |
| 34 | + * |
| 35 | + * @author Yan Kardziyaka |
| 36 | + */ |
| 37 | +public final class JwtConverterCustomizationsArgumentsProvider implements ArgumentsProvider { |
| 38 | + |
| 39 | + @Override |
| 40 | + public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) { |
| 41 | + String customPrefix = "CUSTOM_AUTHORITY_PREFIX_"; |
| 42 | + String customDelimiter = "[~,#:]"; |
| 43 | + String customAuthoritiesClaim = "custom_authorities"; |
| 44 | + String customPrincipalClaim = "custom_principal"; |
| 45 | + |
| 46 | + String jwkSetUriProperty = "spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://jwk-set-uri.com"; |
| 47 | + String authorityPrefixProperty = "spring.security.oauth2.resourceserver.jwt.authority-prefix=" + customPrefix; |
| 48 | + String authoritiesDelimiterProperty = "spring.security.oauth2.resourceserver.jwt.authorities-claim-delimiter=" |
| 49 | + + customDelimiter; |
| 50 | + String authoritiesClaimProperty = "spring.security.oauth2.resourceserver.jwt.authorities-claim-name=" |
| 51 | + + customAuthoritiesClaim; |
| 52 | + String principalClaimProperty = "spring.security.oauth2.resourceserver.jwt.principal-claim-name=" |
| 53 | + + customPrincipalClaim; |
| 54 | + |
| 55 | + String[] noJwtConverterProps = { jwkSetUriProperty }; |
| 56 | + String[] customPrefixProps = { jwkSetUriProperty, authorityPrefixProperty }; |
| 57 | + String[] customDelimiterProps = { jwkSetUriProperty, authoritiesDelimiterProperty }; |
| 58 | + String[] customAuthoritiesClaimProps = { jwkSetUriProperty, authoritiesClaimProperty }; |
| 59 | + String[] customPrincipalClaimProps = { jwkSetUriProperty, principalClaimProperty }; |
| 60 | + String[] allJwtConverterProps = { jwkSetUriProperty, authorityPrefixProperty, authoritiesDelimiterProperty, |
| 61 | + authoritiesClaimProperty, principalClaimProperty }; |
| 62 | + |
| 63 | + String[] jwtScopes = { "custom_scope0", "custom_scope1" }; |
| 64 | + String subjectValue = UUID.randomUUID().toString(); |
| 65 | + String customPrincipalValue = UUID.randomUUID().toString(); |
| 66 | + |
| 67 | + Jwt.Builder jwtBuilder = Jwt.withTokenValue("token") |
| 68 | + .header("alg", "none") |
| 69 | + .expiresAt(Instant.MAX) |
| 70 | + .issuedAt(Instant.MIN) |
| 71 | + .issuer("https://issuer.example.org") |
| 72 | + .jti("jti") |
| 73 | + .notBefore(Instant.MIN) |
| 74 | + .subject(subjectValue) |
| 75 | + .claim(customPrincipalClaim, customPrincipalValue); |
| 76 | + |
| 77 | + Jwt noAuthoritiesCustomizationsJwt = jwtBuilder.claim("scp", jwtScopes[0] + " " + jwtScopes[1]).build(); |
| 78 | + Jwt customAuthoritiesDelimiterJwt = jwtBuilder.claim("scp", jwtScopes[0] + "~" + jwtScopes[1]).build(); |
| 79 | + Jwt customAuthoritiesClaimJwt = jwtBuilder.claim("scp", null) |
| 80 | + .claim(customAuthoritiesClaim, jwtScopes[0] + " " + jwtScopes[1]) |
| 81 | + .build(); |
| 82 | + Jwt customAuthoritiesClaimAndDelimiterJwt = jwtBuilder.claim("scp", null) |
| 83 | + .claim(customAuthoritiesClaim, jwtScopes[0] + "~" + jwtScopes[1]) |
| 84 | + .build(); |
| 85 | + |
| 86 | + String[] customPrefixAuthorities = { customPrefix + jwtScopes[0], customPrefix + jwtScopes[1] }; |
| 87 | + String[] defaultPrefixAuthorities = { "SCOPE_" + jwtScopes[0], "SCOPE_" + jwtScopes[1] }; |
| 88 | + |
| 89 | + return Stream.of( |
| 90 | + Arguments.of(Named.named("No JWT converter customizations", noJwtConverterProps), |
| 91 | + noAuthoritiesCustomizationsJwt, subjectValue, defaultPrefixAuthorities), |
| 92 | + Arguments.of(Named.named("Custom prefix for GrantedAuthority", customPrefixProps), |
| 93 | + noAuthoritiesCustomizationsJwt, subjectValue, customPrefixAuthorities), |
| 94 | + Arguments.of(Named.named("Custom delimiter for JWT scopes", customDelimiterProps), |
| 95 | + customAuthoritiesDelimiterJwt, subjectValue, defaultPrefixAuthorities), |
| 96 | + Arguments.of(Named.named("Custom JWT authority claim name", customAuthoritiesClaimProps), |
| 97 | + customAuthoritiesClaimJwt, subjectValue, defaultPrefixAuthorities), |
| 98 | + Arguments.of(Named.named("Custom JWT principal claim name", customPrincipalClaimProps), |
| 99 | + noAuthoritiesCustomizationsJwt, customPrincipalValue, defaultPrefixAuthorities), |
| 100 | + Arguments.of(Named.named("All JWT converter customizations", allJwtConverterProps), |
| 101 | + customAuthoritiesClaimAndDelimiterJwt, customPrincipalValue, customPrefixAuthorities)); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments