15
15
*/
16
16
package org .springframework .security .oauth2 .client .authentication ;
17
17
18
+ import java .util .Collections ;
19
+
18
20
import org .junit .Before ;
19
21
import org .junit .Test ;
20
- import org .junit .runner .RunWith ;
21
- import org .powermock .core .classloader .annotations .PrepareForTest ;
22
- import org .powermock .modules .junit4 .PowerMockRunner ;
22
+
23
23
import org .springframework .security .oauth2 .client .endpoint .OAuth2AccessTokenResponseClient ;
24
24
import org .springframework .security .oauth2 .client .endpoint .OAuth2AuthorizationCodeGrantRequest ;
25
25
import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
26
- import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
27
26
import org .springframework .security .oauth2 .core .OAuth2AuthorizationException ;
28
- import org .springframework .security .oauth2 .core .OAuth2Error ;
29
27
import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
30
- import org .springframework .security .oauth2 .core .OAuth2RefreshToken ;
31
28
import org .springframework .security .oauth2 .core .endpoint .OAuth2AccessTokenResponse ;
32
29
import org .springframework .security .oauth2 .core .endpoint .OAuth2AuthorizationExchange ;
33
30
import org .springframework .security .oauth2 .core .endpoint .OAuth2AuthorizationRequest ;
34
31
import org .springframework .security .oauth2 .core .endpoint .OAuth2AuthorizationResponse ;
35
32
36
- import java .util .Collections ;
37
-
38
33
import static org .assertj .core .api .Assertions .assertThat ;
39
34
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
40
35
import static org .mockito .ArgumentMatchers .any ;
41
36
import static org .mockito .Mockito .mock ;
42
37
import static org .mockito .Mockito .when ;
38
+ import static org .springframework .security .oauth2 .client .registration .TestClientRegistrations .clientRegistration ;
39
+ import static org .springframework .security .oauth2 .core .endpoint .TestOAuth2AccessTokenResponses .accessTokenResponse ;
40
+ import static org .springframework .security .oauth2 .core .endpoint .TestOAuth2AuthorizationRequests .request ;
41
+ import static org .springframework .security .oauth2 .core .endpoint .TestOAuth2AuthorizationResponses .error ;
42
+ import static org .springframework .security .oauth2 .core .endpoint .TestOAuth2AuthorizationResponses .success ;
43
43
44
44
/**
45
45
* Tests for {@link OAuth2AuthorizationCodeAuthenticationProvider}.
46
46
*
47
47
* @author Joe Grandja
48
48
*/
49
- @ PrepareForTest ({ClientRegistration .class , OAuth2AuthorizationRequest .class ,
50
- OAuth2AuthorizationResponse .class , OAuth2AccessTokenResponse .class })
51
- @ RunWith (PowerMockRunner .class )
52
49
public class OAuth2AuthorizationCodeAuthenticationProviderTests {
53
50
private ClientRegistration clientRegistration ;
54
51
private OAuth2AuthorizationRequest authorizationRequest ;
55
- private OAuth2AuthorizationResponse authorizationResponse ;
56
- private OAuth2AuthorizationExchange authorizationExchange ;
57
52
private OAuth2AccessTokenResponseClient <OAuth2AuthorizationCodeGrantRequest > accessTokenResponseClient ;
58
53
private OAuth2AuthorizationCodeAuthenticationProvider authenticationProvider ;
59
54
60
55
@ Before
61
56
@ SuppressWarnings ("unchecked" )
62
- public void setUp () throws Exception {
63
- this .clientRegistration = mock (ClientRegistration .class );
64
- this .authorizationRequest = mock (OAuth2AuthorizationRequest .class );
65
- this .authorizationResponse = mock (OAuth2AuthorizationResponse .class );
66
- this .authorizationExchange = new OAuth2AuthorizationExchange (this .authorizationRequest , this .authorizationResponse );
57
+ public void setUp () {
58
+ this .clientRegistration = clientRegistration ().build ();
59
+ this .authorizationRequest = request ().build ();
67
60
this .accessTokenResponseClient = mock (OAuth2AccessTokenResponseClient .class );
68
61
this .authenticationProvider = new OAuth2AuthorizationCodeAuthenticationProvider (this .accessTokenResponseClient );
69
-
70
- when (this .authorizationRequest .getState ()).thenReturn ("12345" );
71
- when (this .authorizationResponse .getState ()).thenReturn ("12345" );
72
- when (this .authorizationRequest .getRedirectUri ()).thenReturn ("http://example.com" );
73
- when (this .authorizationResponse .getRedirectUri ()).thenReturn ("http://example.com" );
74
62
}
75
63
76
64
@ Test
@@ -86,60 +74,62 @@ public void supportsWhenTypeOAuth2AuthorizationCodeAuthenticationTokenThenReturn
86
74
87
75
@ Test
88
76
public void authenticateWhenAuthorizationErrorResponseThenThrowOAuth2AuthorizationException () {
89
- when (this .authorizationResponse .statusError ()).thenReturn (true );
90
- when (this .authorizationResponse .getError ()).thenReturn (new OAuth2Error (OAuth2ErrorCodes .INVALID_REQUEST ));
77
+ OAuth2AuthorizationResponse authorizationResponse = error ().errorCode (OAuth2ErrorCodes .INVALID_REQUEST ).build ();
78
+ OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange (
79
+ this .authorizationRequest , authorizationResponse );
91
80
92
81
assertThatThrownBy (() -> {
93
82
this .authenticationProvider .authenticate (
94
83
new OAuth2AuthorizationCodeAuthenticationToken (
95
- this .clientRegistration , this . authorizationExchange ));
84
+ this .clientRegistration , authorizationExchange ));
96
85
}).isInstanceOf (OAuth2AuthorizationException .class ).hasMessageContaining (OAuth2ErrorCodes .INVALID_REQUEST );
97
86
}
98
87
99
88
@ Test
100
89
public void authenticateWhenAuthorizationResponseStateNotEqualAuthorizationRequestStateThenThrowOAuth2AuthorizationException () {
101
- when (this .authorizationRequest .getState ()).thenReturn ("12345" );
102
- when (this .authorizationResponse .getState ()).thenReturn ("67890" );
90
+ OAuth2AuthorizationResponse authorizationResponse = success ().state ("67890" ).build ();
91
+ OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange (
92
+ this .authorizationRequest , authorizationResponse );
103
93
104
94
assertThatThrownBy (() -> {
105
95
this .authenticationProvider .authenticate (
106
96
new OAuth2AuthorizationCodeAuthenticationToken (
107
- this .clientRegistration , this . authorizationExchange ));
97
+ this .clientRegistration , authorizationExchange ));
108
98
}).isInstanceOf (OAuth2AuthorizationException .class ).hasMessageContaining ("invalid_state_parameter" );
109
99
}
110
100
111
101
@ Test
112
102
public void authenticateWhenAuthorizationResponseRedirectUriNotEqualAuthorizationRequestRedirectUriThenThrowOAuth2AuthorizationException () {
113
- when (this .authorizationRequest .getRedirectUri ()).thenReturn ("http://example.com" );
114
- when (this .authorizationResponse .getRedirectUri ()).thenReturn ("http://example2.com" );
103
+ OAuth2AuthorizationResponse authorizationResponse = success ().redirectUri ("http://example2.com" ).build ();
104
+ OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange (
105
+ this .authorizationRequest , authorizationResponse );
115
106
116
107
assertThatThrownBy (() -> {
117
108
this .authenticationProvider .authenticate (
118
109
new OAuth2AuthorizationCodeAuthenticationToken (
119
- this .clientRegistration , this . authorizationExchange ));
110
+ this .clientRegistration , authorizationExchange ));
120
111
}).isInstanceOf (OAuth2AuthorizationException .class ).hasMessageContaining ("invalid_redirect_uri_parameter" );
121
112
}
122
113
123
114
@ Test
124
115
public void authenticateWhenAuthorizationSuccessResponseThenExchangedForAccessToken () {
125
- OAuth2AccessToken accessToken = mock (OAuth2AccessToken .class );
126
- OAuth2RefreshToken refreshToken = mock (OAuth2RefreshToken .class );
127
- OAuth2AccessTokenResponse accessTokenResponse = mock (OAuth2AccessTokenResponse .class );
128
- when (accessTokenResponse .getAccessToken ()).thenReturn (accessToken );
129
- when (accessTokenResponse .getRefreshToken ()).thenReturn (refreshToken );
116
+ OAuth2AccessTokenResponse accessTokenResponse = accessTokenResponse ().refreshToken ("refresh" ).build ();
130
117
when (this .accessTokenResponseClient .getTokenResponse (any ())).thenReturn (accessTokenResponse );
131
118
119
+ OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange (
120
+ this .authorizationRequest , success ().build ());
132
121
OAuth2AuthorizationCodeAuthenticationToken authenticationResult =
133
122
(OAuth2AuthorizationCodeAuthenticationToken ) this .authenticationProvider .authenticate (
134
- new OAuth2AuthorizationCodeAuthenticationToken (this .clientRegistration , this . authorizationExchange ));
123
+ new OAuth2AuthorizationCodeAuthenticationToken (this .clientRegistration , authorizationExchange ));
135
124
136
125
assertThat (authenticationResult .isAuthenticated ()).isTrue ();
137
126
assertThat (authenticationResult .getPrincipal ()).isEqualTo (this .clientRegistration .getClientId ());
138
- assertThat (authenticationResult .getCredentials ()).isEqualTo (accessToken .getTokenValue ());
127
+ assertThat (authenticationResult .getCredentials ())
128
+ .isEqualTo (accessTokenResponse .getAccessToken ().getTokenValue ());
139
129
assertThat (authenticationResult .getAuthorities ()).isEqualTo (Collections .emptyList ());
140
130
assertThat (authenticationResult .getClientRegistration ()).isEqualTo (this .clientRegistration );
141
- assertThat (authenticationResult .getAuthorizationExchange ()).isEqualTo (this . authorizationExchange );
142
- assertThat (authenticationResult .getAccessToken ()).isEqualTo (accessToken );
143
- assertThat (authenticationResult .getRefreshToken ()).isEqualTo (refreshToken );
131
+ assertThat (authenticationResult .getAuthorizationExchange ()).isEqualTo (authorizationExchange );
132
+ assertThat (authenticationResult .getAccessToken ()).isEqualTo (accessTokenResponse . getAccessToken () );
133
+ assertThat (authenticationResult .getRefreshToken ()).isEqualTo (accessTokenResponse . getRefreshToken () );
144
134
}
145
135
}
0 commit comments