|
| 1 | +/* |
| 2 | + * Copyright 2020-2024 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 | +package org.springframework.security.oauth2.server.authorization.token; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 29 | +import org.springframework.security.core.Authentication; |
| 30 | +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; |
| 31 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeActor; |
| 32 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeAuthenticationToken; |
| 33 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenExchangeCompositeAuthenticationToken; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.when; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests for {@link DefaultOAuth2TokenClaimsConsumer}. |
| 41 | + * |
| 42 | + * @author Steve Riesenberg |
| 43 | + */ |
| 44 | +public class DefaultOAuth2TokenClaimsConsumerTests { |
| 45 | + |
| 46 | + private OAuth2TokenContext tokenContext; |
| 47 | + |
| 48 | + private Consumer<Map<String, Object>> consumer; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + public void setUp() { |
| 52 | + this.tokenContext = mock(OAuth2TokenContext.class); |
| 53 | + this.consumer = new DefaultOAuth2TokenClaimsConsumer(this.tokenContext); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void acceptWhenTokenTypeIsRefreshTokenThenNoClaimsAdded() { |
| 58 | + when(this.tokenContext.getTokenType()).thenReturn(OAuth2TokenType.REFRESH_TOKEN); |
| 59 | + Map<String, Object> claims = new LinkedHashMap<>(); |
| 60 | + this.consumer.accept(claims); |
| 61 | + assertThat(claims).isEmpty(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void acceptWhenAuthorizationGrantIsNullThenNoClaimsAdded() { |
| 66 | + when(this.tokenContext.getTokenType()).thenReturn(OAuth2TokenType.ACCESS_TOKEN); |
| 67 | + when(this.tokenContext.getAuthorizationGrant()).thenReturn(null); |
| 68 | + Map<String, Object> claims = new LinkedHashMap<>(); |
| 69 | + this.consumer.accept(claims); |
| 70 | + assertThat(claims).isEmpty(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void acceptWhenTokenExchangeAndAudiencesEmptyThenNoClaimsAdded() { |
| 75 | + OAuth2TokenExchangeAuthenticationToken tokenExchangeAuthentication = mock( |
| 76 | + OAuth2TokenExchangeAuthenticationToken.class); |
| 77 | + when(tokenExchangeAuthentication.getAudiences()).thenReturn(Collections.emptySet()); |
| 78 | + when(this.tokenContext.getTokenType()).thenReturn(OAuth2TokenType.ACCESS_TOKEN); |
| 79 | + when(this.tokenContext.getAuthorizationGrant()).thenReturn(tokenExchangeAuthentication); |
| 80 | + Map<String, Object> claims = new LinkedHashMap<>(); |
| 81 | + this.consumer.accept(claims); |
| 82 | + assertThat(claims).isEmpty(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void acceptWhenTokenExchangeGrantAndAudiencesThenAudClaimAppended() { |
| 87 | + OAuth2TokenExchangeAuthenticationToken tokenExchangeAuthentication = mock( |
| 88 | + OAuth2TokenExchangeAuthenticationToken.class); |
| 89 | + when(tokenExchangeAuthentication.getAudiences()).thenReturn(Set.of("audience1", "audience2")); |
| 90 | + when(this.tokenContext.getTokenType()).thenReturn(OAuth2TokenType.ACCESS_TOKEN); |
| 91 | + when(this.tokenContext.getAuthorizationGrant()).thenReturn(tokenExchangeAuthentication); |
| 92 | + Map<String, Object> claims = new LinkedHashMap<>(); |
| 93 | + claims.put(OAuth2TokenClaimNames.AUD, List.of("client1")); |
| 94 | + this.consumer.accept(claims); |
| 95 | + assertThat(claims).hasSize(1); |
| 96 | + assertThat(claims.get(OAuth2TokenClaimNames.AUD)).isNotNull(); |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + List<String> audiences = (List<String>) claims.get(OAuth2TokenClaimNames.AUD); |
| 99 | + assertThat(audiences).containsExactly("client1", "audience1", "audience2"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void acceptWhenTokenExchangeGrantAndDelegationThenActClaimAdded() { |
| 104 | + OAuth2TokenExchangeAuthenticationToken tokenExchangeAuthentication = mock( |
| 105 | + OAuth2TokenExchangeAuthenticationToken.class); |
| 106 | + when(tokenExchangeAuthentication.getAudiences()).thenReturn(Collections.emptySet()); |
| 107 | + when(this.tokenContext.getTokenType()).thenReturn(OAuth2TokenType.ACCESS_TOKEN); |
| 108 | + when(this.tokenContext.getAuthorizationGrant()).thenReturn(tokenExchangeAuthentication); |
| 109 | + Authentication subject = new TestingAuthenticationToken("subject", null); |
| 110 | + OAuth2TokenExchangeActor actor1 = new OAuth2TokenExchangeActor(Map.of(OAuth2TokenClaimNames.ISS, "issuer1", |
| 111 | + OAuth2TokenClaimNames.SUB, "actor1")); |
| 112 | + OAuth2TokenExchangeActor actor2 = new OAuth2TokenExchangeActor(Map.of(OAuth2TokenClaimNames.ISS, "issuer2", |
| 113 | + OAuth2TokenClaimNames.SUB, "actor2")); |
| 114 | + OAuth2TokenExchangeCompositeAuthenticationToken principal = new OAuth2TokenExchangeCompositeAuthenticationToken( |
| 115 | + subject, List.of(actor1, actor2)); |
| 116 | + when(this.tokenContext.getPrincipal()).thenReturn(principal); |
| 117 | + Map<String, Object> claims = new LinkedHashMap<>(); |
| 118 | + this.consumer.accept(claims); |
| 119 | + assertThat(claims).hasSize(1); |
| 120 | + assertThat(claims.get("act")).isNotNull(); |
| 121 | + @SuppressWarnings("unchecked") |
| 122 | + Map<String, Object> actClaim1 = (Map<String, Object>) claims.get("act"); |
| 123 | + assertThat(actClaim1.get(OAuth2TokenClaimNames.ISS)).isEqualTo("issuer1"); |
| 124 | + assertThat(actClaim1.get(OAuth2TokenClaimNames.SUB)).isEqualTo("actor1"); |
| 125 | + @SuppressWarnings("unchecked") |
| 126 | + Map<String, Object> actClaim2 = (Map<String, Object>) actClaim1.get("act"); |
| 127 | + assertThat(actClaim2.get(OAuth2TokenClaimNames.ISS)).isEqualTo("issuer2"); |
| 128 | + assertThat(actClaim2.get(OAuth2TokenClaimNames.SUB)).isEqualTo("actor2"); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments