Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ pub fn build(b: *std.Build) !void {
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
}),
});

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
Expand Down Expand Up @@ -65,9 +67,11 @@ pub fn build(b: *std.Build) !void {

var exe = b.addExecutable(.{
.name = example.name,
.root_source_file = b.path(example.src),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path(example.src),
.target = target,
.optimize = optimize,
}),
});
exe.root_module.addImport("jwt", jwt);

Expand All @@ -80,9 +84,11 @@ pub fn build(b: *std.Build) !void {
example_step.dependOn(&example_build_step.step);

const benchmark_tests = b.addTest(.{
.root_source_file = b.path("src/bench.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/bench.zig"),
.target = target,
.optimize = optimize,
}),
.filters = &.{"bench"},
});
const benchmark = b.dependency("benchmark", .{
Expand Down
5 changes: 3 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
.{
.name = "jwt",
.name = .jwt,
.fingerprint = 0x8d17cdf07e2c9836,
.version = "0.1.0",
.minimum_zig_version = "0.13.0",
.dependencies = .{
// todo: update this to a versioned tag when this package publishes one
.benchmark = .{
.url = "https://github.com/silversquirl/benchmark.zig/archive/refs/heads/main/main.tar.gz",
.hash = "1220287c22cfcf85f05d353084e12200c6a7dcb5f4511cb05103f5dbd709e50731a7",
.hash = "12208e0453a69b1069810d693c39dbef091af8e17605544a7ac85364389169e6d4c3",
},
},
.paths = .{
Expand Down
55 changes: 52 additions & 3 deletions src/decode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,37 @@ pub fn decode(
return error.MalformedJWT;
}

pub fn decodeNoVerify(
allocator: std.mem.Allocator,
comptime ClaimSet: type,
str: []const u8,
) !JWT(ClaimSet) {
var arena = try allocator.create(std.heap.ArenaAllocator);
arena.* = std.heap.ArenaAllocator.init(allocator);
errdefer {
arena.deinit();
allocator.destroy(arena);
}
if (std.mem.count(u8, str, ".") == 2) {
const sigSplit = std.mem.lastIndexOfScalar(u8, str, '.').?;
const messageEnc = str[0..sigSplit];

const header = try decodePart(arena.allocator(), Header, messageEnc[0..std.mem.indexOfScalar(u8, messageEnc, '.').?]);
const claims = try decodePart(
allocator,
ClaimSet,
messageEnc[std.mem.indexOfScalar(u8, messageEnc, '.').? + 1 ..],
);

return .{
.arena = arena,
.header = header,
.claims = claims,
};
}
return error.MalformedJWT;
}

pub fn verify(
allocator: std.mem.Allocator,
algo: Algorithm,
Expand All @@ -86,8 +117,11 @@ pub fn verify(
.secret => |v| v,
else => return error.InvalidDecodingKey,
});
if (src.len != sig.len) {
return error.InvalidSignature;
}
@memcpy(&src, sig);
if (!std.crypto.utils.timingSafeEql([dest.len]u8, src, dest)) {
if (!std.crypto.timing_safe.eql([dest.len]u8, src, dest)) {
return error.InvalidSignature;
}
},
Expand All @@ -98,8 +132,11 @@ pub fn verify(
.secret => |v| v,
else => return error.InvalidDecodingKey,
});
if (src.len != sig.len) {
return error.InvalidSignature;
}
@memcpy(&src, sig);
if (!std.crypto.utils.timingSafeEql([dest.len]u8, src, dest)) {
if (!std.crypto.timing_safe.eql([dest.len]u8, src, dest)) {
return error.InvalidSignature;
}
},
Expand All @@ -110,13 +147,19 @@ pub fn verify(
.secret => |v| v,
else => return error.InvalidDecodingKey,
});
if (src.len != sig.len) {
return error.InvalidSignature;
}
@memcpy(&src, sig);
if (!std.crypto.utils.timingSafeEql([dest.len]u8, src, dest)) {
if (!std.crypto.timing_safe.eql([dest.len]u8, src, dest)) {
return error.InvalidSignature;
}
},
.ES256 => {
var src: [std.crypto.sign.ecdsa.EcdsaP256Sha256.Signature.encoded_length]u8 = undefined;
if (src.len != sig.len) {
return error.InvalidSignature;
}
@memcpy(&src, sig);
std.crypto.sign.ecdsa.EcdsaP256Sha256.Signature.fromBytes(src).verify(msg, switch (key) {
.es256 => |v| v,
Expand All @@ -127,6 +170,9 @@ pub fn verify(
},
.ES384 => {
var src: [std.crypto.sign.ecdsa.EcdsaP384Sha384.Signature.encoded_length]u8 = undefined;
if (src.len != sig.len) {
return error.InvalidSignature;
}
@memcpy(&src, sig);
std.crypto.sign.ecdsa.EcdsaP384Sha384.Signature.fromBytes(src).verify(msg, switch (key) {
.es384 => |v| v,
Expand All @@ -147,6 +193,9 @@ pub fn verify(
// },
.EdDSA => {
var src: [std.crypto.sign.Ed25519.Signature.encoded_length]u8 = undefined;
if (src.len != sig.len) {
return error.InvalidSignature;
}
@memcpy(&src, sig);
std.crypto.sign.Ed25519.Signature.fromBytes(src).verify(msg, switch (key) {
.edsa => |v| v,
Expand Down
6 changes: 3 additions & 3 deletions src/encode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn encodePart(
part: anytype,
) ![]const u8 {
const encoder = std.base64.url_safe_no_pad.Encoder;
const json = try std.json.stringifyAlloc(allocator, part, .{ .emit_null_optional_fields = false });
const json = try std.json.Stringify.valueAlloc(allocator, part, .{ .emit_null_optional_fields = false });
defer allocator.free(json);
const enc = try allocator.alloc(u8, encoder.calcSize(json.len));
_ = encoder.encode(enc, json);
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn encode(
key: EncodingKey,
) ![]const u8 {
comptime {
if (@typeInfo(@TypeOf(claims)) != .Struct) {
if (@typeInfo(@TypeOf(claims)) != .@"struct") {
@compileError("expected claims to be a struct but was a " ++ @typeName(@TypeOf(claims)));
}
}
Expand All @@ -123,7 +123,7 @@ pub fn encode(
defer allocator.free(sig_enc);
_ = encoder.encode(sig_enc, sig);

var buf = std.ArrayList(u8).init(allocator);
var buf = std.array_list.Managed(u8).init(allocator);
defer buf.deinit();
try buf.appendSlice(msg);
try buf.append('.');
Expand Down
Loading