aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fixtures/test.creds14
-rw-r--r--src/nkeys.zig14
2 files changed, 25 insertions, 3 deletions
diff --git a/fixtures/test.creds b/fixtures/test.creds
new file mode 100644
index 0000000..4212a39
--- /dev/null
+++ b/fixtures/test.creds
@@ -0,0 +1,14 @@
1-----BEGIN NATS USER JWT-----
2eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJqdGkiOiJUWEg1TUxDNTdPTUJUQURYNUJNU0RLWkhSQUtXUFM0TkdHRFFPVlJXRzUyRFdaUlFFVERBIiwiaWF0IjoxNjIxNTgyOTU1LCJpc3MiOiJBQ1ZUQVZMQlFKTklQRjdNWFZWSlpZUFhaTkdFQUZMWVpTUjJSNVRZNk9ESjNSTTRYV0FDNUVFRiIsIm5hbWUiOiJ0ZXN0Iiwic3ViIjoiVUJHSlhLRkVWUlFEM05LM0lDRVc1Q0lDSzM1NkdESVZORkhaRUU0SzdMMkRYWTdORVNQVlFVNEwiLCJuYXRzIjp7InB1YiI6e30sInN1YiI6e30sInN1YnMiOi0xLCJkYXRhIjotMSwicGF5bG9hZCI6LTEsInR5cGUiOiJ1c2VyIiwidmVyc2lvbiI6Mn19.OhPLDZflyJ_keg2xBRDHZZhG5x_Qf_Yb61k9eHLs9zLRf0_ETwMd0PNZI_isuBhXYevobXHVoYA3oxvMVGlDCQ
3------END NATS USER JWT------
4
5************************* IMPORTANT *************************
6NKEY Seed printed below can be used to sign and prove identity.
7NKEYs are sensitive and should be treated as secrets.
8
9-----BEGIN USER NKEY SEED-----
10SUAGIEYODKBBTUMOB666Z5KA4FCWAZV7HWSGRHOD7MK6UM5IYLWLACH7DQ
11------END USER NKEY SEED------
12
13*************************************************************
14
diff --git a/src/nkeys.zig b/src/nkeys.zig
index 7ce44f9..23594a0 100644
--- a/src/nkeys.zig
+++ b/src/nkeys.zig
@@ -388,7 +388,7 @@ pub fn fromRawSeed(prefix: PublicPrefixByte, raw_seed: *const [Ed25519.seed_leng
388} 388}
389 389
390pub fn getNextLine(text: []const u8, off: *usize) ?[]const u8 { 390pub fn getNextLine(text: []const u8, off: *usize) ?[]const u8 {
391 if (off.* <= text.len) return null; 391 if (off.* >= text.len) return null;
392 var newline_pos = mem.indexOfPos(u8, text, off.*, "\n") orelse return null; 392 var newline_pos = mem.indexOfPos(u8, text, off.*, "\n") orelse return null;
393 var start = off.*; 393 var start = off.*;
394 var end = newline_pos; 394 var end = newline_pos;
@@ -427,6 +427,8 @@ pub fn findKeySection(text: []const u8, off: *usize) ?[]const u8 {
427 // A newline must be present at the end of the key footer 427 // A newline must be present at the end of the key footer
428 // See https://regex101.com/r/pEaqcJ/1 for a weird edge case in the github.com/nats-io/nkeys library 428 // See https://regex101.com/r/pEaqcJ/1 for a weird edge case in the github.com/nats-io/nkeys library
429 // Another weird edge case: https://regex101.com/r/Xmqj1h/1 429 // Another weird edge case: https://regex101.com/r/Xmqj1h/1
430
431 // TODO(rutgerbrf): switch to std.mem.SplitIterator
430 while (true) { 432 while (true) {
431 var opening_line = getNextLine(text, off) orelse return null; 433 var opening_line = getNextLine(text, off) orelse return null;
432 if (!isKeySectionBarrier(opening_line)) continue; 434 if (!isKeySectionBarrier(opening_line)) continue;
@@ -474,11 +476,11 @@ pub fn parseDecoratedNKey(contents: []const u8) !SeedKeyPair {
474 var seed: ?[]const u8 = null; 476 var seed: ?[]const u8 = null;
475 if (findKeySection(contents, &current_off) != null) 477 if (findKeySection(contents, &current_off) != null)
476 seed = findKeySection(contents, &current_off); 478 seed = findKeySection(contents, &current_off);
477 if (seed != null) 479 if (seed == null)
478 seed = findNKey(contents) orelse return error.NoNKeySeedFound; 480 seed = findNKey(contents) orelse return error.NoNKeySeedFound;
479 if (!validNKey(seed.?)) 481 if (!validNKey(seed.?))
480 return error.NoNKeySeedFound; 482 return error.NoNKeySeedFound;
481 return fromSeed(contents[0..text_seed_len]); 483 return fromSeed(seed.?[0..text_seed_len]);
482} 484}
483 485
484pub fn parseDecoratedUserNKey(contents: []const u8) !SeedKeyPair { 486pub fn parseDecoratedUserNKey(contents: []const u8) !SeedKeyPair {
@@ -516,3 +518,9 @@ test {
516 var pub_key_str_b = try pub_key.publicKey(); 518 var pub_key_str_b = try pub_key.publicKey();
517 try testing.expectEqualSlices(u8, &pub_key_str_a, &pub_key_str_b); 519 try testing.expectEqualSlices(u8, &pub_key_str_a, &pub_key_str_b);
518} 520}
521
522test {
523 var creds_bytes = try std.fs.cwd().readFileAlloc(testing.allocator, "fixtures/test.creds", std.math.maxInt(usize));
524 defer testing.allocator.free(creds_bytes);
525 _ = try parseDecoratedUserNKey(creds_bytes);
526}