aboutsummaryrefslogtreecommitdiffstats
path: root/src/nkeys.zig
diff options
context:
space:
mode:
authorLibravatar Rutger Broekhoff2021-05-21 10:10:19 +0200
committerLibravatar Rutger Broekhoff2021-05-21 10:10:26 +0200
commit7d6c1d94ad6d2afaf1384e3220a963ebeb3086c6 (patch)
tree1f59f731599082e86655c40241cdbdedd605e20b /src/nkeys.zig
parent2184a7fbb56e7330274031f93d50f4da21e9d2a8 (diff)
downloadzig-nkeys-7d6c1d94ad6d2afaf1384e3220a963ebeb3086c6.tar.gz
zig-nkeys-7d6c1d94ad6d2afaf1384e3220a963ebeb3086c6.zip
Make creds file parsing work
Diffstat (limited to 'src/nkeys.zig')
-rw-r--r--src/nkeys.zig14
1 files changed, 11 insertions, 3 deletions
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}