Skip to content

Commit ea49a71

Browse files
committed
fixed
1 parent 393e697 commit ea49a71

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = .zig_jwt,
33
.description = "A JWT (JSON Web Token) library for zig.",
4-
.version = "1.2.8",
4+
.version = "1.2.9",
55
.fingerprint = 0x30a5aec248bd7ac3,
66
.minimum_zig_version = "0.15.0-dev.337+4e700fdf8",
77
.dependencies = .{},

src/jwt.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub const SigningMethodNone = JWT(none.SigningNone, []const u8, []const u8);
4848
pub const Error = error{
4949
JWTVerifyFail,
5050
JWTSigningMethodNotExists,
51+
JWTTokenInvalid,
5152
JWTTypeInvalid,
5253
JWTAlgoInvalid,
5354
};
@@ -110,6 +111,12 @@ pub fn JWT(comptime Signer: type, comptime SignKeyType: type, comptime VerifyKey
110111
var t = Token.init(self.alloc);
111112
t.parse(token_string);
112113

114+
if (t.getPartCount() < 2) {
115+
defer t.deinit();
116+
117+
return Error.JWTTokenInvalid;
118+
}
119+
113120
var header = try t.getHeader();
114121
defer header.deinit(self.alloc);
115122

@@ -145,6 +152,7 @@ pub fn JWT(comptime Signer: type, comptime SignKeyType: type, comptime VerifyKey
145152
return t;
146153
}
147154

155+
// build token
148156
pub fn build(self: Self) BuilderType {
149157
return BuilderType.init(self.alloc);
150158
}

src/jwt_test.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,23 @@ test "SigningMethodEdDSA type" {
14441444
try testing.expectEqualStrings(headers.alg, headers2.alg);
14451445
}
14461446

1447+
test "SigningMethodEdDSA JWTTokenInvalid" {
1448+
const alloc = testing.allocator;
1449+
1450+
const kp = jwt.eddsa.Ed25519.KeyPair.generate();
1451+
1452+
const token_string = "eyJhbGciOiJFRDI1NTE5IiwidHlwIjoiSldUIn0";
1453+
1454+
const p = jwt.SigningMethodEdDSA.init(alloc);
1455+
1456+
var need_true: bool = false;
1457+
_ = p.parse(token_string, kp.public_key) catch |err| {
1458+
need_true = true;
1459+
try testing.expectEqual(jwt.Error.JWTTokenInvalid, err);
1460+
};
1461+
try testing.expectEqual(true, need_true);
1462+
}
1463+
14471464
test "SigningMethodEdDSA JWTTypeInvalid" {
14481465
const alloc = testing.allocator;
14491466

src/rsa/rsa.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ pub const PublicKey = struct {
2424

2525
const Self = @This();
2626

27+
pub fn size(self: Self) usize {
28+
return byteLen(self.n.bits());
29+
}
30+
2731
pub fn fromBytes(mod: []const u8, exp: []const u8) !PublicKey {
2832
const n = try Modulus.fromBytes(mod, .big);
2933
if (n.bits() <= 512) return error.InsecureBitCount;
@@ -1138,6 +1142,8 @@ test "rsa PKCS1-v1_5 encrypt and decrypt" {
11381142

11391143
try std.testing.expectEqualSlices(u8, msg, dec);
11401144

1145+
try std.testing.expectEqual(256, kp.public_key.size());
1146+
11411147
// ==========
11421148

11431149
const check2 = "907052e0ee7f8f92990751c3432c73a3450a7dece61ba1876169875dc9b28b4aa40699c8377141ed021a92c1ab623d734e8cf1010814eb7fc26321c7b037cc467c0f2b9029c4fc082387c7dedb718dda3251b3b2a7f06871d446be2df051e2013d3726af7002a5e487559cf36ea6a11bacdfb12dc35cc9285bfed8906fac3c0c8a1a69bbdc8f834e5f1a766e13792dcc202bf48e7eb6aca78f8df4904b59d2d09b5eaaf58903217b1d0d21fb66e5e44836b422500a2c9d5e0f37232544dc32a0d1ec33e32c4b113057441097f936a6e7b4f49be6b7fb7240b0f982aee9b3fde4708fb7dfe365b9576bcd0fd0120a50658c76c2e0361b82fbf60a423b363dd354";
@@ -1307,6 +1313,9 @@ fn test_sign_with_key_der(prikey: []const u8, pubkey: []const u8) !void {
13071313
const pri_key = try SecretKey.fromDerAuto(prikey_bytes);
13081314
const pub_key = try PublicKey.fromDerAuto(pubkey_bytes);
13091315

1316+
try std.testing.expectEqual(256, pri_key.public_key.size());
1317+
try std.testing.expectEqual(256, pub_key.size());
1318+
13101319
const msg = "rsa PSS signature";
13111320
var out: [max_modulus_len]u8 = undefined;
13121321

src/token.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ pub const Token = struct {
130130
return self.alloc.dupe(u8, self.msg);
131131
}
132132

133+
pub fn getPartCount(self: *Self) usize {
134+
const count = std.mem.count(u8, self.raw, ".");
135+
return count + 1;
136+
}
137+
133138
fn getRawNoSignature(self: *Self) ![]const u8 {
134139
const count = std.mem.count(u8, self.raw, ".");
135140
if (count <= 1) {
@@ -270,6 +275,9 @@ test "Token" {
270275
defer alloc.free(signature2);
271276
try testing.expectEqual(0, signature2.len);
272277

278+
const partCount = token2.getPartCount();
279+
try testing.expectEqual(2, partCount);
280+
273281
// ====================
274282

275283
var token3 = Token.init(alloc);
@@ -299,6 +307,9 @@ test "Token" {
299307
defer alloc.free(token5);
300308
try testing.expectEqualStrings(check1, token5);
301309

310+
const partCount2 = token3.getPartCount();
311+
try testing.expectEqual(3, partCount2);
312+
302313
// ====================
303314

304315
const check3 = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9";
@@ -315,6 +326,9 @@ test "Token" {
315326
const sig6 = try token6.getMsg();
316327
defer alloc.free(sig6);
317328
try testing.expectEqualStrings(check3, sig6);
329+
330+
const partCount6 = token6.getPartCount();
331+
try testing.expectEqual(1, partCount6);
318332
}
319333

320334
test "Token 2" {

0 commit comments

Comments
 (0)