Skip to content

Commit de188b5

Browse files
committed
skip signing signature if nonce is not provided
replaces behaviour that required a nonce to be issued by the server if the client is configured to use an authentication method
1 parent 527205c commit de188b5

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

Sources/Nats/NatsConnection.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class ConnectionHandler: ChannelInboundHandler {
476476
if self.auth?.nkey != nil && self.auth?.nkeyPath != nil {
477477
throw NatsError.ConnectError.invalidConfig("cannot use both nkey and nkeyPath")
478478
}
479-
if let auth = self.auth, let credentialsPath = auth.credentialsPath {
479+
if let auth = self.auth, let credentialsPath = auth.credentialsPath, let nonce = self.serverInfo?.nonce {
480480
let credentials = try await URLSession.shared.data(from: credentialsPath).0
481481
guard let jwt = JwtUtils.parseDecoratedJWT(contents: credentials) else {
482482
throw NatsError.ConnectError.invalidConfig(
@@ -486,17 +486,15 @@ class ConnectionHandler: ChannelInboundHandler {
486486
throw NatsError.ConnectError.invalidConfig(
487487
"failed to extract NKEY from credentials file")
488488
}
489-
guard let nonce = self.serverInfo?.nonce else {
490-
throw NatsError.ConnectError.invalidConfig("missing nonce")
491-
}
489+
initialConnect.userJwt = String(data: jwt, encoding: .utf8)!
490+
492491
let keypair = try KeyPair(seed: String(data: nkey, encoding: .utf8)!)
493492
let nonceData = nonce.data(using: .utf8)!
494493
let sig = try keypair.sign(input: nonceData)
495494
let base64sig = sig.base64EncodedURLSafeNotPadded()
496495
initialConnect.signature = base64sig
497-
initialConnect.userJwt = String(data: jwt, encoding: .utf8)!
498496
}
499-
if let nkey = self.auth?.nkeyPath {
497+
if let nkey = self.auth?.nkeyPath, let nonce = self.serverInfo?.nonce {
500498
let nkeyData = try await URLSession.shared.data(from: nkey).0
501499

502500
guard let nkeyContent = String(data: nkeyData, encoding: .utf8) else {
@@ -505,20 +503,14 @@ class ConnectionHandler: ChannelInboundHandler {
505503
let keypair = try KeyPair(
506504
seed: nkeyContent.trimmingCharacters(in: .whitespacesAndNewlines)
507505
)
508-
509-
guard let nonce = self.serverInfo?.nonce else {
510-
throw NatsError.ConnectError.invalidConfig("missing nonce")
511-
}
506+
initialConnect.nkey = keypair.publicKeyEncoded
507+
512508
let sig = try keypair.sign(input: nonce.data(using: .utf8)!)
513509
let base64sig = sig.base64EncodedURLSafeNotPadded()
514510
initialConnect.signature = base64sig
515-
initialConnect.nkey = keypair.publicKeyEncoded
516511
}
517-
if let nkey = self.auth?.nkey {
512+
if let nkey = self.auth?.nkey, let nonce = self.serverInfo?.nonce {
518513
let keypair = try KeyPair(seed: nkey)
519-
guard let nonce = self.serverInfo?.nonce else {
520-
throw NatsError.ConnectError.invalidConfig("missing nonce")
521-
}
522514
let nonceData = nonce.data(using: .utf8)!
523515
let sig = try keypair.sign(input: nonceData)
524516
let base64sig = sig.base64EncodedURLSafeNotPadded()

Tests/NatsTests/Integration/ConnectionTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class CoreNatsTests: XCTestCase {
3838
("testUsernameAndPassword", testUsernameAndPassword),
3939
("testTokenAuth", testTokenAuth),
4040
("testCredentialsAuth", testCredentialsAuth),
41+
("testCredentialsAuthWithoutNonce", testCredentialsAuthWithoutNonce),
4142
("testNkeyAuth", testNkeyAuth),
4243
("testNkeyAuthFile", testNkeyAuthFile),
4344
("testMutualTls", testMutualTls),
@@ -589,6 +590,22 @@ class CoreNatsTests: XCTestCase {
589590
try await client.publish("data".data(using: .utf8)!, subject: "foo")
590591
_ = try await subscribe.next()
591592
}
593+
594+
func testCredentialsAuthWithoutNonce() async throws {
595+
logger.logLevel = .critical
596+
let bundle = Bundle.module
597+
natsServer.start()
598+
599+
let creds = bundle.url(forResource: "TestUser", withExtension: "creds")!
600+
601+
let client = NatsClientOptions().url(URL(string: natsServer.clientURL)!).credentialsFile(
602+
creds
603+
).build()
604+
try await client.connect()
605+
let subscribe = try await client.subscribe(subject: "foo").makeAsyncIterator()
606+
try await client.publish("data".data(using: .utf8)!, subject: "foo")
607+
_ = try await subscribe.next()
608+
}
592609

593610
func testNkeyAuth() async throws {
594611
logger.logLevel = .critical

0 commit comments

Comments
 (0)