|
| 1 | +/* |
| 2 | + * Copyright 2020-2022 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.authentication; |
| 17 | + |
| 18 | +import java.security.Principal; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.springframework.security.core.Authentication; |
| 25 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 26 | +import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations; |
| 27 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 28 | +import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link OAuth2ClientCredentialsAuthenticationContext}. |
| 35 | + * |
| 36 | + * @author Steve Riesenberg |
| 37 | + * @author Joe Grandja |
| 38 | + */ |
| 39 | +public class OAuth2ClientCredentialsAuthenticationContextTests { |
| 40 | + private final RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); |
| 41 | + private final OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(this.registeredClient).build(); |
| 42 | + private final Authentication principal = this.authorization.getAttribute(Principal.class.getName()); |
| 43 | + private final OAuth2ClientCredentialsAuthenticationToken authorizationConsentAuthentication = |
| 44 | + new OAuth2ClientCredentialsAuthenticationToken(this.principal, Set.of("a_scope"), Map.of("a_key", "a_value")); |
| 45 | + |
| 46 | + @Test |
| 47 | + public void withWhenAuthenticationNullThenThrowIllegalArgumentException() { |
| 48 | + assertThatThrownBy(() -> OAuth2ClientCredentialsAuthenticationContext.with(null)) |
| 49 | + .isInstanceOf(IllegalArgumentException.class) |
| 50 | + .hasMessage("authentication cannot be null"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void setWhenValueNullThenThrowIllegalArgumentException() { |
| 55 | + OAuth2ClientCredentialsAuthenticationContext.Builder builder = |
| 56 | + OAuth2ClientCredentialsAuthenticationContext.with(this.authorizationConsentAuthentication); |
| 57 | + |
| 58 | + assertThatThrownBy(() -> builder.registeredClient(null)) |
| 59 | + .isInstanceOf(IllegalArgumentException.class); |
| 60 | + assertThatThrownBy(() -> builder.put(null, "")) |
| 61 | + .isInstanceOf(IllegalArgumentException.class); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void buildWhenRequiredValueNullThenThrowIllegalArgumentException() { |
| 66 | + OAuth2ClientCredentialsAuthenticationContext.Builder builder = |
| 67 | + OAuth2ClientCredentialsAuthenticationContext.with(this.authorizationConsentAuthentication); |
| 68 | + assertThatThrownBy(builder::build) |
| 69 | + .isInstanceOf(IllegalArgumentException.class) |
| 70 | + .hasMessage("registeredClient cannot be null"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void buildWhenAllValuesProvidedThenAllValuesAreSet() { |
| 75 | + OAuth2ClientCredentialsAuthenticationContext context = |
| 76 | + OAuth2ClientCredentialsAuthenticationContext.with(this.authorizationConsentAuthentication) |
| 77 | + .registeredClient(this.registeredClient) |
| 78 | + .put("custom-key-1", "custom-value-1") |
| 79 | + .context(ctx -> ctx.put("custom-key-2", "custom-value-2")) |
| 80 | + .build(); |
| 81 | + |
| 82 | + assertThat(context.<Authentication>getAuthentication()).isEqualTo(this.authorizationConsentAuthentication); |
| 83 | + assertThat(context.getRegisteredClient()).isEqualTo(this.registeredClient); |
| 84 | + assertThat(context.<String>get("custom-key-1")).isEqualTo("custom-value-1"); |
| 85 | + assertThat(context.<String>get("custom-key-2")).isEqualTo("custom-value-2"); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments