From 7d6c1d94ad6d2afaf1384e3220a963ebeb3086c6 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Fri, 21 May 2021 10:10:19 +0200 Subject: Make creds file parsing work --- fixtures/test.creds | 14 ++++++++++++++ src/nkeys.zig | 14 +++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 fixtures/test.creds 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 @@ +-----BEGIN NATS USER JWT----- +eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJqdGkiOiJUWEg1TUxDNTdPTUJUQURYNUJNU0RLWkhSQUtXUFM0TkdHRFFPVlJXRzUyRFdaUlFFVERBIiwiaWF0IjoxNjIxNTgyOTU1LCJpc3MiOiJBQ1ZUQVZMQlFKTklQRjdNWFZWSlpZUFhaTkdFQUZMWVpTUjJSNVRZNk9ESjNSTTRYV0FDNUVFRiIsIm5hbWUiOiJ0ZXN0Iiwic3ViIjoiVUJHSlhLRkVWUlFEM05LM0lDRVc1Q0lDSzM1NkdESVZORkhaRUU0SzdMMkRYWTdORVNQVlFVNEwiLCJuYXRzIjp7InB1YiI6e30sInN1YiI6e30sInN1YnMiOi0xLCJkYXRhIjotMSwicGF5bG9hZCI6LTEsInR5cGUiOiJ1c2VyIiwidmVyc2lvbiI6Mn19.OhPLDZflyJ_keg2xBRDHZZhG5x_Qf_Yb61k9eHLs9zLRf0_ETwMd0PNZI_isuBhXYevobXHVoYA3oxvMVGlDCQ +------END NATS USER JWT------ + +************************* IMPORTANT ************************* +NKEY Seed printed below can be used to sign and prove identity. +NKEYs are sensitive and should be treated as secrets. + +-----BEGIN USER NKEY SEED----- +SUAGIEYODKBBTUMOB666Z5KA4FCWAZV7HWSGRHOD7MK6UM5IYLWLACH7DQ +------END USER NKEY SEED------ + +************************************************************* + 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 } pub fn getNextLine(text: []const u8, off: *usize) ?[]const u8 { - if (off.* <= text.len) return null; + if (off.* >= text.len) return null; var newline_pos = mem.indexOfPos(u8, text, off.*, "\n") orelse return null; var start = off.*; var end = newline_pos; @@ -427,6 +427,8 @@ pub fn findKeySection(text: []const u8, off: *usize) ?[]const u8 { // A newline must be present at the end of the key footer // See https://regex101.com/r/pEaqcJ/1 for a weird edge case in the github.com/nats-io/nkeys library // Another weird edge case: https://regex101.com/r/Xmqj1h/1 + + // TODO(rutgerbrf): switch to std.mem.SplitIterator while (true) { var opening_line = getNextLine(text, off) orelse return null; if (!isKeySectionBarrier(opening_line)) continue; @@ -474,11 +476,11 @@ pub fn parseDecoratedNKey(contents: []const u8) !SeedKeyPair { var seed: ?[]const u8 = null; if (findKeySection(contents, ¤t_off) != null) seed = findKeySection(contents, ¤t_off); - if (seed != null) + if (seed == null) seed = findNKey(contents) orelse return error.NoNKeySeedFound; if (!validNKey(seed.?)) return error.NoNKeySeedFound; - return fromSeed(contents[0..text_seed_len]); + return fromSeed(seed.?[0..text_seed_len]); } pub fn parseDecoratedUserNKey(contents: []const u8) !SeedKeyPair { @@ -516,3 +518,9 @@ test { var pub_key_str_b = try pub_key.publicKey(); try testing.expectEqualSlices(u8, &pub_key_str_a, &pub_key_str_b); } + +test { + var creds_bytes = try std.fs.cwd().readFileAlloc(testing.allocator, "fixtures/test.creds", std.math.maxInt(usize)); + defer testing.allocator.free(creds_bytes); + _ = try parseDecoratedUserNKey(creds_bytes); +} -- cgit v1.2.3