26
26
use Symfony \Component \Security \Core \Exception \CredentialsExpiredException ;
27
27
use Symfony \Component \Security \Core \Exception \DisabledException ;
28
28
use Symfony \Component \Security \Core \User \UserCheckerInterface ;
29
- use Symfony \Component \Security \Core \User \UserInterface ;
29
+ use Symfony \Component \Security \Core \User \User ;
30
30
use Symfony \Component \Security \Core \User \UserProviderInterface ;
31
31
use Symfony \Component \Security \Http \Authenticator \Passport \Badge \UserBadge ;
32
32
use Symfony \Component \Security \Http \Authenticator \Passport \Credentials \PasswordCredentials ;
@@ -50,7 +50,7 @@ class OAuthAuthenticatorTest extends \PHPUnit\Framework\TestCase
50
50
protected $ tokenStorage ;
51
51
52
52
/**
53
- * @var \PHPUnit\Framework\MockObject\MockObject|UserInterface
53
+ * @var \PHPUnit\Framework\MockObject\MockObject|User
54
54
*/
55
55
protected $ user ;
56
56
@@ -69,16 +69,20 @@ public function setUp(): void
69
69
$ this ->serverService = $ this ->getMockBuilder (OAuth2::class)
70
70
->disableOriginalConstructor ()
71
71
->setMethods ([
72
+ 'getBearerToken ' ,
72
73
'getVariable ' ,
73
- 'verifyAccessToken '
74
+ 'verifyAccessToken ' ,
74
75
])
75
76
->getMock ()
76
77
;
77
78
$ this ->tokenStorage = $ this ->getMockBuilder (TokenStorageInterface::class)->disableOriginalConstructor ()->getMock ();
78
- $ this ->user = $ this ->getMockBuilder (UserInterface::class)->disableOriginalConstructor ()->getMock ();
79
79
$ this ->userChecker = $ this ->getMockBuilder (UserCheckerInterface::class)->disableOriginalConstructor ()->getMock ();
80
80
$ this ->userProvider = $ this ->getMockBuilder (UserProviderInterface::class)->disableOriginalConstructor ()->getMock ();
81
81
82
+ // mock the core user object rather than the user interface that the new
83
+ // getUserIdentifier method is used rather than the deprecated getUsername
84
+ $ this ->user = $ this ->getMockBuilder (User::class)->disableOriginalConstructor ()->getMock ();
85
+
82
86
$ this ->authenticator = new OAuthAuthenticator (
83
87
$ this ->serverService ,
84
88
$ this ->tokenStorage ,
@@ -89,12 +93,15 @@ public function setUp(): void
89
93
90
94
public function testAuthenticateReturnsPassportIfValid (): void
91
95
{
92
- // expect a token from the token storage
93
- $ token = new OAuthToken ();
94
- $ token ->setToken ('mock_token_string ' );
95
- $ this ->tokenStorage ->expects ($ this ->once ())
96
- ->method ('getToken ' )
97
- ->will ($ this ->returnValue ($ token ))
96
+ // expect the OAuth2 service to get the token from the request header,
97
+ // flagging the authorization header to be removed at the same time
98
+ $ this ->serverService ->expects ($ this ->once ())
99
+ ->method ('getBearerToken ' )
100
+ ->with (
101
+ $ this ->isInstanceOf (Request::class),
102
+ $ this ->equalTo (true )
103
+ )
104
+ ->will ($ this ->returnValue ('mock_token_string ' ))
98
105
;
99
106
100
107
// expect the OAuth2 service to verify the token, returning an access token
@@ -107,18 +114,18 @@ public function testAuthenticateReturnsPassportIfValid(): void
107
114
->will ($ this ->returnValue ($ accessToken ))
108
115
;
109
116
117
+ // expect the username from the user
118
+ $ this ->user ->expects ($ this ->once ())
119
+ ->method ('getUserIdentifier ' )
120
+ ->will ($ this ->returnValue ('test_user ' ))
121
+ ;
122
+
110
123
// expect the user checker to pass
111
124
$ this ->userChecker ->expects ($ this ->once ())
112
125
->method ('checkPreAuth ' )
113
126
->with ($ this ->user )
114
127
;
115
128
116
- // expect the username from the user
117
- $ this ->user ->expects ($ this ->once ())
118
- ->method ('getUsername ' )
119
- ->will ($ this ->returnValue ('test_user ' ))
120
- ;
121
-
122
129
$ passport = $ this ->authenticator ->authenticate (new Request ());
123
130
124
131
$ this ->assertInstanceOf (Passport::class, $ passport );
@@ -128,16 +135,20 @@ public function testAuthenticateReturnsPassportIfValid(): void
128
135
$ this ->assertSame ('test_user ' , $ passport ->getBadge (UserBadge::class)->getUserIdentifier ());
129
136
$ this ->assertSame ('mock_token_string ' , $ passport ->getBadge (OAuthCredentials::class)->getTokenString ());
130
137
$ this ->assertSame (['ROLE_SCOPE_1 ' , 'ROLE_SCOPE_2 ' ], $ passport ->getBadge (OAuthCredentials::class)->getRoles ($ this ->user ));
138
+ $ this ->assertTrue ($ passport ->getBadge (OAuthCredentials::class)->isResolved ());
131
139
}
132
140
133
- public function testAuthenticateReturnsTokenInvalidWhenNullData (): void
141
+ public function testAuthenticateReturnsUnresolvedPassportWhenNullUser (): void
134
142
{
135
- // expect a token from the token storage
136
- $ token = new OAuthToken ();
137
- $ token ->setToken ('mock_token_string ' );
138
- $ this ->tokenStorage ->expects ($ this ->once ())
139
- ->method ('getToken ' )
140
- ->will ($ this ->returnValue ($ token ))
143
+ // expect the OAuth2 service to get the token from the request header,
144
+ // flagging the authorization header to be removed at the same time
145
+ $ this ->serverService ->expects ($ this ->once ())
146
+ ->method ('getBearerToken ' )
147
+ ->with (
148
+ $ this ->isInstanceOf (Request::class),
149
+ $ this ->equalTo (true )
150
+ )
151
+ ->will ($ this ->returnValue ('mock_token_string ' ))
141
152
;
142
153
143
154
// expect the OAuth2 service to verify the token, returning an access
@@ -149,26 +160,29 @@ public function testAuthenticateReturnsTokenInvalidWhenNullData(): void
149
160
->will ($ this ->returnValue ($ accessToken ))
150
161
;
151
162
152
- // expect an authentication exception
153
- $ this ->expectException (AuthenticationException::class);
154
- $ this ->expectExceptionMessage ('OAuth2 authentication failed ' );
163
+ // expect the null user value to not be processed
164
+ $ this ->userChecker ->expects ($ this ->never ())->method ('checkPreAuth ' );
155
165
156
- $ this ->authenticator ->authenticate (new Request ());
166
+ $ passport = $ this ->authenticator ->authenticate (new Request ());
167
+
168
+ // confirm that the returned passport won't pass validation
169
+ $ this ->assertFalse ($ passport ->getBadge (OAuthCredentials::class)->isResolved ());
157
170
}
158
171
159
- public function testAuthenticateTransformsOAuthServerException (): void
172
+ public function testAuthenticateReturnsUnresolvedPassportWhenInvalidToken (): void
160
173
{
161
- // expect a token from the token storage
162
- $ token = new OAuthToken ();
163
- $ token ->setToken ('mock_token_string ' );
164
- $ this ->tokenStorage ->expects ($ this ->once ())
165
- ->method ('getToken ' )
166
- ->will ($ this ->returnValue ($ token ))
174
+ // expect the OAuth2 service to get the token from the request header,
175
+ // flagging the authorization header to be removed at the same time
176
+ $ this ->serverService ->expects ($ this ->once ())
177
+ ->method ('getBearerToken ' )
178
+ ->with (
179
+ $ this ->isInstanceOf (Request::class),
180
+ $ this ->equalTo (true )
181
+ )
182
+ ->will ($ this ->returnValue ('mock_token_string ' ))
167
183
;
168
184
169
- // expect the OAuth2 service to verify the token, returning an access
170
- // token, but without a related user
171
- $ accessToken = new AccessToken ();
185
+ // expect the OAuth2 service to not verify the token, throwing an exception
172
186
$ this ->serverService ->expects ($ this ->once ())
173
187
->method ('verifyAccessToken ' )
174
188
->with ('mock_token_string ' )
@@ -182,21 +196,26 @@ public function testAuthenticateTransformsOAuthServerException(): void
182
196
))
183
197
;
184
198
185
- // expect the thrown exception to be transformed into an authentication exception
186
- $ this ->expectException (AuthenticationException::class);
187
- $ this ->expectExceptionMessage ('OAuth2 authentication failed ' );
199
+ // expect the null user value to not be processed
200
+ $ this ->userChecker ->expects ($ this ->never ())->method ('checkPreAuth ' );
201
+
202
+ $ passport = $ this ->authenticator ->authenticate (new Request ());
188
203
189
- $ this ->authenticator ->authenticate (new Request ());
204
+ // confirm that the returned passport won't pass validation
205
+ $ this ->assertFalse ($ passport ->getBadge (OAuthCredentials::class)->isResolved ());
190
206
}
191
207
192
208
public function testAuthenticateTransformsAccountStatusException (): void
193
209
{
194
- // expect a token from the token storage
195
- $ token = new OAuthToken ();
196
- $ token ->setToken ('mock_token_string ' );
197
- $ this ->tokenStorage ->expects ($ this ->once ())
198
- ->method ('getToken ' )
199
- ->will ($ this ->returnValue ($ token ))
210
+ // expect the OAuth2 service to get the token from the request header,
211
+ // flagging the authorization header to be removed at the same time
212
+ $ this ->serverService ->expects ($ this ->once ())
213
+ ->method ('getBearerToken ' )
214
+ ->with (
215
+ $ this ->isInstanceOf (Request::class),
216
+ $ this ->equalTo (true )
217
+ )
218
+ ->will ($ this ->returnValue ('mock_token_string ' ))
200
219
;
201
220
202
221
// expect the OAuth2 service to verify the token, returning an access token
@@ -216,11 +235,10 @@ public function testAuthenticateTransformsAccountStatusException(): void
216
235
->willThrowException (new DisabledException ('User account is disabled. ' ))
217
236
;
218
237
219
- // expect the thrown exception to be transformed into an authentication exception
220
- $ this ->expectException (AuthenticationException::class);
221
- $ this ->expectExceptionMessage ('OAuth2 authentication failed ' );
238
+ $ passport = $ this ->authenticator ->authenticate (new Request ());
222
239
223
- $ this ->authenticator ->authenticate (new Request ());
240
+ // confirm that the returned passport won't pass validation
241
+ $ this ->assertFalse ($ passport ->getBadge (OAuthCredentials::class)->isResolved ());
224
242
}
225
243
226
244
public function testCreateAuthenticatedTokenWithValidPassport (): void
@@ -245,6 +263,12 @@ public function testCreateAuthenticatedTokenWithValidPassport(): void
245
263
->will ($ this ->returnValue (['ROLE_USER ' ]))
246
264
;
247
265
266
+ // expect a new authenticated token to be stored
267
+ $ this ->tokenStorage ->expects ($ this ->once ())
268
+ ->method ('setToken ' )
269
+ ->with ($ this ->isInstanceOf (OAuthToken::class))
270
+ ;
271
+
248
272
// configure the passport
249
273
$ passport = new Passport (
250
274
new UserBadge ('test_user ' ),
0 commit comments