3333import org .springframework .security .core .Authentication ;
3434import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
3535import org .springframework .security .oauth2 .core .ClientAuthenticationMethod ;
36+ import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
3637import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
3738import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
3839import org .springframework .security .oauth2 .core .OAuth2Token ;
7475import static org .assertj .core .api .Assertions .entry ;
7576import static org .mockito .ArgumentMatchers .any ;
7677import static org .mockito .ArgumentMatchers .eq ;
78+ import static org .mockito .BDDMockito .given ;
79+ import static org .mockito .BDDMockito .willAnswer ;
7780import static org .mockito .Mockito .doAnswer ;
7881import static org .mockito .Mockito .mock ;
7982import static org .mockito .Mockito .spy ;
@@ -118,7 +121,8 @@ public OAuth2Token generate(OAuth2TokenContext context) {
118121 });
119122 this .authenticationProvider = new OAuth2AuthorizationCodeAuthenticationProvider (
120123 this .authorizationService , this .tokenGenerator );
121- AuthorizationServerSettings authorizationServerSettings = AuthorizationServerSettings .builder ().issuer ("https://provider.com" ).build ();
124+ AuthorizationServerSettings authorizationServerSettings = AuthorizationServerSettings .builder ()
125+ .issuer ("https://provider.com" ).build ();
122126 AuthorizationServerContextHolder .setContext (new TestAuthorizationServerContext (authorizationServerSettings , null ));
123127 }
124128
@@ -302,7 +306,8 @@ public void authenticateWhenAccessTokenNotGeneratedThenThrowOAuth2Authentication
302306 OAuth2TokenContext context = answer .getArgument (0 );
303307 if (OAuth2TokenType .ACCESS_TOKEN .equals (context .getTokenType ())) {
304308 return null ;
305- } else {
309+ }
310+ else {
306311 return answer .callRealMethod ();
307312 }
308313 }).when (this .tokenGenerator ).generate (any ());
@@ -317,36 +322,39 @@ public void authenticateWhenAccessTokenNotGeneratedThenThrowOAuth2Authentication
317322 }
318323
319324 @ Test
320- public void authenticateWhenRefreshTokenNotGeneratedThenThrowOAuth2AuthenticationException () {
325+ public void authenticateWhenInvalidRefreshTokenGeneratedThenThrowOAuth2AuthenticationException () {
321326 RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
322327 OAuth2Authorization authorization = TestOAuth2Authorizations .authorization (registeredClient ).build ();
323- when (this .authorizationService .findByToken (eq (AUTHORIZATION_CODE ), eq (AUTHORIZATION_CODE_TOKEN_TYPE )))
324- .thenReturn (authorization );
328+ given (this .authorizationService .findByToken (eq (AUTHORIZATION_CODE ), eq (AUTHORIZATION_CODE_TOKEN_TYPE )))
329+ .willReturn (authorization );
325330
326- OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (
327- registeredClient , ClientAuthenticationMethod .CLIENT_SECRET_BASIC , registeredClient .getClientSecret ());
328- OAuth2AuthorizationRequest authorizationRequest = authorization . getAttribute (
329- OAuth2AuthorizationRequest .class .getName ());
330- OAuth2AuthorizationCodeAuthenticationToken authentication =
331- new OAuth2AuthorizationCodeAuthenticationToken ( AUTHORIZATION_CODE , clientPrincipal , authorizationRequest .getRedirectUri (), null );
331+ OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (registeredClient ,
332+ ClientAuthenticationMethod .CLIENT_SECRET_BASIC , registeredClient .getClientSecret ());
333+ OAuth2AuthorizationRequest authorizationRequest = authorization
334+ . getAttribute ( OAuth2AuthorizationRequest .class .getName ());
335+ OAuth2AuthorizationCodeAuthenticationToken authentication = new OAuth2AuthorizationCodeAuthenticationToken (
336+ AUTHORIZATION_CODE , clientPrincipal , authorizationRequest .getRedirectUri (), null );
332337
333- when (this .jwtEncoder .encode (any ())).thenReturn (createJwt ());
338+ given (this .jwtEncoder .encode (any ())).willReturn (createJwt ());
334339
335- doAnswer ( answer -> {
340+ willAnswer (( answer ) -> {
336341 OAuth2TokenContext context = answer .getArgument (0 );
337342 if (OAuth2TokenType .REFRESH_TOKEN .equals (context .getTokenType ())) {
338- return null ;
339- } else {
343+ return new OAuth2AccessToken (OAuth2AccessToken .TokenType .BEARER , "access-token" , Instant .now (),
344+ Instant .now ().plusSeconds (300 ));
345+ }
346+ else {
340347 return answer .callRealMethod ();
341348 }
342- }).when (this .tokenGenerator ).generate (any ());
349+ }).given (this .tokenGenerator ).generate (any ());
343350
344351 assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
345352 .isInstanceOf (OAuth2AuthenticationException .class )
346- .extracting (ex -> ((OAuth2AuthenticationException ) ex ).getError ())
347- .satisfies (error -> {
353+ .extracting (( ex ) -> ((OAuth2AuthenticationException ) ex ).getError ())
354+ .satisfies (( error ) -> {
348355 assertThat (error .getErrorCode ()).isEqualTo (OAuth2ErrorCodes .SERVER_ERROR );
349- assertThat (error .getDescription ()).contains ("The token generator failed to generate the refresh token." );
356+ assertThat (error .getDescription ())
357+ .contains ("The token generator failed to generate a valid refresh token." );
350358 });
351359 }
352360
@@ -370,7 +378,8 @@ public void authenticateWhenIdTokenNotGeneratedThenThrowOAuth2AuthenticationExce
370378 OAuth2TokenContext context = answer .getArgument (0 );
371379 if (OidcParameterNames .ID_TOKEN .equals (context .getTokenType ().getValue ())) {
372380 return null ;
373- } else {
381+ }
382+ else {
374383 return answer .callRealMethod ();
375384 }
376385 }).when (this .tokenGenerator ).generate (any ());
@@ -428,12 +437,16 @@ public void authenticateWhenValidCodeThenReturnAccessToken() {
428437 verify (this .authorizationService ).save (authorizationCaptor .capture ());
429438 OAuth2Authorization updatedAuthorization = authorizationCaptor .getValue ();
430439
431- assertThat (accessTokenAuthentication .getRegisteredClient ().getId ()).isEqualTo (updatedAuthorization .getRegisteredClientId ());
440+ assertThat (accessTokenAuthentication .getRegisteredClient ()
441+ .getId ()).isEqualTo (updatedAuthorization .getRegisteredClientId ());
432442 assertThat (accessTokenAuthentication .getPrincipal ()).isEqualTo (clientPrincipal );
433- assertThat (accessTokenAuthentication .getAccessToken ()).isEqualTo (updatedAuthorization .getAccessToken ().getToken ());
434- assertThat (accessTokenAuthentication .getAccessToken ().getScopes ()).isEqualTo (authorization .getAuthorizedScopes ());
443+ assertThat (accessTokenAuthentication .getAccessToken ()).isEqualTo (updatedAuthorization .getAccessToken ()
444+ .getToken ());
445+ assertThat (accessTokenAuthentication .getAccessToken ()
446+ .getScopes ()).isEqualTo (authorization .getAuthorizedScopes ());
435447 assertThat (accessTokenAuthentication .getRefreshToken ()).isNotNull ();
436- assertThat (accessTokenAuthentication .getRefreshToken ()).isEqualTo (updatedAuthorization .getRefreshToken ().getToken ());
448+ assertThat (accessTokenAuthentication .getRefreshToken ()).isEqualTo (updatedAuthorization .getRefreshToken ()
449+ .getToken ());
437450 OAuth2Authorization .Token <OAuth2AuthorizationCode > authorizationCode = updatedAuthorization .getToken (OAuth2AuthorizationCode .class );
438451 assertThat (authorizationCode .isInvalidated ()).isTrue ();
439452 }
@@ -443,7 +456,8 @@ public void authenticateWhenValidCodeAndAuthenticationRequestThenReturnIdToken()
443456 RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().scope (OidcScopes .OPENID ).build ();
444457 OAuth2AuthorizationCode authorizationCode = new OAuth2AuthorizationCode (
445458 "code" , Instant .now (), Instant .now ().plusSeconds (120 ));
446- OAuth2Authorization authorization = TestOAuth2Authorizations .authorization (registeredClient , authorizationCode ).build ();
459+ OAuth2Authorization authorization = TestOAuth2Authorizations .authorization (registeredClient , authorizationCode )
460+ .build ();
447461 when (this .authorizationService .findByToken (eq (AUTHORIZATION_CODE ), eq (AUTHORIZATION_CODE_TOKEN_TYPE )))
448462 .thenReturn (authorization );
449463
@@ -490,19 +504,22 @@ public void authenticateWhenValidCodeAndAuthenticationRequestThenReturnIdToken()
490504 assertThat (idTokenContext .getJwsHeader ()).isNotNull ();
491505 assertThat (idTokenContext .getClaims ()).isNotNull ();
492506
493- verify (this .jwtEncoder , times (2 )).encode (any ()); // Access token and ID Token
507+ verify (this .jwtEncoder , times (2 )).encode (any ()); // Access token and ID Token
494508
495509 ArgumentCaptor <OAuth2Authorization > authorizationCaptor = ArgumentCaptor .forClass (OAuth2Authorization .class );
496510 verify (this .authorizationService ).save (authorizationCaptor .capture ());
497511 OAuth2Authorization updatedAuthorization = authorizationCaptor .getValue ();
498512
499- assertThat (accessTokenAuthentication .getRegisteredClient ().getId ()).isEqualTo (updatedAuthorization .getRegisteredClientId ());
513+ assertThat (accessTokenAuthentication .getRegisteredClient ()
514+ .getId ()).isEqualTo (updatedAuthorization .getRegisteredClientId ());
500515 assertThat (accessTokenAuthentication .getPrincipal ()).isEqualTo (clientPrincipal );
501- assertThat (accessTokenAuthentication .getAccessToken ()).isEqualTo (updatedAuthorization .getAccessToken ().getToken ());
516+ assertThat (accessTokenAuthentication .getAccessToken ()).isEqualTo (updatedAuthorization .getAccessToken ()
517+ .getToken ());
502518 Set <String > accessTokenScopes = new HashSet <>(updatedAuthorization .getAuthorizedScopes ());
503519 assertThat (accessTokenAuthentication .getAccessToken ().getScopes ()).isEqualTo (accessTokenScopes );
504520 assertThat (accessTokenAuthentication .getRefreshToken ()).isNotNull ();
505- assertThat (accessTokenAuthentication .getRefreshToken ()).isEqualTo (updatedAuthorization .getRefreshToken ().getToken ());
521+ assertThat (accessTokenAuthentication .getRefreshToken ()).isEqualTo (updatedAuthorization .getRefreshToken ()
522+ .getToken ());
506523 OAuth2Authorization .Token <OAuth2AuthorizationCode > authorizationCodeToken = updatedAuthorization .getToken (OAuth2AuthorizationCode .class );
507524 assertThat (authorizationCodeToken .isInvalidated ()).isTrue ();
508525 OAuth2Authorization .Token <OidcIdToken > idToken = updatedAuthorization .getToken (OidcIdToken .class );
@@ -558,10 +575,13 @@ public void authenticateWhenPublicClientThenRefreshTokenNotIssued() {
558575 verify (this .authorizationService ).save (authorizationCaptor .capture ());
559576 OAuth2Authorization updatedAuthorization = authorizationCaptor .getValue ();
560577
561- assertThat (accessTokenAuthentication .getRegisteredClient ().getId ()).isEqualTo (updatedAuthorization .getRegisteredClientId ());
578+ assertThat (accessTokenAuthentication .getRegisteredClient ()
579+ .getId ()).isEqualTo (updatedAuthorization .getRegisteredClientId ());
562580 assertThat (accessTokenAuthentication .getPrincipal ()).isEqualTo (clientPrincipal );
563- assertThat (accessTokenAuthentication .getAccessToken ()).isEqualTo (updatedAuthorization .getAccessToken ().getToken ());
564- assertThat (accessTokenAuthentication .getAccessToken ().getScopes ()).isEqualTo (authorization .getAuthorizedScopes ());
581+ assertThat (accessTokenAuthentication .getAccessToken ()).isEqualTo (updatedAuthorization .getAccessToken ()
582+ .getToken ());
583+ assertThat (accessTokenAuthentication .getAccessToken ()
584+ .getScopes ()).isEqualTo (authorization .getAuthorizedScopes ());
565585 assertThat (accessTokenAuthentication .getRefreshToken ()).isNull ();
566586 OAuth2Authorization .Token <OAuth2AuthorizationCode > authorizationCode = updatedAuthorization .getToken (OAuth2AuthorizationCode .class );
567587 assertThat (authorizationCode .isInvalidated ()).isTrue ();
@@ -600,13 +620,17 @@ public void authenticateWhenTokenTimeToLiveConfiguredThenTokenExpirySet() {
600620 verify (this .authorizationService ).save (authorizationCaptor .capture ());
601621 OAuth2Authorization updatedAuthorization = authorizationCaptor .getValue ();
602622
603- assertThat (accessTokenAuthentication .getAccessToken ()).isEqualTo (updatedAuthorization .getAccessToken ().getToken ());
604- Instant expectedAccessTokenExpiresAt = accessTokenAuthentication .getAccessToken ().getIssuedAt ().plus (accessTokenTTL );
623+ assertThat (accessTokenAuthentication .getAccessToken ()).isEqualTo (updatedAuthorization .getAccessToken ()
624+ .getToken ());
625+ Instant expectedAccessTokenExpiresAt = accessTokenAuthentication .getAccessToken ().getIssuedAt ()
626+ .plus (accessTokenTTL );
605627 assertThat (accessTokenAuthentication .getAccessToken ().getExpiresAt ()).isBetween (
606628 expectedAccessTokenExpiresAt .minusSeconds (1 ), expectedAccessTokenExpiresAt .plusSeconds (1 ));
607629
608- assertThat (accessTokenAuthentication .getRefreshToken ()).isEqualTo (updatedAuthorization .getRefreshToken ().getToken ());
609- Instant expectedRefreshTokenExpiresAt = accessTokenAuthentication .getRefreshToken ().getIssuedAt ().plus (refreshTokenTTL );
630+ assertThat (accessTokenAuthentication .getRefreshToken ()).isEqualTo (updatedAuthorization .getRefreshToken ()
631+ .getToken ());
632+ Instant expectedRefreshTokenExpiresAt = accessTokenAuthentication .getRefreshToken ().getIssuedAt ()
633+ .plus (refreshTokenTTL );
610634 assertThat (accessTokenAuthentication .getRefreshToken ().getExpiresAt ()).isBetween (
611635 expectedRefreshTokenExpiresAt .minusSeconds (1 ), expectedRefreshTokenExpiresAt .plusSeconds (1 ));
612636 }
0 commit comments