Skip to content

Commit 49b199c

Browse files
committed
Polish gh-1429
1 parent d4ae69b commit 49b199c

File tree

5 files changed

+91
-92
lines changed

5 files changed

+91
-92
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AccessTokenAuthenticationContext.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.authentication;
1717

18-
import org.springframework.lang.Nullable;
19-
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
20-
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2AccessTokenResponseAuthenticationSuccessHandler;
21-
import org.springframework.util.Assert;
22-
2318
import java.util.Collections;
2419
import java.util.HashMap;
2520
import java.util.Map;
2621
import java.util.function.Consumer;
2722

23+
import org.springframework.lang.Nullable;
24+
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
25+
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2AccessTokenResponseAuthenticationSuccessHandler;
26+
import org.springframework.util.Assert;
27+
2828
/**
29-
* An {@link OAuth2AuthenticationContext} that holds an {@link OAuth2AccessTokenResponse.Builder}
30-
* and is used when customizing the building of the {@link OAuth2AccessTokenResponse}.
29+
* An {@link OAuth2AuthenticationContext} that holds an {@link OAuth2AccessTokenAuthenticationToken} and additional information
30+
* and is used when customizing the {@link OAuth2AccessTokenResponse}.
3131
*
3232
* @author Dmitriy Dubson
33+
* @since 1.3
3334
* @see OAuth2AuthenticationContext
35+
* @see OAuth2AccessTokenAuthenticationToken
3436
* @see OAuth2AccessTokenResponse
3537
* @see OAuth2AccessTokenResponseAuthenticationSuccessHandler#setAccessTokenResponseCustomizer(Consumer)
36-
* @since 1.3
3738
*/
3839
public final class OAuth2AccessTokenAuthenticationContext implements OAuth2AuthenticationContext {
3940
private final Map<Object, Object> context;
@@ -56,7 +57,8 @@ public boolean hasKey(Object key) {
5657
}
5758

5859
/**
59-
* Returns the {@link OAuth2AccessTokenResponse.Builder} access token response builder
60+
* Returns the {@link OAuth2AccessTokenResponse.Builder access token response builder}.
61+
*
6062
* @return the {@link OAuth2AccessTokenResponse.Builder}
6163
*/
6264
public OAuth2AccessTokenResponse.Builder getAccessTokenResponse() {
@@ -69,20 +71,22 @@ public OAuth2AccessTokenResponse.Builder getAccessTokenResponse() {
6971
* @param authentication the {@link OAuth2AccessTokenAuthenticationToken}
7072
* @return the {@link Builder}
7173
*/
72-
public static OAuth2AccessTokenAuthenticationContext.Builder with(OAuth2AccessTokenAuthenticationToken authentication) {
73-
return new OAuth2AccessTokenAuthenticationContext.Builder(authentication);
74+
public static Builder with(OAuth2AccessTokenAuthenticationToken authentication) {
75+
return new Builder(authentication);
7476
}
7577

7678
/**
77-
* A builder for {@link OAuth2AccessTokenAuthenticationContext}
79+
* A builder for {@link OAuth2AccessTokenAuthenticationContext}.
7880
*/
7981
public static final class Builder extends AbstractBuilder<OAuth2AccessTokenAuthenticationContext, Builder> {
82+
8083
private Builder(OAuth2AccessTokenAuthenticationToken authentication) {
8184
super(authentication);
8285
}
8386

8487
/**
85-
* Sets the {@link OAuth2AccessTokenResponse.Builder} access token response builder
88+
* Sets the {@link OAuth2AccessTokenResponse.Builder access token response builder}.
89+
*
8690
* @param accessTokenResponse the {@link OAuth2AccessTokenResponse.Builder}
8791
* @return the {@link Builder} for further configuration
8892
*/
@@ -97,8 +101,9 @@ public Builder accessTokenResponse(OAuth2AccessTokenResponse.Builder accessToken
97101
*/
98102
public OAuth2AccessTokenAuthenticationContext build() {
99103
Assert.notNull(get(OAuth2AccessTokenResponse.Builder.class), "accessTokenResponse cannot be null");
100-
101104
return new OAuth2AccessTokenAuthenticationContext(getContext());
102105
}
106+
103107
}
108+
104109
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2TokenEndpointFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import jakarta.servlet.ServletException;
2323
import jakarta.servlet.http.HttpServletRequest;
2424
import jakarta.servlet.http.HttpServletResponse;
25+
2526
import org.springframework.core.log.LogMessage;
2627
import org.springframework.http.HttpMethod;
2728
import org.springframework.security.authentication.AbstractAuthenticationToken;
@@ -41,12 +42,12 @@
4142
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2DeviceCodeAuthenticationProvider;
4243
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2RefreshTokenAuthenticationProvider;
4344
import org.springframework.security.oauth2.server.authorization.web.authentication.DelegatingAuthenticationConverter;
45+
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2AccessTokenResponseAuthenticationSuccessHandler;
4446
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2AuthorizationCodeAuthenticationConverter;
4547
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2ClientCredentialsAuthenticationConverter;
4648
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2DeviceCodeAuthenticationConverter;
4749
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2ErrorAuthenticationFailureHandler;
4850
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2RefreshTokenAuthenticationConverter;
49-
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2AccessTokenResponseAuthenticationSuccessHandler;
5051
import org.springframework.security.web.authentication.AuthenticationConverter;
5152
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
5253
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/authentication/OAuth2AccessTokenResponseAuthenticationSuccessHandler.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@
2323
import jakarta.servlet.ServletException;
2424
import jakarta.servlet.http.HttpServletRequest;
2525
import jakarta.servlet.http.HttpServletResponse;
26+
2627
import org.apache.commons.logging.Log;
2728
import org.apache.commons.logging.LogFactory;
29+
2830
import org.springframework.http.converter.HttpMessageConverter;
2931
import org.springframework.http.server.ServletServerHttpResponse;
3032
import org.springframework.security.core.Authentication;
31-
import org.springframework.security.oauth2.core.*;
33+
import org.springframework.security.oauth2.core.OAuth2AccessToken;
34+
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
35+
import org.springframework.security.oauth2.core.OAuth2Error;
36+
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
37+
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
3238
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
3339
import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter;
3440
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationContext;
@@ -42,20 +48,19 @@
4248
* and returning the {@link OAuth2AccessTokenResponse Access Token Response}.
4349
*
4450
* @author Dmitriy Dubson
51+
* @since 1.3
4552
* @see AuthenticationSuccessHandler
4653
* @see OAuth2AccessTokenResponseHttpMessageConverter
47-
* @since 1.3
4854
*/
4955
public final class OAuth2AccessTokenResponseAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
5056
private final Log logger = LogFactory.getLog(getClass());
51-
5257
private final HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenResponseConverter =
5358
new OAuth2AccessTokenResponseHttpMessageConverter();
54-
5559
private Consumer<OAuth2AccessTokenAuthenticationContext> accessTokenResponseCustomizer;
5660

5761
@Override
58-
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
62+
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
63+
Authentication authentication) throws IOException, ServletException {
5964
if (!(authentication instanceof OAuth2AccessTokenAuthenticationToken accessTokenAuthentication)) {
6065
if (this.logger.isErrorEnabled()) {
6166
this.logger.error(Authentication.class.getSimpleName() + " must be of type " +
@@ -112,4 +117,5 @@ public void setAccessTokenResponseCustomizer(Consumer<OAuth2AccessTokenAuthentic
112117
Assert.notNull(accessTokenResponseCustomizer, "accessTokenResponseCustomizer cannot be null");
113118
this.accessTokenResponseCustomizer = accessTokenResponseCustomizer;
114119
}
120+
115121
}
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.authentication;
1717

18-
1918
import org.junit.jupiter.api.Test;
19+
2020
import org.springframework.security.core.Authentication;
21+
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
2122
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
2223
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
2324
import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations;
2425
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
2526
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
2627

27-
import java.security.Principal;
28-
2928
import static org.assertj.core.api.Assertions.assertThat;
3029
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3130

@@ -34,12 +33,14 @@
3433
*
3534
* @author Dmitriy Dubson
3635
*/
37-
public class OAuth2AccessTokenAuthenticationContextTest {
36+
public class OAuth2AccessTokenAuthenticationContextTests {
3837
private final RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
3938
private final OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(this.registeredClient).build();
40-
private final Authentication principal = this.authorization.getAttribute(Principal.class.getName());
41-
private final OAuth2AccessTokenAuthenticationToken accessTokenAuthenticationToken = new OAuth2AccessTokenAuthenticationToken(registeredClient, principal,
42-
authorization.getAccessToken().getToken(), authorization.getRefreshToken().getToken());
39+
private OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
40+
this.registeredClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, this.registeredClient.getClientSecret());
41+
private final OAuth2AccessTokenAuthenticationToken accessTokenAuthenticationToken =
42+
new OAuth2AccessTokenAuthenticationToken(this.registeredClient, this.clientPrincipal,
43+
this.authorization.getAccessToken().getToken(), this.authorization.getRefreshToken().getToken());
4344

4445
@Test
4546
public void withWhenAuthenticationNullThenThrowIllegalArgumentException() {
@@ -54,12 +55,14 @@ public void setWhenValueNullThenThrowIllegalArgumentException() {
5455
OAuth2AccessTokenAuthenticationContext.with(this.accessTokenAuthenticationToken);
5556

5657
assertThatThrownBy(() -> builder.accessTokenResponse(null))
57-
.isInstanceOf(IllegalArgumentException.class).hasMessage("value cannot be null");
58+
.isInstanceOf(IllegalArgumentException.class)
59+
.hasMessage("value cannot be null");
5860
}
5961

6062
@Test
6163
public void buildWhenAllValuesProvidedThenAllValuesAreSet() {
62-
OAuth2AccessTokenResponse.Builder accessTokenResponseBuilder = OAuth2AccessTokenResponse.withToken(this.accessTokenAuthenticationToken.getAccessToken().getTokenValue());
64+
OAuth2AccessTokenResponse.Builder accessTokenResponseBuilder =
65+
OAuth2AccessTokenResponse.withToken(this.accessTokenAuthenticationToken.getAccessToken().getTokenValue());
6366
OAuth2AccessTokenAuthenticationContext context =
6467
OAuth2AccessTokenAuthenticationContext.with(this.accessTokenAuthenticationToken)
6568
.accessTokenResponse(accessTokenResponseBuilder)
@@ -68,4 +71,5 @@ public void buildWhenAllValuesProvidedThenAllValuesAreSet() {
6871
assertThat(context.<Authentication>getAuthentication()).isEqualTo(this.accessTokenAuthenticationToken);
6972
assertThat(context.getAccessTokenResponse()).isEqualTo(accessTokenResponseBuilder);
7073
}
74+
7175
}

0 commit comments

Comments
 (0)