Skip to content

Commit d41d555

Browse files
Add quickfix to be able to use NKey + Seed auth handler and username + password combined.
1 parent 6f91621 commit d41d555

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/main/java/io/nats/client/Options.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,9 @@ public CharBuffer buildProtocolConnectOptionsString(String serverURI, boolean in
23712371
appendOption(connectString, Options.OPTION_NKEY, nkey, true, true);
23722372
appendOption(connectString, Options.OPTION_SIG, encodedSig, true, true);
23732373
appendOption(connectString, Options.OPTION_JWT, jwt, true, true);
2374-
} else if (includeAuth) {
2374+
}
2375+
2376+
if (includeAuth) {
23752377
String uriUser = null;
23762378
String uriPass = null;
23772379
String uriToken = null;

src/test/java/io/nats/client/AuthHandlerForTesting.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@
1818

1919
public class AuthHandlerForTesting implements AuthHandler {
2020
private final NKey nkey;
21+
private final char[] jwt;
2122

2223
public AuthHandlerForTesting(NKey nkey) {
2324
this.nkey = nkey;
25+
this.jwt = null;
26+
}
27+
28+
public AuthHandlerForTesting(NKey nkey, char[] jwt) {
29+
this.nkey = nkey;
30+
this.jwt = jwt;
2431
}
2532

2633
public AuthHandlerForTesting() throws Exception {
2734
this.nkey = NKey.createUser(null);
35+
this.jwt = null;
2836
}
2937

3038
public NKey getNKey() {
@@ -48,6 +56,6 @@ public byte[] sign(byte[] nonce) {
4856
}
4957

5058
public char[] getJWT() {
51-
return null;
59+
return this.jwt;
5260
}
5361
}

src/test/java/io/nats/client/OptionsTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,41 @@ public void testNKeyConnectOptions() throws Exception {
693693
assertEquals(expectedWithAuth, o.buildProtocolConnectOptionsString("nats://localhost:4222", true, nonce).toString(), "auth connect options");
694694
}
695695

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+
696731
@Test
697732
public void testDefaultDataPort() {
698733
Options o = new Options.Builder().socketWriteTimeout(null).build();

0 commit comments

Comments
 (0)