Skip to content

Commit 03e8316

Browse files
committed
fixed
1 parent 8403cf8 commit 03e8316

File tree

10 files changed

+509
-222
lines changed

10 files changed

+509
-222
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn main() !void {
6060
};
6161
6262
const s = jwt.SigningMethodEdDSA.init(alloc);
63-
const token_string = try s.make(claims, kp.secret_key);
63+
const token_string = try s.sign(claims, kp.secret_key);
6464
6565
// output:
6666
// make jwt: eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSJ9.eyJhdWQiOiJleGFtcGxlLmNvbSIsInN1YiI6ImZvbyJ9.8aYTV-9_Z1RQUPepUlut9gvniX_Cx_z8P60Z5FbnMMgNLPNP29ZtNG3k6pcU2TY_O3DkSsdxbN2HkmgvjDUPBg

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 library for zig.",
4-
.version = "1.0.5",
4+
.version = "1.0.6",
55
.paths = .{
66
"build.zig",
77
"build.zig.zon",

src/ecdsa.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ pub fn SignECDSA(comptime EC: type, comptime name: []const u8) type {
5555
@memcpy(signed[0..], signature);
5656

5757
const sig = EC.Signature.fromBytes(signed);
58-
5958
sig.verify(msg, key) catch {
6059
return false;
6160
};

src/eddsa.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ pub fn SignEdDSA(comptime name: []const u8) type {
5454
@memcpy(signed[0..], signature);
5555

5656
const sig = Ed25519.Signature.fromBytes(signed);
57-
5857
sig.verify(msg, key) catch {
5958
return false;
6059
};

src/jwt.zig

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ pub const eddsa = @import("eddsa.zig");
1414
pub const hmac = @import("hmac.zig");
1515
pub const none = @import("none.zig");
1616

17-
pub const SigningMethodRS256 = JWT(rsa.SigningRS256, crypto_rsa.KeyPair, crypto_rsa.PublicKey);
18-
pub const SigningMethodRS384 = JWT(rsa.SigningRS384, crypto_rsa.KeyPair, crypto_rsa.PublicKey);
19-
pub const SigningMethodRS512 = JWT(rsa.SigningRS512, crypto_rsa.KeyPair, crypto_rsa.PublicKey);
17+
pub const SigningMethodRS256 = JWT(rsa.SigningRS256, crypto_rsa.SecretKey, crypto_rsa.PublicKey);
18+
pub const SigningMethodRS384 = JWT(rsa.SigningRS384, crypto_rsa.SecretKey, crypto_rsa.PublicKey);
19+
pub const SigningMethodRS512 = JWT(rsa.SigningRS512, crypto_rsa.SecretKey, crypto_rsa.PublicKey);
2020

21-
pub const SigningMethodPS256 = JWT(rsa_pss.SigningPS256, crypto_rsa.KeyPair, crypto_rsa.PublicKey);
22-
pub const SigningMethodPS384 = JWT(rsa_pss.SigningPS384, crypto_rsa.KeyPair, crypto_rsa.PublicKey);
23-
pub const SigningMethodPS512 = JWT(rsa_pss.SigningPS512, crypto_rsa.KeyPair, crypto_rsa.PublicKey);
21+
pub const SigningMethodPS256 = JWT(rsa_pss.SigningPS256, crypto_rsa.SecretKey, crypto_rsa.PublicKey);
22+
pub const SigningMethodPS384 = JWT(rsa_pss.SigningPS384, crypto_rsa.SecretKey, crypto_rsa.PublicKey);
23+
pub const SigningMethodPS512 = JWT(rsa_pss.SigningPS512, crypto_rsa.SecretKey, crypto_rsa.PublicKey);
2424

2525
pub const SigningMethodES256 = JWT(ecdsa.SigningES256, ecdsa.ecdsa.EcdsaP256Sha256.SecretKey, ecdsa.ecdsa.EcdsaP256Sha256.PublicKey);
2626
pub const SigningMethodES384 = JWT(ecdsa.SigningES384, ecdsa.ecdsa.EcdsaP384Sha384.SecretKey, ecdsa.ecdsa.EcdsaP384Sha384.PublicKey);
@@ -56,7 +56,7 @@ pub fn JWT(comptime Signer: type, comptime SecretKeyType: type, comptime PublicK
5656
};
5757
}
5858

59-
pub fn make(self: Self, claims: anytype, key: SecretKeyType) ![]const u8 {
59+
pub fn sign(self: Self, claims: anytype, key: SecretKeyType) ![]const u8 {
6060
var t = token.Token.init(self.alloc);
6161
try t.setHeader(.{
6262
.typ = "JWT",
@@ -68,7 +68,7 @@ pub fn JWT(comptime Signer: type, comptime SecretKeyType: type, comptime PublicK
6868
defer t.deinit();
6969

7070
const signed_string = try self.signer.sign(signed, key);
71-
try t.setSignature(signed_string);
71+
t.withSignature(signed_string);
7272

7373
defer self.alloc.free(signed_string);
7474

@@ -95,7 +95,7 @@ pub fn JWT(comptime Signer: type, comptime SecretKeyType: type, comptime PublicK
9595

9696
defer self.alloc.free(signed);
9797

98-
const msg = try t.signingString();
98+
const msg = t.getRawNoSignature();
9999
if (!self.signer.verify(msg, signed, key)) {
100100
return Error.JWTVerifyFail;
101101
}
@@ -256,7 +256,7 @@ test "SigningMethodEdDSA" {
256256
};
257257

258258
const s = SigningMethodEdDSA.init(alloc);
259-
const token_string = try s.make(claims, kp.secret_key);
259+
const token_string = try s.sign(claims, kp.secret_key);
260260
try testing.expectEqual(true, token_string.len > 0);
261261

262262
// ==========
@@ -281,7 +281,7 @@ test "SigningMethodES256" {
281281
};
282282

283283
const s = SigningMethodES256.init(alloc);
284-
const token_string = try s.make(claims, kp.secret_key);
284+
const token_string = try s.sign(claims, kp.secret_key);
285285
try testing.expectEqual(true, token_string.len > 0);
286286

287287
// ==========
@@ -306,7 +306,7 @@ test "SigningMethodES384" {
306306
};
307307

308308
const s = SigningMethodES384.init(alloc);
309-
const token_string = try s.make(claims, kp.secret_key);
309+
const token_string = try s.sign(claims, kp.secret_key);
310310
try testing.expectEqual(true, token_string.len > 0);
311311

312312
// ==========
@@ -330,7 +330,7 @@ test "SigningMethodHS256" {
330330
const key = "test-key";
331331

332332
const s = SigningMethodHS256.init(alloc);
333-
const token_string = try s.make(claims, key);
333+
const token_string = try s.sign(claims, key);
334334
try testing.expectEqual(true, token_string.len > 0);
335335

336336
// ==========
@@ -354,7 +354,7 @@ test "SigningMethodHS384" {
354354
const key = "test-key";
355355

356356
const s = SigningMethodHS384.init(alloc);
357-
const token_string = try s.make(claims, key);
357+
const token_string = try s.sign(claims, key);
358358
try testing.expectEqual(true, token_string.len > 0);
359359

360360
// ==========
@@ -378,7 +378,7 @@ test "SigningMethodHS512" {
378378
const key = "test-key";
379379

380380
const s = SigningMethodHS512.init(alloc);
381-
const token_string = try s.make(claims, key);
381+
const token_string = try s.sign(claims, key);
382382
try testing.expectEqual(true, token_string.len > 0);
383383

384384
// ==========
@@ -402,7 +402,7 @@ test "SigningMethodNone" {
402402
const key = "";
403403

404404
const s = SigningMethodNone.init(alloc);
405-
const token_string = try s.make(claims, key);
405+
const token_string = try s.sign(claims, key);
406406
try testing.expectEqual(true, token_string.len > 0);
407407

408408
// ==========
@@ -451,7 +451,7 @@ test "SigningMethodES256 Check" {
451451
};
452452

453453
const s = SigningMethodES256.init(alloc);
454-
const token_string = try s.make(claims, secret_key);
454+
const token_string = try s.sign(claims, secret_key);
455455
try testing.expectEqual(true, token_string.len > 0);
456456

457457
// ==========
@@ -487,7 +487,7 @@ test "SigningMethodES384 Check" {
487487
};
488488

489489
const s = SigningMethodES384.init(alloc);
490-
const token_string = try s.make(claims, secret_key);
490+
const token_string = try s.sign(claims, secret_key);
491491
try testing.expectEqual(true, token_string.len > 0);
492492

493493
// ==========
@@ -546,7 +546,7 @@ test "SigningMethodEdDSA Check" {
546546
};
547547

548548
const s = SigningMethodED25519.init(alloc);
549-
const token_string = try s.make(claims, secret_key);
549+
const token_string = try s.sign(claims, secret_key);
550550
try testing.expectEqual(true, token_string.len > 0);
551551

552552
// ==========
@@ -599,7 +599,7 @@ test "SigningMethodHS256 Check" {
599599
};
600600

601601
const s = SigningMethodHS256.init(alloc);
602-
const token_string = try s.make(claims, key_bytes);
602+
const token_string = try s.sign(claims, key_bytes);
603603
try testing.expectEqual(true, token_string.len > 0);
604604

605605
// ==========
@@ -628,7 +628,7 @@ test "SigningMethodHS384 Check" {
628628
};
629629

630630
const s = SigningMethodHS384.init(alloc);
631-
const token_string = try s.make(claims, key_bytes);
631+
const token_string = try s.sign(claims, key_bytes);
632632
try testing.expectEqual(true, token_string.len > 0);
633633

634634
// ==========
@@ -657,7 +657,7 @@ test "SigningMethodHS512 Check" {
657657
};
658658

659659
const s = SigningMethodHS512.init(alloc);
660-
const token_string = try s.make(claims, key_bytes);
660+
const token_string = try s.sign(claims, key_bytes);
661661
try testing.expectEqual(true, token_string.len > 0);
662662

663663
// ==========
@@ -711,7 +711,7 @@ test "SigningMethodES256 with JWTClaims" {
711711
};
712712

713713
const s = SigningMethodES256.init(alloc);
714-
const token_string = try s.make(claims, kp.secret_key);
714+
const token_string = try s.sign(claims, kp.secret_key);
715715
try testing.expectEqual(true, token_string.len > 0);
716716

717717
// ==========
@@ -734,7 +734,7 @@ test "SigningMethodRS256" {
734734
const prikey_bytes = try utils.base64Decode(alloc, prikey);
735735
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
736736

737-
const secret_key = try crypto_rsa.KeyPair.fromDer(prikey_bytes);
737+
const secret_key = try crypto_rsa.SecretKey.fromDer(prikey_bytes);
738738
const public_key = try crypto_rsa.PublicKey.fromDer(pubkey_bytes);
739739

740740
const claims = .{
@@ -743,7 +743,7 @@ test "SigningMethodRS256" {
743743
};
744744

745745
const s = SigningMethodRS256.init(alloc);
746-
const token_string = try s.make(claims, secret_key);
746+
const token_string = try s.sign(claims, secret_key);
747747
try testing.expectEqual(true, token_string.len > 0);
748748

749749
// ==========
@@ -766,7 +766,7 @@ test "SigningMethodRS384" {
766766
const prikey_bytes = try utils.base64Decode(alloc, prikey);
767767
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
768768

769-
const secret_key = try crypto_rsa.KeyPair.fromDer(prikey_bytes);
769+
const secret_key = try crypto_rsa.SecretKey.fromDer(prikey_bytes);
770770
const public_key = try crypto_rsa.PublicKey.fromDer(pubkey_bytes);
771771

772772
const claims = .{
@@ -775,7 +775,7 @@ test "SigningMethodRS384" {
775775
};
776776

777777
const s = SigningMethodRS384.init(alloc);
778-
const token_string = try s.make(claims, secret_key);
778+
const token_string = try s.sign(claims, secret_key);
779779
try testing.expectEqual(true, token_string.len > 0);
780780

781781
// ==========
@@ -798,7 +798,7 @@ test "SigningMethodRS512" {
798798
const prikey_bytes = try utils.base64Decode(alloc, prikey);
799799
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
800800

801-
const secret_key = try crypto_rsa.KeyPair.fromDer(prikey_bytes);
801+
const secret_key = try crypto_rsa.SecretKey.fromDer(prikey_bytes);
802802
const public_key = try crypto_rsa.PublicKey.fromDer(pubkey_bytes);
803803

804804
const claims = .{
@@ -807,7 +807,7 @@ test "SigningMethodRS512" {
807807
};
808808

809809
const s = SigningMethodRS512.init(alloc);
810-
const token_string = try s.make(claims, secret_key);
810+
const token_string = try s.sign(claims, secret_key);
811811
try testing.expectEqual(true, token_string.len > 0);
812812

813813
// ==========
@@ -830,7 +830,7 @@ test "SigningMethodPS256" {
830830
const prikey_bytes = try utils.base64Decode(alloc, prikey);
831831
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
832832

833-
const secret_key = try crypto_rsa.KeyPair.fromDer(prikey_bytes);
833+
const secret_key = try crypto_rsa.SecretKey.fromDer(prikey_bytes);
834834
const public_key = try crypto_rsa.PublicKey.fromDer(pubkey_bytes);
835835

836836
const claims = .{
@@ -839,7 +839,7 @@ test "SigningMethodPS256" {
839839
};
840840

841841
const s = SigningMethodPS256.init(alloc);
842-
const token_string = try s.make(claims, secret_key);
842+
const token_string = try s.sign(claims, secret_key);
843843
try testing.expectEqual(true, token_string.len > 0);
844844

845845
// ==========
@@ -862,7 +862,7 @@ test "SigningMethodPS384" {
862862
const prikey_bytes = try utils.base64Decode(alloc, prikey);
863863
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
864864

865-
const secret_key = try crypto_rsa.KeyPair.fromDer(prikey_bytes);
865+
const secret_key = try crypto_rsa.SecretKey.fromDer(prikey_bytes);
866866
const public_key = try crypto_rsa.PublicKey.fromDer(pubkey_bytes);
867867

868868
const claims = .{
@@ -871,7 +871,7 @@ test "SigningMethodPS384" {
871871
};
872872

873873
const s = SigningMethodPS384.init(alloc);
874-
const token_string = try s.make(claims, secret_key);
874+
const token_string = try s.sign(claims, secret_key);
875875
try testing.expectEqual(true, token_string.len > 0);
876876

877877
// ==========
@@ -894,7 +894,7 @@ test "SigningMethodPS512" {
894894
const prikey_bytes = try utils.base64Decode(alloc, prikey);
895895
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
896896

897-
const secret_key = try crypto_rsa.KeyPair.fromDer(prikey_bytes);
897+
const secret_key = try crypto_rsa.SecretKey.fromDer(prikey_bytes);
898898
const public_key = try crypto_rsa.PublicKey.fromDer(pubkey_bytes);
899899

900900
const claims = .{
@@ -903,7 +903,7 @@ test "SigningMethodPS512" {
903903
};
904904

905905
const s = SigningMethodPS512.init(alloc);
906-
const token_string = try s.make(claims, secret_key);
906+
const token_string = try s.sign(claims, secret_key);
907907
try testing.expectEqual(true, token_string.len > 0);
908908

909909
// ==========

src/rsa.zig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,26 @@ pub fn SignRSA(comptime RSAType: type, comptime name: []const u8) type {
4040
return max_modulus_len;
4141
}
4242

43-
pub fn sign(self: Self, msg: []const u8, key: rsa.KeyPair) ![]u8 {
43+
pub fn sign(self: Self, msg: []const u8, key: rsa.SecretKey) ![]u8 {
4444
var signer = RSAType.Signer.init(key);
4545
signer.update(msg[0..]);
4646

4747
var out: [max_modulus_len]u8 = undefined;
4848
const sig = try signer.finalize(&out);
4949

50-
const out_string = try self.alloc.alloc(u8, sig.bytes.len);
51-
@memcpy(out_string[0..], sig.bytes[0..]);
50+
const signed = sig.toBytes();
51+
52+
const out_string = try self.alloc.alloc(u8, signed.len);
53+
@memcpy(out_string[0..], signed[0..]);
5254

5355
return out_string;
5456
}
5557

5658
pub fn verify(self: Self, msg: []const u8, signature: []u8, key: rsa.PublicKey) bool {
5759
_ = self;
5860

59-
var verifier = RSAType.Signature{
60-
.bytes = signature,
61-
};
62-
verifier.verify(msg, key) catch {
61+
var sig = RSAType.Signature.fromBytes(signature);
62+
sig.verify(msg, key) catch {
6363
return false;
6464
};
6565

@@ -84,7 +84,7 @@ test "SigningRS256" {
8484
const prikey_bytes = try utils.base64Decode(alloc, prikey);
8585
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
8686

87-
const secret_key = try rsa.KeyPair.fromDer(prikey_bytes);
87+
const secret_key = try rsa.SecretKey.fromDer(prikey_bytes);
8888
const public_key = try rsa.PublicKey.fromDer(pubkey_bytes);
8989

9090
const msg = "test-data";
@@ -115,7 +115,7 @@ test "SigningRS384" {
115115
const prikey_bytes = try utils.base64Decode(alloc, prikey);
116116
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
117117

118-
const secret_key = try rsa.KeyPair.fromDer(prikey_bytes);
118+
const secret_key = try rsa.SecretKey.fromDer(prikey_bytes);
119119
const public_key = try rsa.PublicKey.fromDer(pubkey_bytes);
120120

121121
const msg = "test-data";
@@ -146,7 +146,7 @@ test "SigningRS512" {
146146
const prikey_bytes = try utils.base64Decode(alloc, prikey);
147147
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
148148

149-
const secret_key = try rsa.KeyPair.fromDer(prikey_bytes);
149+
const secret_key = try rsa.SecretKey.fromDer(prikey_bytes);
150150
const public_key = try rsa.PublicKey.fromDer(pubkey_bytes);
151151

152152
const msg = "test-data";
@@ -177,7 +177,7 @@ test "SigningRS256 check" {
177177
const prikey_bytes = try utils.base64Decode(alloc, prikey);
178178
const pubkey_bytes = try utils.base64Decode(alloc, pubkey);
179179

180-
const secret_key = try rsa.KeyPair.fromDer(prikey_bytes);
180+
const secret_key = try rsa.SecretKey.fromDer(prikey_bytes);
181181
const public_key = try rsa.PublicKey.fromDer(pubkey_bytes);
182182

183183
const msg = "test-data";

0 commit comments

Comments
 (0)