@@ -664,136 +664,52 @@ private void kbaseTokenSuccess(
664
664
665
665
@ Test
666
666
public void getTokenWithMfaTrue () throws Exception {
667
- final UUID id = UUID .randomUUID ();
668
- final IncomingToken it = new IncomingToken ("mfatokenvalue" );
669
- final UserName userName = new UserName ("mfauser" );
670
-
671
- // Create simple local user
672
- manager .storage .createLocalUser (LocalUser .getLocalUserBuilder (
673
- userName , id , new DisplayName ("MFA User" ), inst (10000 ))
674
- .
withEmailAddress (
new EmailAddress (
"[email protected] " )).
build (),
675
- new PasswordHashAndSalt ("passwordhash1234" .getBytes (), "salt" .getBytes ()));
676
-
677
- // Create token with MFA=true
678
- manager .storage .storeToken (StoredToken .getBuilder (
679
- TokenType .AGENT , id , userName )
680
- .withLifeTime (Instant .ofEpochMilli (10000 ), Instant .ofEpochMilli (1000000000000000L ))
681
- .withTokenName (new TokenName ("mfatoken" ))
682
- .withMfaAuthenticated (true )
683
- .build (), it .getHashedToken ().getTokenHash ());
684
-
685
- final URI target = UriBuilder .fromUri (host ).path ("/api/V2/token" ).build ();
686
-
687
- final WebTarget wt = CLI .target (target );
688
- final Builder req = wt .request ()
689
- .header ("authorization" , it .getToken ());
690
-
691
- final Response res = req .get ();
692
-
693
- assertThat ("incorrect response code" , res .getStatus (), is (200 ));
694
-
695
- @ SuppressWarnings ("unchecked" )
696
- final Map <String , Object > response = res .readEntity (Map .class );
697
-
698
- assertThat ("incorrect MFA status" , response .get ("mfaAuthenticated" ), is (true ));
699
- assertThat ("incorrect user" , response .get ("user" ), is ("mfauser" ));
700
- assertThat ("incorrect token name" , response .get ("name" ), is ("mfatoken" ));
667
+ testTokenEndpointReturnsMfaAuthenticatedField (
"mfauser" ,
"MFA User" ,
"[email protected] " ,
668
+ "mfatoken" , "mfatokenvalue" , true );
701
669
}
702
670
703
671
@ Test
704
672
public void getTokenWithMfaFalse () throws Exception {
705
- final UUID id = UUID .randomUUID ();
706
- final IncomingToken it = new IncomingToken ("nomfatokenvalue" );
707
- final UserName userName = new UserName ("nomfauser" );
708
-
709
- // Create simple local user
710
- manager .storage .createLocalUser (LocalUser .getLocalUserBuilder (
711
- userName , id , new DisplayName ("No MFA User" ), inst (10000 ))
712
- .
withEmailAddress (
new EmailAddress (
"[email protected] " )).
build (),
713
- new PasswordHashAndSalt ("passwordhash1234" .getBytes (), "salt" .getBytes ()));
714
-
715
- // Create token with MFA=false
716
- manager .storage .storeToken (StoredToken .getBuilder (
717
- TokenType .AGENT , id , userName )
718
- .withLifeTime (Instant .ofEpochMilli (10000 ), Instant .ofEpochMilli (1000000000000000L ))
719
- .withTokenName (new TokenName ("nomfatoken" ))
720
- .withMfaAuthenticated (false )
721
- .build (), it .getHashedToken ().getTokenHash ());
722
-
723
- final URI target = UriBuilder .fromUri (host ).path ("/api/V2/token" ).build ();
724
-
725
- final WebTarget wt = CLI .target (target );
726
- final Builder req = wt .request ()
727
- .header ("authorization" , it .getToken ());
728
-
729
- final Response res = req .get ();
730
-
731
- assertThat ("incorrect response code" , res .getStatus (), is (200 ));
732
-
733
- @ SuppressWarnings ("unchecked" )
734
- final Map <String , Object > response = res .readEntity (Map .class );
735
-
736
- assertThat ("incorrect MFA status" , response .get ("mfaAuthenticated" ), is (false ));
737
- assertThat ("incorrect user" , response .get ("user" ), is ("nomfauser" ));
738
- assertThat ("incorrect token name" , response .get ("name" ), is ("nomfatoken" ));
673
+ testTokenEndpointReturnsMfaAuthenticatedField (
"nomfauser" ,
"No MFA User" ,
"[email protected] " ,
674
+ "nomfatoken" , "nomfatokenvalue" , false );
739
675
}
740
676
741
677
@ Test
742
678
public void getTokenWithMfaNull () throws Exception {
743
- final UUID id = UUID .randomUUID ();
744
- final IncomingToken it = new IncomingToken ("unknownmfatokenvalue" );
745
- final UserName userName = new UserName ("unknownmfauser" );
746
-
747
- // Create simple local user
748
- manager .storage .createLocalUser (LocalUser .getLocalUserBuilder (
749
- userName , id , new DisplayName ("Unknown MFA User" ), inst (10000 ))
750
- .
withEmailAddress (
new EmailAddress (
"[email protected] " )).
build (),
751
- new PasswordHashAndSalt ("passwordhash1234" .getBytes (), "salt" .getBytes ()));
752
-
753
- // Create token with MFA=null (unknown)
754
- manager .storage .storeToken (StoredToken .getBuilder (
755
- TokenType .AGENT , id , userName )
756
- .withLifeTime (Instant .ofEpochMilli (10000 ), Instant .ofEpochMilli (1000000000000000L ))
757
- .withTokenName (new TokenName ("unknownmfatoken" ))
758
- .withMfaAuthenticated (null )
759
- .build (), it .getHashedToken ().getTokenHash ());
760
-
761
- final URI target = UriBuilder .fromUri (host ).path ("/api/V2/token" ).build ();
762
-
763
- final WebTarget wt = CLI .target (target );
764
- final Builder req = wt .request ()
765
- .header ("authorization" , it .getToken ());
766
-
767
- final Response res = req .get ();
768
-
769
- assertThat ("incorrect response code" , res .getStatus (), is (200 ));
770
-
771
- @ SuppressWarnings ("unchecked" )
772
- final Map <String , Object > response = res .readEntity (Map .class );
773
-
774
- assertThat ("incorrect MFA status" , response .get ("mfaAuthenticated" ), is ((Object ) null ));
775
- assertThat ("incorrect user" , response .get ("user" ), is ("unknownmfauser" ));
776
- assertThat ("incorrect token name" , response .get ("name" ), is ("unknownmfatoken" ));
679
+ testTokenEndpointReturnsMfaAuthenticatedField (
"unknownmfauser" ,
"Unknown MFA User" ,
"[email protected] " ,
680
+ "unknownmfatoken" , "unknownmfatokenvalue" , null );
777
681
}
778
682
779
683
@ Test
780
684
public void getTokenWithNoMfaSet () throws Exception {
685
+ testTokenEndpointReturnsMfaAuthenticatedField (
"nomfasetuser" ,
"No MFA Set User" ,
"[email protected] " ,
686
+ "nomfasettoken" , "nomfasettokenvalue" , null );
687
+ }
688
+
689
+ private void testTokenEndpointReturnsMfaAuthenticatedField (final String userName , final String displayName ,
690
+ final String email , final String tokenName , final String tokenValue ,
691
+ final Boolean mfaStatus ) throws Exception {
781
692
final UUID id = UUID .randomUUID ();
782
- final IncomingToken it = new IncomingToken ("nomfasettokenvalue" );
783
- final UserName userName = new UserName ("nomfasetuser" );
693
+ final IncomingToken it = new IncomingToken (tokenValue );
694
+ final UserName user = new UserName (userName );
784
695
785
696
// Create simple local user
786
697
manager .storage .createLocalUser (LocalUser .getLocalUserBuilder (
787
- userName , id , new DisplayName ("No MFA Set User" ), inst (10000 ))
788
- .
withEmailAddress (
new EmailAddress (
"[email protected] " )).
build (),
698
+ user , id , new DisplayName (displayName ), inst (10000 ))
699
+ .withEmailAddress (new EmailAddress (email )).build (),
789
700
new PasswordHashAndSalt ("passwordhash1234" .getBytes (), "salt" .getBytes ()));
790
701
791
- // Create token without explicitly setting MFA (should default to null)
792
- manager . storage . storeToken ( StoredToken .getBuilder (
793
- TokenType .AGENT , id , userName )
702
+ // Create token with specified MFA status
703
+ final StoredToken . Builder tokenBuilder = StoredToken .getBuilder (
704
+ TokenType .AGENT , id , user )
794
705
.withLifeTime (Instant .ofEpochMilli (10000 ), Instant .ofEpochMilli (1000000000000000L ))
795
- .withTokenName (new TokenName ("nomfasettoken" ))
796
- .build (), it .getHashedToken ().getTokenHash ());
706
+ .withTokenName (new TokenName (tokenName ));
707
+
708
+ if (mfaStatus != null ) {
709
+ tokenBuilder .withMfaAuthenticated (mfaStatus );
710
+ }
711
+
712
+ manager .storage .storeToken (tokenBuilder .build (), it .getHashedToken ().getTokenHash ());
797
713
798
714
final URI target = UriBuilder .fromUri (host ).path ("/api/V2/token" ).build ();
799
715
@@ -808,9 +724,8 @@ userName, id, new DisplayName("No MFA Set User"), inst(10000))
808
724
@ SuppressWarnings ("unchecked" )
809
725
final Map <String , Object > response = res .readEntity (Map .class );
810
726
811
- // Should return null when MFA not explicitly set
812
- assertThat ("incorrect MFA status" , response .get ("mfaAuthenticated" ), is ((Object ) null ));
813
- assertThat ("incorrect user" , response .get ("user" ), is ("nomfasetuser" ));
814
- assertThat ("incorrect token name" , response .get ("name" ), is ("nomfasettoken" ));
727
+ assertThat ("incorrect MFA status" , response .get ("mfaAuthenticated" ), is ((Object ) mfaStatus ));
728
+ assertThat ("incorrect user" , response .get ("user" ), is (userName ));
729
+ assertThat ("incorrect token name" , response .get ("name" ), is (tokenName ));
815
730
}
816
731
}
0 commit comments