11const std = @import ("std" );
22const fmt = std .fmt ;
3+ const time = std .time ;
34const testing = std .testing ;
45const Allocator = std .mem .Allocator ;
56
67pub const crypto_rsa = @import ("rsa/rsa.zig" );
78
8- pub const token = @import ("token.zig" );
9- pub const utils = @import ("utils.zig" );
109pub const rsa = @import ("rsa.zig" );
1110pub const rsa_pss = @import ("rsa_pss.zig" );
1211pub const ecdsa = @import ("ecdsa.zig" );
1312pub const eddsa = @import ("eddsa.zig" );
1413pub const hmac = @import ("hmac.zig" );
1514pub const none = @import ("none.zig" );
15+ pub const utils = @import ("utils.zig" );
16+
17+ pub const Token = @import ("token.zig" ).Token ;
18+ pub const Validator = @import ("validator.zig" ).Validator ;
1619
1720pub const SigningMethodRS256 = JWT (rsa .SigningRS256 , crypto_rsa .SecretKey , crypto_rsa .PublicKey );
1821pub const SigningMethodRS384 = JWT (rsa .SigningRS384 , crypto_rsa .SecretKey , crypto_rsa .PublicKey );
@@ -76,7 +79,7 @@ pub fn JWT(comptime Signer: type, comptime SecretKeyType: type, comptime PublicK
7679
7780 // use SigningMethod with header to make token
7881 pub fn signWithHeader (self : Self , header : anytype , claims : anytype , secret_key : SecretKeyType ) ! []const u8 {
79- var t = token . Token .init (self .alloc );
82+ var t = Token .init (self .alloc );
8083 try t .setHeader (header );
8184 try t .setClaims (claims );
8285
@@ -93,8 +96,8 @@ pub fn JWT(comptime Signer: type, comptime SecretKeyType: type, comptime PublicK
9396 }
9497
9598 // parse token and verify token signature
96- pub fn parse (self : Self , token_string : []const u8 , public_key : PublicKeyType ) ! token. Token {
97- var t = token . Token .init (self .alloc );
99+ pub fn parse (self : Self , token_string : []const u8 , public_key : PublicKeyType ) ! Token {
100+ var t = Token .init (self .alloc );
98101 try t .parse (token_string );
99102
100103 const header = try t .getHeader ();
@@ -189,8 +192,8 @@ pub fn getSigningMethod(name: []const u8) !type {
189192 return Error .JWTSigningMethodNotExists ;
190193}
191194
192- pub fn getTokenHeader (alloc : Allocator , token_string : []const u8 ) ! token. Token.Header {
193- var t = token . Token .init (alloc );
195+ pub fn getTokenHeader (alloc : Allocator , token_string : []const u8 ) ! Token.Header {
196+ var t = Token .init (alloc );
194197 try t .parse (token_string );
195198
196199 const header = try t .getHeader ();
@@ -262,6 +265,29 @@ test "parse JWTSignatureInvalid" {
262265
263266}
264267
268+ test "Token Validator" {
269+ const alloc = std .heap .page_allocator ;
270+
271+ const check1 = "eyJ0eXAiOiJKV0UiLCJhbGciOiJFUzI1NiIsImtpZCI6ImtpZHMifQ.eyJpc3MiOiJpc3MiLCJpYXQiOjE1Njc4NDIzODgsImV4cCI6MTc2Nzg0MjM4OCwiYXVkIjoiZXhhbXBsZS5jb20iLCJzdWIiOiJzdWIiLCJqdGkiOiJqdGkgcnJyIiwibmJmIjoxNTY3ODQyMzg4fQ.dGVzdC1zaWduYXR1cmU" ;
272+ const now = time .timestamp ();
273+
274+ var token = Token .init (alloc );
275+ try token .parse (check1 );
276+
277+ var validator = try Validator .init (token );
278+ defer validator .deinit ();
279+
280+ try testing .expectEqual (true , validator .hasBeenIssuedBy ("iss" ));
281+ try testing .expectEqual (true , validator .isRelatedTo ("sub" ));
282+ try testing .expectEqual (true , validator .isIdentifiedBy ("jti rrr" ));
283+ try testing .expectEqual (true , validator .isPermittedFor ("example.com" ));
284+ try testing .expectEqual (true , validator .hasBeenIssuedBefore (now ));
285+
286+ const claims = try token .getClaims ();
287+ try testing .expectEqual (true , claims .object .get ("nbf" ).? .integer > 0 );
288+
289+ }
290+
265291test "SigningMethodEdDSA signWithHeader" {
266292 const alloc = std .heap .page_allocator ;
267293
0 commit comments