5757import org .elasticsearch .xpack .core .XPackField ;
5858import org .elasticsearch .xpack .core .XPackSettings ;
5959import org .elasticsearch .xpack .core .security .authc .Authentication ;
60+ import org .elasticsearch .xpack .core .security .authc .Authentication .AuthenticationType ;
6061import org .elasticsearch .xpack .core .security .authc .Authentication .RealmRef ;
6162import org .elasticsearch .xpack .core .security .authc .AuthenticationField ;
6263import org .elasticsearch .xpack .core .security .authc .AuthenticationResult ;
@@ -270,6 +271,7 @@ public void testAuthenticateBothSupportSecondSucceeds() throws Exception {
270271 assertThat (result .getUser (), is (user ));
271272 assertThat (result .getLookedUpBy (), is (nullValue ()));
272273 assertThat (result .getAuthenticatedBy (), is (notNullValue ())); // TODO implement equals
274+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
273275 assertThreadContextContainsAuthentication (result );
274276 setCompletedToTrue (completed );
275277 }, this ::logAndFail ));
@@ -289,6 +291,7 @@ public void testAuthenticateFirstNotSupportingSecondSucceeds() throws Exception
289291 service .authenticate ("_action" , message , (User )null , ActionListener .wrap (result -> {
290292 assertThat (result , notNullValue ());
291293 assertThat (result .getUser (), is (user ));
294+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
292295 assertThreadContextContainsAuthentication (result );
293296 setCompletedToTrue (completed );
294297 }, this ::logAndFail ));
@@ -306,6 +309,7 @@ public void testAuthenticateCached() throws Exception {
306309
307310 assertThat (result , notNullValue ());
308311 assertThat (result , is (authentication ));
312+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
309313 verifyZeroInteractions (auditTrail );
310314 verifyZeroInteractions (firstRealm );
311315 verifyZeroInteractions (secondRealm );
@@ -342,6 +346,7 @@ public void authenticationInContextAndHeader() throws Exception {
342346
343347 assertThat (result , notNullValue ());
344348 assertThat (result .getUser (), is (user ));
349+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
345350
346351 String userStr = threadContext .getHeader (AuthenticationField .AUTHENTICATION_KEY );
347352 assertThat (userStr , notNullValue ());
@@ -387,6 +392,7 @@ public void testAuthenticateTransportFallback() throws Exception {
387392 Authentication result = authenticateBlocking ("_action" , message , user1 );
388393 assertThat (result , notNullValue ());
389394 assertThat (result .getUser (), sameInstance (user1 ));
395+ assertThat (result .getAuthenticationType (), is (AuthenticationType .INTERNAL ));
390396 assertThreadContextContainsAuthentication (result );
391397 }
392398
@@ -432,6 +438,7 @@ public void testAuthenticateTransportSuccess() throws Exception {
432438 assertThat (result , notNullValue ());
433439 assertThat (result .getUser (), sameInstance (user ));
434440 assertThreadContextContainsAuthentication (result );
441+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
435442 setCompletedToTrue (completed );
436443 }, this ::logAndFail ));
437444
@@ -450,6 +457,7 @@ public void testAuthenticateRestSuccess() throws Exception {
450457 service .authenticate (restRequest , ActionListener .wrap (authentication -> {
451458 assertThat (authentication , notNullValue ());
452459 assertThat (authentication .getUser (), sameInstance (user1 ));
460+ assertThat (authentication .getAuthenticationType (), is (AuthenticationType .REALM ));
453461 assertThreadContextContainsAuthentication (authentication );
454462 setCompletedToTrue (completed );
455463 }, this ::logAndFail ));
@@ -459,7 +467,7 @@ public void testAuthenticateRestSuccess() throws Exception {
459467 assertTrue (completed .get ());
460468 }
461469
462- public void testAutheticateTransportContextAndHeader () throws Exception {
470+ public void testAuthenticateTransportContextAndHeader () throws Exception {
463471 User user1 = new User ("username" , "r1" , "r2" );
464472 when (firstRealm .token (threadContext )).thenReturn (token );
465473 when (firstRealm .supports (token )).thenReturn (true );
@@ -469,9 +477,9 @@ public void testAutheticateTransportContextAndHeader() throws Exception {
469477 final SetOnce <String > authHeaderRef = new SetOnce <>();
470478 try (ThreadContext .StoredContext ignore = threadContext .stashContext ()) {
471479 service .authenticate ("_action" , message , SystemUser .INSTANCE , ActionListener .wrap (authentication -> {
472-
473480 assertThat (authentication , notNullValue ());
474481 assertThat (authentication .getUser (), sameInstance (user1 ));
482+ assertThat (authentication .getAuthenticationType (), is (AuthenticationType .REALM ));
475483 assertThreadContextContainsAuthentication (authentication );
476484 authRef .set (authentication );
477485 authHeaderRef .set (threadContext .getHeader (AuthenticationField .AUTHENTICATION_KEY ));
@@ -530,6 +538,7 @@ public void testAutheticateTransportContextAndHeader() throws Exception {
530538 service .authenticate ("_action" , new InternalMessage (), SystemUser .INSTANCE , ActionListener .wrap (result -> {
531539 assertThat (result , notNullValue ());
532540 assertThat (result .getUser (), equalTo (user1 ));
541+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
533542 setCompletedToTrue (completed );
534543 }, this ::logAndFail ));
535544 assertTrue (completed .get ());
@@ -570,6 +579,7 @@ public void testAnonymousUserRest() throws Exception {
570579
571580 assertThat (result , notNullValue ());
572581 assertThat (result .getUser (), sameInstance ((Object ) anonymousUser ));
582+ assertThat (result .getAuthenticationType (), is (AuthenticationType .ANONYMOUS ));
573583 assertThreadContextContainsAuthentication (result );
574584 String reqId = expectAuditRequestId ();
575585 verify (auditTrail ).authenticationSuccess (reqId , "__anonymous" , new AnonymousUser (settings ), request );
@@ -588,6 +598,7 @@ public void testAnonymousUserTransportNoDefaultUser() throws Exception {
588598 Authentication result = authenticateBlocking ("_action" , message , null );
589599 assertThat (result , notNullValue ());
590600 assertThat (result .getUser (), sameInstance (anonymousUser ));
601+ assertThat (result .getAuthenticationType (), is (AuthenticationType .ANONYMOUS ));
591602 assertThreadContextContainsAuthentication (result );
592603 }
593604
@@ -604,6 +615,7 @@ public void testAnonymousUserTransportWithDefaultUser() throws Exception {
604615 Authentication result = authenticateBlocking ("_action" , message , SystemUser .INSTANCE );
605616 assertThat (result , notNullValue ());
606617 assertThat (result .getUser (), sameInstance (SystemUser .INSTANCE ));
618+ assertThat (result .getAuthenticationType (), is (AuthenticationType .INTERNAL ));
607619 assertThreadContextContainsAuthentication (result );
608620 }
609621
@@ -790,6 +802,7 @@ public void testRunAsLookupSameRealm() throws Exception {
790802 final AtomicBoolean completed = new AtomicBoolean (false );
791803 ActionListener <Authentication > listener = ActionListener .wrap (result -> {
792804 assertThat (result , notNullValue ());
805+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
793806 User authenticated = result .getUser ();
794807
795808 assertThat (authenticated .principal (), is ("looked up user" ));
@@ -835,6 +848,7 @@ public void testRunAsLookupDifferentRealm() throws Exception {
835848 final AtomicBoolean completed = new AtomicBoolean (false );
836849 ActionListener <Authentication > listener = ActionListener .wrap (result -> {
837850 assertThat (result , notNullValue ());
851+ assertThat (result .getAuthenticationType (), is (AuthenticationType .REALM ));
838852 User authenticated = result .getUser ();
839853
840854 assertThat (SystemUser .is (authenticated ), is (false ));
@@ -958,7 +972,7 @@ public void testAuthenticateWithToken() throws Exception {
958972 assertThat (result .getUser (), is (user ));
959973 assertThat (result .getLookedUpBy (), is (nullValue ()));
960974 assertThat (result .getAuthenticatedBy (), is (notNullValue ()));
961- assertEquals ( expected , result );
975+ assertThat ( result . getAuthenticationType (), is ( AuthenticationType . TOKEN ) );
962976 setCompletedToTrue (completed );
963977 }, this ::logAndFail ));
964978 }
@@ -1115,6 +1129,7 @@ public void testApiKeyAuth() {
11151129 threadContext .putHeader ("Authorization" , headerValue );
11161130 final Authentication authentication = authenticateBlocking ("_action" , message , null );
11171131 assertThat (authentication .getUser ().principal (), is ("johndoe" ));
1132+ assertThat (authentication .getAuthenticationType (), is (AuthenticationType .API_KEY ));
11181133 }
11191134 }
11201135
0 commit comments