-
Notifications
You must be signed in to change notification settings - Fork 1
Implement token authentication #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+223
−17
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| const std = @import("std"); | ||
| const nats = @import("nats"); | ||
| const utils = @import("utils.zig"); | ||
|
|
||
| const log = std.log.default; | ||
|
|
||
| test "token authentication success" { | ||
| // Test against actual NATS server with token auth (port 14225) | ||
| const opts = nats.ConnectionOptions{ | ||
| .token = "test_token_123", | ||
| }; | ||
|
|
||
| const conn = try utils.createConnection(.token_auth, opts); | ||
| defer utils.closeConnection(conn); | ||
|
|
||
| // If we reach here, authentication succeeded | ||
| // Test basic publish/subscribe to verify connection works | ||
| try conn.publish("test.auth.success", "authenticated message"); | ||
| try conn.flush(); | ||
| } | ||
|
|
||
| test "token handler authentication" { | ||
| // Test token handler callback against real server | ||
| const TestTokenHandler = struct { | ||
| fn getToken() []const u8 { | ||
| return "test_token_123"; // Return valid token for auth server | ||
| } | ||
| }; | ||
|
|
||
| const opts = nats.ConnectionOptions{ | ||
| .token_handler = TestTokenHandler.getToken, | ||
| }; | ||
|
|
||
| const conn = try utils.createConnection(.token_auth, opts); | ||
| defer utils.closeConnection(conn); | ||
|
|
||
| // If we reach here, the token handler was called and authentication succeeded | ||
| try conn.publish("test.auth.handler", "handler authenticated"); | ||
| try conn.flush(); | ||
| } | ||
|
|
||
| test "token handler takes precedence over static token" { | ||
| // Test that dynamic token handler takes precedence over static token | ||
| const TestTokenHandler = struct { | ||
| fn getToken() []const u8 { | ||
| return "test_token_123"; // Valid token (handler wins) | ||
| } | ||
| }; | ||
|
|
||
| const opts = nats.ConnectionOptions{ | ||
| .token = "invalid_static_token", // Invalid static token | ||
| .token_handler = TestTokenHandler.getToken, | ||
| }; | ||
|
|
||
| // Should succeed because handler returns valid token | ||
| const conn = try utils.createConnection(.token_auth, opts); | ||
| defer utils.closeConnection(conn); | ||
|
|
||
| // Authentication succeeded, proving handler took precedence | ||
| try conn.publish("test.auth.precedence", "handler wins"); | ||
| try conn.flush(); | ||
| } | ||
|
|
||
| test "token authentication failure" { | ||
| // Test authentication failure with invalid token and short timeout | ||
| const opts = nats.ConnectionOptions{ | ||
| .token = "invalid_token", | ||
| .timeout_ms = 2000, // 2 second timeout | ||
| }; | ||
|
|
||
| // This should fail with AuthFailed error | ||
| const result = utils.createConnection(.token_auth, opts); | ||
|
|
||
| if (result) |conn| { | ||
| defer utils.closeConnection(conn); | ||
| // Should not reach here | ||
| std.log.err("Connection unexpectedly succeeded with invalid token", .{}); | ||
| try std.testing.expect(false); | ||
| } else |err| { | ||
| std.log.info("Got error: {}", .{err}); | ||
| // Now we get specific protocol errors | ||
| try std.testing.expect(err == nats.ProtocolError.AuthorizationViolation); | ||
| } | ||
| } | ||
|
|
||
| test "no authentication options against auth server" { | ||
| // Test connection without token to auth server (should fail) | ||
| const opts = nats.ConnectionOptions{}; | ||
|
|
||
| const result = utils.createConnection(.token_auth, opts); | ||
|
|
||
| if (result) |conn| { | ||
| defer utils.closeConnection(conn); | ||
| // Should not reach here | ||
| try std.testing.expect(false); | ||
| } else |err| { | ||
| try std.testing.expect(err == nats.ProtocolError.AuthorizationViolation); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Ensure we don’t emit "auth_token": null
If auth_token is null and stringify emits nulls, some servers reject the CONNECT. Set the option explicitly.
🌐 Web query:
💡 Result:
No — in Zig std 0.12.x the default for StringifyOptions.emit_null_optional_fields is true. [1][2]
Sources:
[1] std/json/stringify.zig (browseable) — shows
emit_null_optional_fields: bool = true.[2] std/lib source copy (Fossies) — documents
Options.emit_null_optional_fieldsand the behavior.Don't emit "auth_token": null — explicitly disable emitting null optional fields.
Zig std 0.12.x defaults emit_null_optional_fields = true, so stringify will emit nulls; set it to false.
📝 Committable suggestion
🤖 Prompt for AI Agents