Skip to content

Commit 4eeb819

Browse files
committed
fixed
1 parent 664572c commit 4eeb819

File tree

2 files changed

+219
-181
lines changed

2 files changed

+219
-181
lines changed

src/token.zig

Lines changed: 13 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -181,119 +181,6 @@ pub const Token = struct {
181181
pub fn getSignature(self: *Self) []const u8 {
182182
return self.signature;
183183
}
184-
185-
pub fn isPermittedFor(self: *Self, audience: []const u8) !bool {
186-
const claims = try self.getClaims();
187-
188-
if (claims.object.get("aud")) |val| {
189-
if (val == .string) {
190-
if (utils.eq(audience, val.string)) {
191-
return true;
192-
}
193-
}
194-
195-
return false;
196-
}
197-
198-
return false;
199-
}
200-
201-
pub fn isIdentifiedBy(self: *Self, id: []const u8) !bool {
202-
const claims = try self.getClaims();
203-
204-
if (claims.object.get("jti")) |val| {
205-
if (val == .string) {
206-
if (utils.eq(id, val.string)) {
207-
return true;
208-
}
209-
}
210-
211-
return false;
212-
}
213-
214-
return false;
215-
}
216-
217-
pub fn isRelatedTo(self: *Self, subject: []const u8) !bool {
218-
const claims = try self.getClaims();
219-
220-
if (claims.object.get("sub")) |val| {
221-
if (val == .string) {
222-
if (utils.eq(subject, val.string)) {
223-
return true;
224-
}
225-
}
226-
227-
return false;
228-
}
229-
230-
return false;
231-
}
232-
233-
pub fn hasBeenIssuedBy(self: *Self, issuer: []const u8) !bool {
234-
const claims = try self.getClaims();
235-
236-
if (claims.object.get("iss")) |val| {
237-
if (val == .string) {
238-
if (utils.eq(issuer, val.string)) {
239-
return true;
240-
}
241-
}
242-
243-
return false;
244-
}
245-
246-
return false;
247-
}
248-
249-
pub fn hasBeenIssuedBefore(self: *Self, now: i64) !bool {
250-
const claims = try self.getClaims();
251-
252-
if (claims.object.get("iat")) |val| {
253-
if (val == .integer) {
254-
if (now > val.integer) {
255-
return true;
256-
}
257-
}
258-
259-
return false;
260-
}
261-
262-
return true;
263-
}
264-
265-
pub fn isMinimumTimeBefore(self: *Self, now: i64) !bool {
266-
const claims = try self.getClaims();
267-
268-
if (claims.object.get("nbf")) |val| {
269-
if (val == .integer) {
270-
if (now > val.integer) {
271-
return true;
272-
}
273-
}
274-
275-
return false;
276-
}
277-
278-
return true;
279-
}
280-
281-
pub fn isExpired(self: *Self, now: i64) !bool {
282-
const claims = try self.getClaims();
283-
284-
if (claims.object.get("exp")) |val| {
285-
if (val == .integer) {
286-
if (now <= val.integer) {
287-
return false;
288-
}
289-
}
290-
291-
return true;
292-
}
293-
294-
return false;
295-
}
296-
297184
};
298185

299186
test "Token" {
@@ -327,6 +214,19 @@ test "Token" {
327214

328215
// ====================
329216

217+
// pub const ObjectMap = StringArrayHashMap(Value);
218+
// pub const Array = ArrayList(Value);
219+
// pub const json.Value = union(enum) {
220+
// null,
221+
// bool: bool,
222+
// integer: i64,
223+
// float: f64,
224+
// number_string: []const u8,
225+
// string: []const u8,
226+
// array: Array,
227+
// object: ObjectMap,
228+
// }
229+
330230
var token2 = Token.init(alloc);
331231
try token2.parse(check1);
332232

@@ -475,71 +375,3 @@ test "Token 3" {
475375
try testing.expectEqualStrings(header.kid.?, header33.object.get("kid").?.string);
476376

477377
}
478-
479-
test "Token isExpired" {
480-
const alloc = std.heap.page_allocator;
481-
482-
const check1 = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJleHAiOjE3Mzk4MTAzOTB9.dGVzdC1zaWduYXR1cmU";
483-
const now = time.timestamp();
484-
485-
var token = Token.init(alloc);
486-
try token.parse(check1);
487-
const isExpired = try token.isExpired(now);
488-
489-
try testing.expectEqual(true, isExpired);
490-
try testing.expectEqualStrings(check1, token.raw);
491-
492-
// pub const ObjectMap = StringArrayHashMap(Value);
493-
// pub const Array = ArrayList(Value);
494-
// pub const json.Value = union(enum) {
495-
// null,
496-
// bool: bool,
497-
// integer: i64,
498-
// float: f64,
499-
// number_string: []const u8,
500-
// string: []const u8,
501-
// array: Array,
502-
// object: ObjectMap,
503-
// }
504-
505-
const claims = try token.getClaims();
506-
try testing.expectEqual(true, claims.object.get("exp").?.integer > 0);
507-
508-
}
509-
510-
test "Token isMinimumTimeBefore" {
511-
const alloc = std.heap.page_allocator;
512-
513-
const check1 = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJleGFtcGxlLmNvbSIsImlhdCI6ImZvbyIsIm5iZiI6MTczOTgxNjU0MH0.dGVzdC1zaWduYXR1cmU";
514-
const now = time.timestamp();
515-
516-
var token = Token.init(alloc);
517-
try token.parse(check1);
518-
const isMinimumTimeBefore = try token.isMinimumTimeBefore(now);
519-
520-
try testing.expectEqual(true, isMinimumTimeBefore);
521-
522-
const claims = try token.getClaims();
523-
try testing.expectEqual(true, claims.object.get("nbf").?.integer > 0);
524-
525-
}
526-
527-
test "Token hasBeenIssuedBefore" {
528-
const alloc = std.heap.page_allocator;
529-
530-
const check1 = "eyJ0eXAiOiJKV0UiLCJhbGciOiJFUzI1NiIsImtpZCI6ImtpZHMifQ.eyJpc3MiOiJpc3MiLCJpYXQiOjE1Njc4NDIzODgsImV4cCI6MTc2Nzg0MjM4OCwiYXVkIjoiZXhhbXBsZS5jb20iLCJzdWIiOiJzdWIiLCJqdGkiOiJqdGkgcnJyIiwibmJmIjoxNTY3ODQyMzg4fQ.dGVzdC1zaWduYXR1cmU";
531-
const now = time.timestamp();
532-
533-
var token = Token.init(alloc);
534-
try token.parse(check1);
535-
536-
try testing.expectEqual(true, try token.hasBeenIssuedBy("iss"));
537-
try testing.expectEqual(true, try token.isRelatedTo("sub"));
538-
try testing.expectEqual(true, try token.isIdentifiedBy("jti rrr"));
539-
try testing.expectEqual(true, try token.isPermittedFor("example.com"));
540-
try testing.expectEqual(true, try token.hasBeenIssuedBefore(now));
541-
542-
const claims = try token.getClaims();
543-
try testing.expectEqual(true, claims.object.get("nbf").?.integer > 0);
544-
545-
}

0 commit comments

Comments
 (0)