Skip to content

Commit 28f62b9

Browse files
committed
Add token auth
Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
1 parent 2f1e954 commit 28f62b9

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

Sources/NatsSwift/NatsClient/NatsClient.swift

+4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ public enum NatsState {
2222
public struct Auth {
2323
var user: String?
2424
var password: String?
25+
var token: String?
2526

2627
init(user: String, password: String) {
2728
self.user = user
2829
self.password = password
2930
}
31+
init(token: String) {
32+
self.token = token
33+
}
3034
}
3135

3236
public class Client {

Sources/NatsSwift/NatsClient/NatsClientOptions.swift

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public class ClientOptions {
5252
return self
5353
}
5454

55+
public func token(_ token: String) -> ClientOptions {
56+
if self.auth == nil {
57+
self.auth = Auth(token: token)
58+
} else {
59+
self.auth?.token = token
60+
}
61+
return self
62+
}
63+
5564
public func build() -> Client {
5665
let client = Client()
5766
client.urls = urls

Sources/NatsSwift/NatsConnection.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class ConnectionHandler: ChannelInboundHandler {
149149
}
150150
self.serverInfo = info
151151
// TODO(jrm): Add rest of auth here.
152-
let connect = ConnectInfo(verbose: false, pedantic: false, userJwt: nil, nkey: "", signature: nil, name: "", echo: true, lang: self.lang, version: self.version, natsProtocol: .dynamic, tlsRequired: false, user: self.auth?.user ?? "", pass: self.auth?.password ?? "", authToken: "", headers: true, noResponders: true)
152+
let connect = ConnectInfo(verbose: false, pedantic: false, userJwt: nil, nkey: "", signature: nil, name: "", echo: true, lang: self.lang, version: self.version, natsProtocol: .dynamic, tlsRequired: false, user: self.auth?.user ?? "", pass: self.auth?.password ?? "", authToken: self.auth?.token ?? "", headers: true, noResponders: true)
153153

154154
try await withCheckedThrowingContinuation { continuation in
155155
self.connectionEstablishedContinuation = continuation

Tests/NatsSwiftTests/Integration/ConnectionTests.swift

+38
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class CoreNatsTests: XCTestCase {
1717
("testConnect", testConnect),
1818
("testReconnect", testReconnect),
1919
("testUsernameAndPassword", testUsernameAndPassword),
20+
("testTokenAuth", testTokenAuth)
2021
]
2122
var natsServer = NatsServer()
2223

@@ -187,4 +188,41 @@ class CoreNatsTests: XCTestCase {
187188
}
188189

189190
}
191+
192+
func testTokenAuth() async throws {
193+
logger.logLevel = .debug
194+
let currentFile = URL(fileURLWithPath: #file)
195+
// Navigate up to the Tests directory
196+
let testsDir = currentFile.deletingLastPathComponent().deletingLastPathComponent()
197+
// Construct the path to the resource
198+
let resourceURL = testsDir
199+
.appendingPathComponent("Integration/Resources/token.conf", isDirectory: false)
200+
natsServer.start(cfg: resourceURL.path)
201+
let client = ClientOptions()
202+
.url(URL(string:natsServer.clientURL)!)
203+
.token("s3cr3t")
204+
.maxReconnects(5)
205+
.build()
206+
try await client.connect()
207+
try client.publish("msg".data(using: .utf8)!, subject: "test")
208+
try await client.flush()
209+
try await client.subscribe(to: "test")
210+
XCTAssertNotNil(client, "Client should not be nil")
211+
212+
213+
// Test if client with bad credentials throws an error
214+
let bad_creds_client = ClientOptions()
215+
.url(URL(string:natsServer.clientURL)!)
216+
.token("badtoken")
217+
.maxReconnects(5)
218+
.build()
219+
220+
do {
221+
try await bad_creds_client.connect()
222+
XCTFail("Should have thrown an error")
223+
} catch {
224+
XCTAssertNotNil(error, "Error should not be nil")
225+
}
226+
227+
}
190228
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
authorization {
2+
token: s3cr3t
3+
}
4+

0 commit comments

Comments
 (0)