27
27
28
28
import org .springframework .mock .web .MockHttpServletRequest ;
29
29
import org .springframework .mock .web .MockHttpServletResponse ;
30
+ import org .springframework .security .authentication .AuthenticationDetailsSource ;
30
31
import org .springframework .security .authentication .AuthenticationManager ;
31
32
import org .springframework .security .core .Authentication ;
32
33
import org .springframework .security .core .AuthenticationException ;
50
51
import org .springframework .security .oauth2 .core .endpoint .OAuth2ParameterNames ;
51
52
import org .springframework .security .oauth2 .core .user .OAuth2User ;
52
53
import org .springframework .security .web .authentication .AuthenticationFailureHandler ;
54
+ import org .springframework .security .web .authentication .WebAuthenticationDetails ;
53
55
import org .springframework .security .web .util .UrlUtils ;
54
56
import org .springframework .web .util .UriComponentsBuilder ;
55
57
@@ -79,6 +81,7 @@ public class OAuth2LoginAuthenticationFilterTests {
79
81
private AuthorizationRequestRepository <OAuth2AuthorizationRequest > authorizationRequestRepository ;
80
82
private AuthenticationFailureHandler failureHandler ;
81
83
private AuthenticationManager authenticationManager ;
84
+ private AuthenticationDetailsSource authenticationDetailsSource ;
82
85
private OAuth2LoginAuthenticationToken loginAuthentication ;
83
86
private OAuth2LoginAuthenticationFilter filter ;
84
87
@@ -93,11 +96,13 @@ public void setUp() {
93
96
this .authorizationRequestRepository = new HttpSessionOAuth2AuthorizationRequestRepository ();
94
97
this .failureHandler = mock (AuthenticationFailureHandler .class );
95
98
this .authenticationManager = mock (AuthenticationManager .class );
99
+ this .authenticationDetailsSource = mock (AuthenticationDetailsSource .class );
96
100
this .filter = spy (new OAuth2LoginAuthenticationFilter (this .clientRegistrationRepository ,
97
101
this .authorizedClientRepository , OAuth2LoginAuthenticationFilter .DEFAULT_FILTER_PROCESSES_URI ));
98
102
this .filter .setAuthorizationRequestRepository (this .authorizationRequestRepository );
99
103
this .filter .setAuthenticationFailureHandler (this .failureHandler );
100
104
this .filter .setAuthenticationManager (this .authenticationManager );
105
+ this .filter .setAuthenticationDetailsSource (this .authenticationDetailsSource );
101
106
}
102
107
103
108
@ Test
@@ -400,6 +405,29 @@ public void doFilterWhenAuthorizationResponseHasNonDefaultPortThenRedirectUriMat
400
405
assertThat (authorizationResponse .getRedirectUri ()).isEqualTo (expectedRedirectUri );
401
406
}
402
407
408
+ // gh-6866
409
+ @ Test
410
+ public void attemptAuthenticationShouldSetAuthenticationDetailsOnAuthenticationResult () throws Exception {
411
+ String requestUri = "/login/oauth2/code/" + this .registration1 .getRegistrationId ();
412
+ String state = "state" ;
413
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , requestUri );
414
+ request .setServletPath (requestUri );
415
+ request .addParameter (OAuth2ParameterNames .CODE , "code" );
416
+ request .addParameter (OAuth2ParameterNames .STATE , state );
417
+
418
+ WebAuthenticationDetails webAuthenticationDetails = mock (WebAuthenticationDetails .class );
419
+ when (authenticationDetailsSource .buildDetails (any ())).thenReturn (webAuthenticationDetails );
420
+
421
+ MockHttpServletResponse response = new MockHttpServletResponse ();
422
+
423
+ this .setUpAuthorizationRequest (request , response , this .registration2 , state );
424
+ this .setUpAuthenticationResult (this .registration2 );
425
+
426
+ Authentication result = this .filter .attemptAuthentication (request , response );
427
+
428
+ assertThat (result .getDetails ()).isEqualTo (webAuthenticationDetails );
429
+ }
430
+
403
431
private void setUpAuthorizationRequest (HttpServletRequest request , HttpServletResponse response ,
404
432
ClientRegistration registration , String state ) {
405
433
Map <String , Object > attributes = new HashMap <>();
0 commit comments