Skip to content

Commit b7e2720

Browse files
committed
update token
1 parent 98159bb commit b7e2720

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
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.7",
4+
.version = "1.2.8",
55
.fingerprint = 0x30a5aec248bd7ac3,
66
.minimum_zig_version = "0.15.0-dev.337+4e700fdf8",
77
.dependencies = .{},

src/token.zig

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const utils = @import("utils.zig");
88

99
pub const Token = struct {
1010
raw: []const u8 = "",
11+
msg: []const u8 = "",
1112
header: []const u8 = "",
1213
claims: []const u8 = "",
1314
signature: []const u8 = "",
@@ -37,6 +38,8 @@ pub const Token = struct {
3738
}
3839

3940
pub fn deinit(self: *Self) void {
41+
self.alloc.free(self.raw);
42+
self.alloc.free(self.msg);
4043
self.alloc.free(self.header);
4144
self.alloc.free(self.claims);
4245
self.alloc.free(self.signature);
@@ -100,7 +103,7 @@ pub const Token = struct {
100103
return;
101104
}
102105

103-
self.raw = token_string;
106+
self.raw = self.alloc.dupe(u8, token_string) catch "";
104107
self.header = "";
105108
self.claims = "";
106109
self.signature = "";
@@ -115,13 +118,19 @@ pub const Token = struct {
115118
if (it.next()) |pair| {
116119
self.signature = utils.base64UrlDecode(self.alloc, pair) catch "";
117120
}
121+
122+
self.msg = self.getRawNoSignature() catch "";
118123
}
119124

120125
pub fn getRaw(self: *Self) ![]const u8 {
121126
return self.alloc.dupe(u8, self.raw);
122127
}
123128

124-
pub fn getRawNoSignature(self: *Self) ![]const u8 {
129+
pub fn getMsg(self: *Self) ![]const u8 {
130+
return self.alloc.dupe(u8, self.msg);
131+
}
132+
133+
fn getRawNoSignature(self: *Self) ![]const u8 {
125134
const count = std.mem.count(u8, self.raw, ".");
126135
if (count <= 1) {
127136
return self.alloc.dupe(u8, self.raw);
@@ -286,7 +295,7 @@ test "Token" {
286295
defer alloc.free(token51);
287296
try testing.expectEqualStrings(check2, token51);
288297

289-
const token5 = try token3.getRawNoSignature();
298+
const token5 = try token3.getMsg();
290299
defer alloc.free(token5);
291300
try testing.expectEqualStrings(check1, token5);
292301

@@ -303,7 +312,7 @@ test "Token" {
303312
defer alloc.free(sig61);
304313
try testing.expectEqualStrings(check3, sig61);
305314

306-
const sig6 = try token6.getRawNoSignature();
315+
const sig6 = try token6.getMsg();
307316
defer alloc.free(sig6);
308317
try testing.expectEqualStrings(check3, sig6);
309318
}

0 commit comments

Comments
 (0)