2
2
3
3
import static org .hamcrest .CoreMatchers .hasItems ;
4
4
import static org .hamcrest .CoreMatchers .instanceOf ;
5
+ import static org .hamcrest .CoreMatchers .is ;
5
6
import static org .hamcrest .CoreMatchers .startsWith ;
6
- import static org .hamcrest .core .Is .is ;
7
7
import static org .junit .Assert .assertThat ;
8
8
import static org .mockito .Matchers .any ;
9
9
import static org .mockito .Mockito .when ;
@@ -110,20 +110,23 @@ public void should_throw_UsernameNotFoundException_when_clientService_throws_Inv
110
110
}
111
111
112
112
@ Test
113
- public void should_throw_AuthenticationServiceException_for_PlainJWT_when_AuthMethod_is_different_than_NONE () {
113
+ public void should_throw_AuthenticationServiceException_for_PlainJWT () {
114
114
mockPlainJWTAuthAttempt ();
115
- List <AuthMethod > unsupportedAuthMethods = Arrays .asList (
116
- null , AuthMethod .PRIVATE_KEY , AuthMethod .PRIVATE_KEY , AuthMethod .SECRET_BASIC , AuthMethod .SECRET_JWT , AuthMethod .SECRET_POST
117
- );
118
115
119
- for (AuthMethod authMethod : unsupportedAuthMethods ) {
120
- when (client .getTokenEndpointAuthMethod ()).thenReturn (authMethod );
116
+ Throwable thrown = authenticateAndReturnThrownException ();
121
117
122
- Throwable thrown = authenticateAndReturnThrownException ();
118
+ assertThat (thrown , instanceOf (AuthenticationServiceException .class ));
119
+ assertThat (thrown .getMessage (), is ("Unsupported JWT type: " + PlainJWT .class .getName ()));
120
+ }
123
121
124
- assertThat (thrown , instanceOf (AuthenticationServiceException .class ));
125
- assertThat (thrown .getMessage (), is ("Client does not support this authentication method." ));
126
- }
122
+ @ Test
123
+ public void should_throw_AuthenticationServiceException_for_EncryptedJWT () {
124
+ mockEncryptedJWTAuthAttempt ();
125
+
126
+ Throwable thrown = authenticateAndReturnThrownException ();
127
+
128
+ assertThat (thrown , instanceOf (AuthenticationServiceException .class ));
129
+ assertThat (thrown .getMessage (), is ("Unsupported JWT type: " + EncryptedJWT .class .getName ()));
127
130
}
128
131
129
132
@ Test
@@ -228,21 +231,10 @@ public void should_throw_AuthenticationServiceException_for_SignedJWT_when_inval
228
231
assertThat (thrown .getMessage (), is ("Signature did not validate for presented JWT authentication." ));
229
232
}
230
233
231
- @ Test
232
- public void should_throw_AuthenticationServiceException_for_EncryptedJWT () {
233
- EncryptedJWT encryptedJWT = createEncryptedJWT ();
234
- when (token .getJwt ()).thenReturn (encryptedJWT );
235
-
236
- Throwable thrown = authenticateAndReturnThrownException ();
237
-
238
- assertThat (thrown , instanceOf (AuthenticationServiceException .class ));
239
- assertThat (thrown .getMessage (), is ("Unsupported JWT type: " + EncryptedJWT .class .getName ()));
240
- }
241
-
242
234
@ Test
243
235
public void should_throw_AuthenticationServiceException_when_null_issuer () {
244
236
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ().issuer (null ).build ();
245
- mockPlainJWTAuthAttempt (jwtClaimsSet );
237
+ mockSignedJWTAuthAttempt (jwtClaimsSet );
246
238
247
239
Throwable thrown = authenticateAndReturnThrownException ();
248
240
@@ -253,7 +245,7 @@ public void should_throw_AuthenticationServiceException_when_null_issuer() {
253
245
@ Test
254
246
public void should_throw_AuthenticationServiceException_when_not_matching_issuer () {
255
247
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ().issuer ("not matching" ).build ();
256
- mockPlainJWTAuthAttempt (jwtClaimsSet );
248
+ mockSignedJWTAuthAttempt (jwtClaimsSet );
257
249
258
250
Throwable thrown = authenticateAndReturnThrownException ();
259
251
@@ -264,7 +256,7 @@ public void should_throw_AuthenticationServiceException_when_not_matching_issuer
264
256
@ Test
265
257
public void should_throw_AuthenticationServiceException_when_null_expiration_time () {
266
258
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ().issuer (CLIENT_ID ).expirationTime (null ).build ();
267
- mockPlainJWTAuthAttempt (jwtClaimsSet );
259
+ mockSignedJWTAuthAttempt (jwtClaimsSet );
268
260
269
261
Throwable thrown = authenticateAndReturnThrownException ();
270
262
@@ -276,7 +268,7 @@ public void should_throw_AuthenticationServiceException_when_null_expiration_tim
276
268
public void should_throw_AuthenticationServiceException_when_expired_jwt () {
277
269
Date expiredDate = new Date (System .currentTimeMillis () - TimeUnit .SECONDS .toMillis (500 ));
278
270
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ().issuer (CLIENT_ID ).expirationTime (expiredDate ).build ();
279
- mockPlainJWTAuthAttempt (jwtClaimsSet );
271
+ mockSignedJWTAuthAttempt (jwtClaimsSet );
280
272
281
273
Throwable thrown = authenticateAndReturnThrownException ();
282
274
@@ -288,7 +280,7 @@ public void should_throw_AuthenticationServiceException_when_expired_jwt() {
288
280
public void should_throw_AuthenticationServiceException_when_jwt_valid_in_future () {
289
281
Date futureDate = new Date (System .currentTimeMillis () + TimeUnit .SECONDS .toMillis (500 ));
290
282
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ().issuer (CLIENT_ID ).expirationTime (futureDate ).notBeforeTime (futureDate ).build ();
291
- mockPlainJWTAuthAttempt (jwtClaimsSet );
283
+ mockSignedJWTAuthAttempt (jwtClaimsSet );
292
284
293
285
Throwable thrown = authenticateAndReturnThrownException ();
294
286
@@ -300,7 +292,7 @@ public void should_throw_AuthenticationServiceException_when_jwt_valid_in_future
300
292
public void should_throw_AuthenticationServiceException_when_jwt_issued_in_future () {
301
293
Date futureDate = new Date (System .currentTimeMillis () + TimeUnit .SECONDS .toMillis (500 ));
302
294
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ().issuer (CLIENT_ID ).expirationTime (futureDate ).issueTime (futureDate ).build ();
303
- mockPlainJWTAuthAttempt (jwtClaimsSet );
295
+ mockSignedJWTAuthAttempt (jwtClaimsSet );
304
296
305
297
Throwable thrown = authenticateAndReturnThrownException ();
306
298
@@ -311,7 +303,7 @@ public void should_throw_AuthenticationServiceException_when_jwt_issued_in_futur
311
303
@ Test
312
304
public void should_throw_AuthenticationServiceException_when_unmatching_audience () {
313
305
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ().issuer (CLIENT_ID ).expirationTime (new Date ()).audience ("invalid" ).build ();
314
- mockPlainJWTAuthAttempt (jwtClaimsSet );
306
+ mockSignedJWTAuthAttempt (jwtClaimsSet );
315
307
316
308
Throwable thrown = authenticateAndReturnThrownException ();
317
309
@@ -320,28 +312,7 @@ public void should_throw_AuthenticationServiceException_when_unmatching_audience
320
312
}
321
313
322
314
@ Test
323
- public void should_return_valid_token_for_PlainJWT_when_audience_contains_token_endpoint () {
324
- JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ()
325
- .issuer (CLIENT_ID )
326
- .subject (SUBJECT )
327
- .expirationTime (new Date ())
328
- .audience (ImmutableList .of ("http://issuer.com/token" , "invalid" ))
329
- .build ();
330
- PlainJWT jwt = mockPlainJWTAuthAttempt (jwtClaimsSet );
331
-
332
- Authentication authentication = jwtBearerAuthenticationProvider .authenticate (token );
333
-
334
- assertThat (authentication , instanceOf (JWTBearerAssertionAuthenticationToken .class ));
335
-
336
- JWTBearerAssertionAuthenticationToken token = (JWTBearerAssertionAuthenticationToken ) authentication ;
337
- assertThat (token .getName (), is (SUBJECT ));
338
- assertThat (token .getJwt (), is (jwt ));
339
- assertThat (token .getAuthorities (), hasItems (authority1 , authority2 , authority3 ));
340
- assertThat (token .getAuthorities ().size (), is (4 ));
341
- }
342
-
343
- @ Test
344
- public void should_return_valid_token_for_SignedJWT_when_audience_contains_token_endpoint () {
315
+ public void should_return_valid_token_when_audience_contains_token_endpoint () {
345
316
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ()
346
317
.issuer (CLIENT_ID )
347
318
.subject (SUBJECT )
@@ -362,29 +333,7 @@ public void should_return_valid_token_for_SignedJWT_when_audience_contains_token
362
333
}
363
334
364
335
@ Test
365
- public void should_return_valid_token_for_PlainJWT_when_issuer_does_not_end_with_slash_and_audience_contains_token_endpoint () {
366
- JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ()
367
- .issuer (CLIENT_ID )
368
- .subject (SUBJECT )
369
- .expirationTime (new Date ())
370
- .audience (ImmutableList .of ("http://issuer.com/token" ))
371
- .build ();
372
- PlainJWT jwt = mockPlainJWTAuthAttempt (jwtClaimsSet );
373
- when (config .getIssuer ()).thenReturn ("http://issuer.com/" );
374
-
375
- Authentication authentication = jwtBearerAuthenticationProvider .authenticate (token );
376
-
377
- assertThat (authentication , instanceOf (JWTBearerAssertionAuthenticationToken .class ));
378
-
379
- JWTBearerAssertionAuthenticationToken token = (JWTBearerAssertionAuthenticationToken ) authentication ;
380
- assertThat (token .getName (), is (SUBJECT ));
381
- assertThat (token .getJwt (), is (jwt ));
382
- assertThat (token .getAuthorities (), hasItems (authority1 , authority2 , authority3 ));
383
- assertThat (token .getAuthorities ().size (), is (4 ));
384
- }
385
-
386
- @ Test
387
- public void should_return_valid_token_for_SignedJWT_when_issuer_does_not_end_with_slash_and_audience_contains_token_endpoint () {
336
+ public void should_return_valid_token_when_issuer_does_not_end_with_slash_and_audience_contains_token_endpoint () {
388
337
JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ()
389
338
.issuer (CLIENT_ID )
390
339
.subject (SUBJECT )
@@ -405,14 +354,15 @@ public void should_return_valid_token_for_SignedJWT_when_issuer_does_not_end_wit
405
354
assertThat (token .getAuthorities ().size (), is (4 ));
406
355
}
407
356
408
- private PlainJWT mockPlainJWTAuthAttempt () {
409
- return mockPlainJWTAuthAttempt (createJwtClaimsSet ());
357
+ private void mockPlainJWTAuthAttempt () {
358
+ PlainJWT plainJWT = new PlainJWT (createJwtClaimsSet ());
359
+ when (token .getJwt ()).thenReturn (plainJWT );
410
360
}
411
361
412
- private PlainJWT mockPlainJWTAuthAttempt ( JWTClaimsSet jwtClaimsSet ) {
413
- PlainJWT plainJWT = createPlainJWT ( jwtClaimsSet );
414
- when ( token . getJwt ()). thenReturn ( plainJWT );
415
- return plainJWT ;
362
+ private void mockEncryptedJWTAuthAttempt ( ) {
363
+ JWEHeader jweHeader = new JWEHeader . Builder ( JWEAlgorithm . A128GCMKW , EncryptionMethod . A256GCM ). build ( );
364
+ EncryptedJWT encryptedJWT = new EncryptedJWT ( jweHeader , createJwtClaimsSet () );
365
+ when ( token . getJwt ()). thenReturn ( encryptedJWT ) ;
416
366
}
417
367
418
368
private SignedJWT mockSignedJWTAuthAttempt () {
@@ -436,10 +386,6 @@ private Throwable authenticateAndReturnThrownException() {
436
386
throw new AssertionError ("No exception thrown when expected" );
437
387
}
438
388
439
- private PlainJWT createPlainJWT (JWTClaimsSet jwtClaimsSet ) {
440
- return new PlainJWT (jwtClaimsSet );
441
- }
442
-
443
389
private SignedJWT createSignedJWT () {
444
390
return createSignedJWT (JWSAlgorithm .RS256 );
445
391
}
@@ -457,11 +403,6 @@ private SignedJWT createSignedJWT(JWSAlgorithm jwsAlgorithm, JWTClaimsSet jwtCla
457
403
return new SignedJWT (jwsHeader , jwtClaimsSet );
458
404
}
459
405
460
- private EncryptedJWT createEncryptedJWT () {
461
- JWEHeader jweHeader = new JWEHeader .Builder (JWEAlgorithm .A128GCMKW , EncryptionMethod .A256GCM ).build ();
462
- return new EncryptedJWT (jweHeader , createJwtClaimsSet ());
463
- }
464
-
465
406
private JWTClaimsSet createJwtClaimsSet () {
466
407
return new JWTClaimsSet .Builder ()
467
408
.issuer (CLIENT_ID )
0 commit comments