57
57
import org .elasticsearch .xpack .core .XPackField ;
58
58
import org .elasticsearch .xpack .core .XPackSettings ;
59
59
import org .elasticsearch .xpack .core .security .authc .Authentication ;
60
+ import org .elasticsearch .xpack .core .security .authc .Authentication .AuthenticationType ;
60
61
import org .elasticsearch .xpack .core .security .authc .Authentication .RealmRef ;
61
62
import org .elasticsearch .xpack .core .security .authc .AuthenticationField ;
62
63
import org .elasticsearch .xpack .core .security .authc .AuthenticationResult ;
@@ -270,6 +271,7 @@ public void testAuthenticateBothSupportSecondSucceeds() throws Exception {
270
271
assertThat (result .getUser (), is (user ));
271
272
assertThat (result .getLookedUpBy (), is (nullValue ()));
272
273
assertThat (result .getAuthenticatedBy (), is (notNullValue ())); // TODO implement equals
274
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
273
275
assertThreadContextContainsAuthentication (result );
274
276
setCompletedToTrue (completed );
275
277
}, this ::logAndFail ));
@@ -289,6 +291,7 @@ public void testAuthenticateFirstNotSupportingSecondSucceeds() throws Exception
289
291
service .authenticate ("_action" , message , (User )null , ActionListener .wrap (result -> {
290
292
assertThat (result , notNullValue ());
291
293
assertThat (result .getUser (), is (user ));
294
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
292
295
assertThreadContextContainsAuthentication (result );
293
296
setCompletedToTrue (completed );
294
297
}, this ::logAndFail ));
@@ -306,6 +309,7 @@ public void testAuthenticateCached() throws Exception {
306
309
307
310
assertThat (result , notNullValue ());
308
311
assertThat (result , is (authentication ));
312
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
309
313
verifyZeroInteractions (auditTrail );
310
314
verifyZeroInteractions (firstRealm );
311
315
verifyZeroInteractions (secondRealm );
@@ -342,6 +346,7 @@ public void authenticationInContextAndHeader() throws Exception {
342
346
343
347
assertThat (result , notNullValue ());
344
348
assertThat (result .getUser (), is (user ));
349
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
345
350
346
351
String userStr = threadContext .getHeader (AuthenticationField .AUTHENTICATION_KEY );
347
352
assertThat (userStr , notNullValue ());
@@ -387,6 +392,7 @@ public void testAuthenticateTransportFallback() throws Exception {
387
392
Authentication result = authenticateBlocking ("_action" , message , user1 );
388
393
assertThat (result , notNullValue ());
389
394
assertThat (result .getUser (), sameInstance (user1 ));
395
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .INTERNAL ));
390
396
assertThreadContextContainsAuthentication (result );
391
397
}
392
398
@@ -432,6 +438,7 @@ public void testAuthenticateTransportSuccess() throws Exception {
432
438
assertThat (result , notNullValue ());
433
439
assertThat (result .getUser (), sameInstance (user ));
434
440
assertThreadContextContainsAuthentication (result );
441
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
435
442
setCompletedToTrue (completed );
436
443
}, this ::logAndFail ));
437
444
@@ -450,6 +457,7 @@ public void testAuthenticateRestSuccess() throws Exception {
450
457
service .authenticate (restRequest , ActionListener .wrap (authentication -> {
451
458
assertThat (authentication , notNullValue ());
452
459
assertThat (authentication .getUser (), sameInstance (user1 ));
460
+ assertThat (authentication .getAuthenticationType (), is (AuthenticationType .REALM ));
453
461
assertThreadContextContainsAuthentication (authentication );
454
462
setCompletedToTrue (completed );
455
463
}, this ::logAndFail ));
@@ -459,7 +467,7 @@ public void testAuthenticateRestSuccess() throws Exception {
459
467
assertTrue (completed .get ());
460
468
}
461
469
462
- public void testAutheticateTransportContextAndHeader () throws Exception {
470
+ public void testAuthenticateTransportContextAndHeader () throws Exception {
463
471
User user1 = new User ("username" , "r1" , "r2" );
464
472
when (firstRealm .token (threadContext )).thenReturn (token );
465
473
when (firstRealm .supports (token )).thenReturn (true );
@@ -469,9 +477,9 @@ public void testAutheticateTransportContextAndHeader() throws Exception {
469
477
final SetOnce <String > authHeaderRef = new SetOnce <>();
470
478
try (ThreadContext .StoredContext ignore = threadContext .stashContext ()) {
471
479
service .authenticate ("_action" , message , SystemUser .INSTANCE , ActionListener .wrap (authentication -> {
472
-
473
480
assertThat (authentication , notNullValue ());
474
481
assertThat (authentication .getUser (), sameInstance (user1 ));
482
+ assertThat (authentication .getAuthenticationType (), is (AuthenticationType .REALM ));
475
483
assertThreadContextContainsAuthentication (authentication );
476
484
authRef .set (authentication );
477
485
authHeaderRef .set (threadContext .getHeader (AuthenticationField .AUTHENTICATION_KEY ));
@@ -530,6 +538,7 @@ public void testAutheticateTransportContextAndHeader() throws Exception {
530
538
service .authenticate ("_action" , new InternalMessage (), SystemUser .INSTANCE , ActionListener .wrap (result -> {
531
539
assertThat (result , notNullValue ());
532
540
assertThat (result .getUser (), equalTo (user1 ));
541
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
533
542
setCompletedToTrue (completed );
534
543
}, this ::logAndFail ));
535
544
assertTrue (completed .get ());
@@ -570,6 +579,7 @@ public void testAnonymousUserRest() throws Exception {
570
579
571
580
assertThat (result , notNullValue ());
572
581
assertThat (result .getUser (), sameInstance ((Object ) anonymousUser ));
582
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .ANONYMOUS ));
573
583
assertThreadContextContainsAuthentication (result );
574
584
String reqId = expectAuditRequestId ();
575
585
verify (auditTrail ).authenticationSuccess (reqId , "__anonymous" , new AnonymousUser (settings ), request );
@@ -588,6 +598,7 @@ public void testAnonymousUserTransportNoDefaultUser() throws Exception {
588
598
Authentication result = authenticateBlocking ("_action" , message , null );
589
599
assertThat (result , notNullValue ());
590
600
assertThat (result .getUser (), sameInstance (anonymousUser ));
601
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .ANONYMOUS ));
591
602
assertThreadContextContainsAuthentication (result );
592
603
}
593
604
@@ -604,6 +615,7 @@ public void testAnonymousUserTransportWithDefaultUser() throws Exception {
604
615
Authentication result = authenticateBlocking ("_action" , message , SystemUser .INSTANCE );
605
616
assertThat (result , notNullValue ());
606
617
assertThat (result .getUser (), sameInstance (SystemUser .INSTANCE ));
618
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .INTERNAL ));
607
619
assertThreadContextContainsAuthentication (result );
608
620
}
609
621
@@ -790,6 +802,7 @@ public void testRunAsLookupSameRealm() throws Exception {
790
802
final AtomicBoolean completed = new AtomicBoolean (false );
791
803
ActionListener <Authentication > listener = ActionListener .wrap (result -> {
792
804
assertThat (result , notNullValue ());
805
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
793
806
User authenticated = result .getUser ();
794
807
795
808
assertThat (authenticated .principal (), is ("looked up user" ));
@@ -835,6 +848,7 @@ public void testRunAsLookupDifferentRealm() throws Exception {
835
848
final AtomicBoolean completed = new AtomicBoolean (false );
836
849
ActionListener <Authentication > listener = ActionListener .wrap (result -> {
837
850
assertThat (result , notNullValue ());
851
+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
838
852
User authenticated = result .getUser ();
839
853
840
854
assertThat (SystemUser .is (authenticated ), is (false ));
@@ -958,7 +972,7 @@ public void testAuthenticateWithToken() throws Exception {
958
972
assertThat (result .getUser (), is (user ));
959
973
assertThat (result .getLookedUpBy (), is (nullValue ()));
960
974
assertThat (result .getAuthenticatedBy (), is (notNullValue ()));
961
- assertEquals ( expected , result );
975
+ assertThat ( result . getAuthenticationType (), is ( AuthenticationType . TOKEN ) );
962
976
setCompletedToTrue (completed );
963
977
}, this ::logAndFail ));
964
978
}
@@ -1115,6 +1129,7 @@ public void testApiKeyAuth() {
1115
1129
threadContext .putHeader ("Authorization" , headerValue );
1116
1130
final Authentication authentication = authenticateBlocking ("_action" , message , null );
1117
1131
assertThat (authentication .getUser ().principal (), is ("johndoe" ));
1132
+ assertThat (authentication .getAuthenticationType (), is (AuthenticationType .API_KEY ));
1118
1133
}
1119
1134
}
1120
1135
0 commit comments