|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
- * Copyright 2002-2021 the original author or authors.
|
|
|
+ * Copyright 2002-2022 the original author or authors.
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -18,6 +18,7 @@ package org.springframework.security.oauth2.client;
|
|
|
|
|
|
import java.time.Duration;
|
|
|
import java.time.Instant;
|
|
|
+import java.util.function.Function;
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
@@ -42,6 +43,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
import static org.mockito.BDDMockito.given;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
|
/**
|
|
|
* Tests for {@link JwtBearerOAuth2AuthorizedClientProvider}.
|
|
@@ -87,6 +89,13 @@ public class JwtBearerOAuth2AuthorizedClientProviderTests {
|
|
|
.withMessage("accessTokenResponseClient cannot be null");
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void setJwtAssertionResolverWhenNullThenThrowIllegalArgumentException() {
|
|
|
+ assertThatIllegalArgumentException()
|
|
|
+ .isThrownBy(() -> this.authorizedClientProvider.setJwtAssertionResolver(null))
|
|
|
+ .withMessage("jwtAssertionResolver cannot be null");
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void setClockSkewWhenNullThenThrowIllegalArgumentException() {
|
|
|
// @formatter:off
|
|
@@ -198,7 +207,7 @@ public class JwtBearerOAuth2AuthorizedClientProviderTests {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void authorizeWhenJwtBearerAndNotAuthorizedAndPrincipalNotJwtThenUnableToAuthorize() {
|
|
|
+ public void authorizeWhenJwtBearerAndNotAuthorizedAndJwtDoesNotResolveThenUnableToAuthorize() {
|
|
|
// @formatter:off
|
|
|
OAuth2AuthorizationContext authorizationContext = OAuth2AuthorizationContext
|
|
|
.withClientRegistration(this.clientRegistration)
|
|
@@ -209,7 +218,7 @@ public class JwtBearerOAuth2AuthorizedClientProviderTests {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void authorizeWhenJwtBearerAndNotAuthorizedAndPrincipalJwtThenAuthorize() {
|
|
|
+ public void authorizeWhenJwtBearerAndNotAuthorizedAndJwtResolvesThenAuthorize() {
|
|
|
OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses.accessTokenResponse().build();
|
|
|
given(this.accessTokenResponseClient.getTokenResponse(any())).willReturn(accessTokenResponse);
|
|
|
// @formatter:off
|
|
@@ -224,4 +233,25 @@ public class JwtBearerOAuth2AuthorizedClientProviderTests {
|
|
|
assertThat(authorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void authorizeWhenCustomJwtAssertionResolverSetThenUsed() {
|
|
|
+ Function<OAuth2AuthorizationContext, Jwt> jwtAssertionResolver = mock(Function.class);
|
|
|
+ given(jwtAssertionResolver.apply(any())).willReturn(this.jwtAssertion);
|
|
|
+ this.authorizedClientProvider.setJwtAssertionResolver(jwtAssertionResolver);
|
|
|
+ OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses.accessTokenResponse().build();
|
|
|
+ given(this.accessTokenResponseClient.getTokenResponse(any())).willReturn(accessTokenResponse);
|
|
|
+ // @formatter:off
|
|
|
+ TestingAuthenticationToken principal = new TestingAuthenticationToken("user", "password");
|
|
|
+ OAuth2AuthorizationContext authorizationContext = OAuth2AuthorizationContext
|
|
|
+ .withClientRegistration(this.clientRegistration)
|
|
|
+ .principal(principal)
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
+ OAuth2AuthorizedClient authorizedClient = this.authorizedClientProvider.authorize(authorizationContext);
|
|
|
+ verify(jwtAssertionResolver).apply(any());
|
|
|
+ assertThat(authorizedClient.getClientRegistration()).isSameAs(this.clientRegistration);
|
|
|
+ assertThat(authorizedClient.getPrincipalName()).isEqualTo(principal.getName());
|
|
|
+ assertThat(authorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
|
|
|
+ }
|
|
|
+
|
|
|
}
|