Skip to content

Commit 019e847

Browse files
committed
fixed
1 parent 4eeb819 commit 019e847

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,39 @@ pub fn main() !void {
9393
~~~
9494

9595

96+
### Token Validator
97+
98+
~~~zig
99+
const std = @import("std");
100+
const jwt = @import("zig-jwt");
101+
102+
pub fn main() !void {
103+
const alloc = std.heap.page_allocator;
104+
105+
const token_string = "eyJ0eXAiOiJKV0UiLCJhbGciOiJFUzI1NiIsImtpZCI6ImtpZHMifQ.eyJpc3MiOiJpc3MiLCJpYXQiOjE1Njc4NDIzODgsImV4cCI6MTc2Nzg0MjM4OCwiYXVkIjoiZXhhbXBsZS5jb20iLCJzdWIiOiJzdWIiLCJqdGkiOiJqdGkgcnJyIiwibmJmIjoxNTY3ODQyMzg4fQ.dGVzdC1zaWduYXR1cmU";
106+
107+
var token = jwt.Token.init(alloc);
108+
try token.parse(token_string);
109+
110+
var validator = try jwt.Validator.init(token);
111+
defer validator.deinit();
112+
113+
// output:
114+
// hasBeenIssuedBy: true
115+
std.debug.print("hasBeenIssuedBy: {} \n", .{validator.hasBeenIssuedBy("iss")});
116+
117+
// have functions:
118+
// validator.hasBeenIssuedBy("iss")
119+
// validator.isRelatedTo("sub")
120+
// validator.isIdentifiedBy("jti rrr")
121+
// validator.isPermittedFor("example.com") // audience
122+
// validator.hasBeenIssuedBefore(now) // now is time timestamp
123+
// validator.isMinimumTimeBefore(now)
124+
// validator.isExpired(now)
125+
}
126+
~~~
127+
128+
96129
### Signing Methods
97130

98131
The JWT library have signing methods:

src/jwt.zig

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
const std = @import("std");
22
const fmt = std.fmt;
3+
const time = std.time;
34
const testing = std.testing;
45
const Allocator = std.mem.Allocator;
56

67
pub const crypto_rsa = @import("rsa/rsa.zig");
78

8-
pub const token = @import("token.zig");
9-
pub const utils = @import("utils.zig");
109
pub const rsa = @import("rsa.zig");
1110
pub const rsa_pss = @import("rsa_pss.zig");
1211
pub const ecdsa = @import("ecdsa.zig");
1312
pub const eddsa = @import("eddsa.zig");
1413
pub const hmac = @import("hmac.zig");
1514
pub 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

1720
pub const SigningMethodRS256 = JWT(rsa.SigningRS256, crypto_rsa.SecretKey, crypto_rsa.PublicKey);
1821
pub 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+
265291
test "SigningMethodEdDSA signWithHeader" {
266292
const alloc = std.heap.page_allocator;
267293

0 commit comments

Comments
 (0)