Skip to content

Commit b077e5c

Browse files
author
Steve Riesenberg
committed
Do not issue refresh token to public client
Closes spring-projectsgh-296
1 parent 7f294ab commit b077e5c

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ public Authentication authenticate(Authentication authentication) throws Authent
191191
jwtAccessToken.getExpiresAt(), authorizedScopes);
192192

193193
OAuth2RefreshToken refreshToken = null;
194-
if (registeredClient.getAuthorizationGrantTypes().contains(AuthorizationGrantType.REFRESH_TOKEN)) {
194+
if (registeredClient.getAuthorizationGrantTypes().contains(AuthorizationGrantType.REFRESH_TOKEN)
195+
&& StringUtils.hasText(registeredClient.getClientSecret())) {
195196
refreshToken = generateRefreshToken(registeredClient.getTokenSettings().getRefreshTokenTimeToLive());
196197
}
197198

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java

+57
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,63 @@ public void authenticateWhenInvalidCodeThenThrowOAuth2AuthenticationException()
165165
.isEqualTo(OAuth2ErrorCodes.INVALID_GRANT);
166166
}
167167

168+
// gh-296
169+
@Test
170+
public void authenticateWhenPublicClientThenRefreshTokenIsNotIssued() {
171+
RegisteredClient registeredClient = TestRegisteredClients.registeredPublicClient()
172+
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
173+
.build();
174+
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
175+
when(this.authorizationService.findByToken(eq(AUTHORIZATION_CODE), eq(AUTHORIZATION_CODE_TOKEN_TYPE)))
176+
.thenReturn(authorization);
177+
178+
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
179+
OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(
180+
OAuth2AuthorizationRequest.class.getName());
181+
OAuth2AuthorizationCodeAuthenticationToken authentication =
182+
new OAuth2AuthorizationCodeAuthenticationToken(AUTHORIZATION_CODE, clientPrincipal, authorizationRequest.getRedirectUri(), null);
183+
184+
when(this.jwtEncoder.encode(any(), any())).thenReturn(createJwt());
185+
186+
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
187+
(OAuth2AccessTokenAuthenticationToken) this.authenticationProvider.authenticate(authentication);
188+
189+
ArgumentCaptor<JwtEncodingContext> jwtEncodingContextCaptor = ArgumentCaptor.forClass(JwtEncodingContext.class);
190+
verify(this.jwtCustomizer).customize(jwtEncodingContextCaptor.capture());
191+
JwtEncodingContext jwtEncodingContext = jwtEncodingContextCaptor.getValue();
192+
assertThat(jwtEncodingContext.getRegisteredClient()).isEqualTo(registeredClient);
193+
assertThat(jwtEncodingContext.<Authentication>getPrincipal()).isEqualTo(authorization.getAttribute(Principal.class.getName()));
194+
assertThat(jwtEncodingContext.getAuthorization()).isEqualTo(authorization);
195+
assertThat(jwtEncodingContext.getAuthorizedScopes())
196+
.isEqualTo(authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME));
197+
assertThat(jwtEncodingContext.getTokenType()).isEqualTo(OAuth2TokenType.ACCESS_TOKEN);
198+
assertThat(jwtEncodingContext.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
199+
assertThat(jwtEncodingContext.<OAuth2AuthorizationGrantAuthenticationToken>getAuthorizationGrant()).isEqualTo(authentication);
200+
assertThat(jwtEncodingContext.getHeaders()).isNotNull();
201+
assertThat(jwtEncodingContext.getClaims()).isNotNull();
202+
203+
ArgumentCaptor<JwtClaimsSet> jwtClaimsSetCaptor = ArgumentCaptor.forClass(JwtClaimsSet.class);
204+
verify(this.jwtEncoder).encode(any(), jwtClaimsSetCaptor.capture());
205+
JwtClaimsSet jwtClaimsSet = jwtClaimsSetCaptor.getValue();
206+
207+
Set<String> scopes = jwtClaimsSet.getClaim(OAuth2ParameterNames.SCOPE);
208+
assertThat(scopes).isEqualTo(authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME));
209+
assertThat(jwtClaimsSet.getSubject()).isEqualTo(authorization.getPrincipalName());
210+
211+
ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class);
212+
verify(this.authorizationService).save(authorizationCaptor.capture());
213+
OAuth2Authorization updatedAuthorization = authorizationCaptor.getValue();
214+
215+
assertThat(accessTokenAuthentication.getRegisteredClient().getId()).isEqualTo(updatedAuthorization.getRegisteredClientId());
216+
assertThat(accessTokenAuthentication.getPrincipal()).isEqualTo(clientPrincipal);
217+
assertThat(accessTokenAuthentication.getAccessToken()).isEqualTo(updatedAuthorization.getAccessToken().getToken());
218+
assertThat(accessTokenAuthentication.getAccessToken().getScopes())
219+
.isEqualTo(authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME));
220+
assertThat(accessTokenAuthentication.getRefreshToken()).isNull();
221+
OAuth2Authorization.Token<OAuth2AuthorizationCode> authorizationCode = updatedAuthorization.getToken(OAuth2AuthorizationCode.class);
222+
assertThat(authorizationCode.isInvalidated()).isTrue();
223+
}
224+
168225
@Test
169226
public void authenticateWhenCodeIssuedToAnotherClientThenThrowOAuth2AuthenticationException() {
170227
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization().build();

0 commit comments

Comments
 (0)