Skip to content

Commit 826c550

Browse files
committed
Merge branch '1.1.x'
2 parents a74f90d + 2895169 commit 826c550

File tree

2 files changed

+47
-2
lines changed
  • oauth2-authorization-server/src

2 files changed

+47
-2
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/token/JwtGenerator.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ public Jwt generate(OAuth2TokenContext context) {
134134
}
135135
} else if (AuthorizationGrantType.REFRESH_TOKEN.equals(context.getAuthorizationGrantType())) {
136136
OidcIdToken currentIdToken = context.getAuthorization().getToken(OidcIdToken.class).getToken();
137-
claimsBuilder.claim("sid", currentIdToken.getClaim("sid"));
138-
claimsBuilder.claim(IdTokenClaimNames.AUTH_TIME, currentIdToken.<Date>getClaim(IdTokenClaimNames.AUTH_TIME));
137+
if (currentIdToken.hasClaim("sid")) {
138+
claimsBuilder.claim("sid", currentIdToken.getClaim("sid"));
139+
}
140+
if (currentIdToken.hasClaim(IdTokenClaimNames.AUTH_TIME)) {
141+
claimsBuilder.claim(IdTokenClaimNames.AUTH_TIME, currentIdToken.<Date>getClaim(IdTokenClaimNames.AUTH_TIME));
142+
}
139143
}
140144
}
141145
// @formatter:on

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/token/JwtGeneratorTests.java

+41
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,47 @@ public void generateWhenIdTokenTypeAndRefreshTokenGrantThenReturnJwt() {
236236
assertGeneratedTokenType(tokenContext);
237237
}
238238

239+
// gh-1283
240+
@Test
241+
public void generateWhenIdTokenTypeWithoutSidAndRefreshTokenGrantThenReturnJwt() {
242+
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
243+
.scope(OidcScopes.OPENID)
244+
.build();
245+
OidcIdToken idToken = OidcIdToken.withTokenValue("id-token")
246+
.issuer("https://provider.com")
247+
.subject("subject")
248+
.issuedAt(Instant.now())
249+
.expiresAt(Instant.now().plusSeconds(60))
250+
.build();
251+
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
252+
.token(idToken)
253+
.build();
254+
255+
OAuth2RefreshToken refreshToken = authorization.getRefreshToken().getToken();
256+
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
257+
registeredClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, registeredClient.getClientSecret());
258+
259+
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
260+
refreshToken.getTokenValue(), clientPrincipal, null, null);
261+
262+
Authentication principal = authorization.getAttribute(Principal.class.getName());
263+
264+
// @formatter:off
265+
OAuth2TokenContext tokenContext = DefaultOAuth2TokenContext.builder()
266+
.registeredClient(registeredClient)
267+
.principal(principal)
268+
.authorizationServerContext(this.authorizationServerContext)
269+
.authorization(authorization)
270+
.authorizedScopes(authorization.getAuthorizedScopes())
271+
.tokenType(ID_TOKEN_TOKEN_TYPE)
272+
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
273+
.authorizationGrant(authentication)
274+
.build();
275+
// @formatter:on
276+
277+
assertGeneratedTokenType(tokenContext);
278+
}
279+
239280
private void assertGeneratedTokenType(OAuth2TokenContext tokenContext) {
240281
this.jwtGenerator.generate(tokenContext);
241282

0 commit comments

Comments
 (0)