@@ -693,6 +693,41 @@ public void testNKeyConnectOptions() throws Exception {
693
693
assertEquals (expectedWithAuth , o .buildProtocolConnectOptionsString ("nats://localhost:4222" , true , nonce ).toString (), "auth connect options" );
694
694
}
695
695
696
+ // Test for auth handler from nkey, option JWT and user info
697
+ @ Test
698
+ public void testNKeyJWTAndUserInfoOptions () throws Exception {
699
+ // "jwt" is encoded from:
700
+ // Header: {"alg":"HS256"}
701
+ // Payload: {"jti":"","iat":2000000000,"iss":"","name":"user_jwt","sub":"","nats":{"pub":{"deny":[">"]},
702
+ // "sub":{"deny":[">"]},"subs":-1,"data":-1,"payload":-1,"type":"user","version":2}}
703
+ String jwt = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIiLCJpYXQiOjIwMDAwMDAwMDAsImlzcyI6IiIsIm5hbWUiOiJ1c2VyX2p3"
704
+ + "dCIsInN1YiI6IiIsIm5hdHMiOnsicHViIjp7ImRlbnkiOlsiPiJdfSwic3ViIjp7ImRlbnkiOlsiPiJdfSwic3VicyI6LTEsImRh"
705
+ + "dGEiOi0xLCJwYXlsb2FkIjotMSwidHlwZSI6InVzZXIiLCJ2ZXJzaW9uIjoyfX0" ;
706
+ NKey nkey = NKey .createUser (null );
707
+ String username = "username" ;
708
+ String password = "password" ;
709
+ AuthHandlerForTesting th = new AuthHandlerForTesting (nkey , jwt .toCharArray ());
710
+ byte [] nonce = "abcdefg" .getBytes (StandardCharsets .UTF_8 );
711
+ String sig = Base64 .getUrlEncoder ().withoutPadding ().encodeToString (th .sign (nonce ));
712
+
713
+ Options options = new Options .Builder ().authHandler (th )
714
+ .userInfo (username .toCharArray (), password .toCharArray ()).build ();
715
+ String expectedWithoutAuth = "{\" lang\" :\" java\" ,\" version\" :\" " + Nats .CLIENT_VERSION + "\" "
716
+ + ",\" protocol\" :1,\" verbose\" :false,\" pedantic\" :false,\" tls_required\" :false,\" echo\" :true,"
717
+ + "\" headers\" :true,\" no_responders\" :true}" ;
718
+ String actualWithoutAuth = options
719
+ .buildProtocolConnectOptionsString ("nats://localhost:4222" , false , nonce ).toString ();
720
+ assertEquals (expectedWithoutAuth , actualWithoutAuth , "no auth connect options" );
721
+
722
+ String expectedWithAuth = "{\" lang\" :\" java\" ,\" version\" :\" " + Nats .CLIENT_VERSION + "\" "
723
+ + ",\" protocol\" :1,\" verbose\" :false,\" pedantic\" :false,\" tls_required\" :false,\" echo\" :true,"
724
+ + "\" headers\" :true,\" no_responders\" :true,\" nkey\" :\" " + new String (th .getID ()) + "\" ,\" sig\" :\" "
725
+ + sig + "\" ,\" jwt\" :\" " + jwt + "\" ,\" user\" :\" " + username + "\" ,\" pass\" :\" " + password + "\" }" ;
726
+ String actualWithAuth = options
727
+ .buildProtocolConnectOptionsString ("nats://localhost:4222" , true , nonce ).toString ();
728
+ assertEquals (expectedWithAuth , actualWithAuth , "auth connect options" );
729
+ }
730
+
696
731
@ Test
697
732
public void testDefaultDataPort () {
698
733
Options o = new Options .Builder ().socketWriteTimeout (null ).build ();
0 commit comments