authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-31 15:47:41 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-31 15:47:41 -07:00
logbd3a5452ae3f88dcefd030bef36915079062308e
tree64e9ff3394efb6d1fb1ff0b6365b49340217cda6
parentdaedcedeb7b2bf9720b851a23044f10ea1dbfeba
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

switch from extras.parse_json to zig-json


3 files changed, 21 insertions(+), 16 deletions(-)

licenses.txt+3
...@@ -2,6 +2,8 @@ MIT:...@@ -2,6 +2,8 @@ MIT:
2= https://spdx.org/licenses/MIT2= https://spdx.org/licenses/MIT
3- This3- This
4- git https://github.com/nektro/zig-extras4- git https://github.com/nektro/zig-extras
5- git https://github.com/nektro/zig-json
6- git https://github.com/nektro/zig-sys-darwin
5- git https://github.com/nektro/zig-sys-linux7- git https://github.com/nektro/zig-sys-linux
6- git https://github.com/nektro/zig-time8- git https://github.com/nektro/zig-time
7- git https://github.com/nektro/zig-tracer9- git https://github.com/nektro/zig-tracer
...@@ -10,6 +12,7 @@ MIT:...@@ -10,6 +12,7 @@ MIT:
1012
11MPL-2.0:13MPL-2.0:
12= https://spdx.org/licenses/MPL-2.014= https://spdx.org/licenses/MPL-2.0
15- git https://github.com/nektro/zig-intrusive-parser
13- git https://github.com/nektro/zig-net-http16- git https://github.com/nektro/zig-net-http
14- git https://github.com/nektro/zig-net17- git https://github.com/nektro/zig-net
15- git https://github.com/nektro/zig-nfs18- git https://github.com/nektro/zig-nfs
oauth2.zig+17-16
...@@ -8,6 +8,7 @@ const extras = @import("extras");...@@ -8,6 +8,7 @@ const extras = @import("extras");
8const url = @import("url");8const url = @import("url");
9const http = @import("http");9const http = @import("http");
10const nio = @import("nio");10const nio = @import("nio");
11const json = @import("json");
11const Base = @This();12const Base = @This();
1213
13pub const Provider = struct {14pub const Provider = struct {
...@@ -332,17 +333,17 @@ pub fn Handlers(comptime T: type) type {...@@ -332,17 +333,17 @@ pub fn Handlers(comptime T: type) type {
332 const body_content = try req.reader().readAllAlloc(alloc, 1024 * 1024 * 5);333 const body_content = try req.reader().readAllAlloc(alloc, 1024 * 1024 * 5);
333 if (req.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req.response.status), body_content });334 if (req.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req.response.status), body_content });
334 if (req.response.status != .ok) return error.OauthBadToken;335 if (req.response.status != .ok) return error.OauthBadToken;
335 const val = try extras.parse_json(alloc, body_content);336 const val = try json.parseFromSlice(alloc, "body.json", body_content, .{ .maximum_depth = 100, .support_trailing_commas = true });
336337 val.acquire();
337 const tt = val.value.object.get("token_type").?.string;338 const tt = val.root.object().getS("token_type").?;
338 if (!std.mem.eql(u8, tt, "bearer")) return fail(response_status, body_writer, "oauth2: invalid token type: {s}", .{tt});339 if (!std.mem.eql(u8, tt, "bearer")) return fail(response_status, body_writer, "oauth2: invalid token type: {s}", .{tt});
339340 const at = val.root.object().getS("access_token") orelse return try fail(response_status, body_writer, "Identity Provider Login Error!\n{s}", .{body_content});
340 const at = val.value.object.get("access_token") orelse return try fail(response_status, body_writer, "Identity Provider Login Error!\n{s}", .{body_content});341 val.release();
341342
342 var req2 = try http_client.open(.GET, try std.Uri.parse(client.provider.me_url), .{343 var req2 = try http_client.open(.GET, try std.Uri.parse(client.provider.me_url), .{
343 .server_header_buffer = &buf,344 .server_header_buffer = &buf,
344 .headers = .{345 .headers = .{
345 .authorization = .{ .override = try nio.fmt.allocPrint(alloc, "Bearer {s}", .{at.string}) },346 .authorization = .{ .override = try nio.fmt.allocPrint(alloc, "Bearer {s}", .{at}) },
346 },347 },
347 .extra_headers = &.{348 .extra_headers = &.{
348 .{ .name = "Accept", .value = "application/json" },349 .{ .name = "Accept", .value = "application/json" },
...@@ -356,11 +357,12 @@ pub fn Handlers(comptime T: type) type {...@@ -356,11 +357,12 @@ pub fn Handlers(comptime T: type) type {
356 const body_content2 = try req2.reader().readAllAlloc(alloc, 1024 * 1024 * 5);357 const body_content2 = try req2.reader().readAllAlloc(alloc, 1024 * 1024 * 5);
357 if (req2.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req2.response.status), body_content2 });358 if (req2.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req2.response.status), body_content2 });
358 if (req2.response.status != .ok) return error.OauthBadUserinfo;359 if (req2.response.status != .ok) return error.OauthBadUserinfo;
359 const val2 = try extras.parse_json(alloc, body_content2);360 const val2 = try json.parseFromSlice(alloc, "body2.json", body_content2, .{ .maximum_depth = 100, .support_trailing_commas = true });
360361 val2.acquire();
361 const id = try fixId(alloc, val2.value.object.get(client.provider.id_prop).?);362 const id = try fixId(val2.root.object().getAny(client.provider.id_prop).?);
362 const name = val2.value.object.get(client.provider.name_prop).?.string;363 const name = val2.root.object().getS(client.provider.name_prop).?;
363 try T.saveInfo(response_headers, alloc, client.provider, id, name, val.value, val2.value);364 val2.release();
365 try T.saveInfo(response_headers, alloc, client.provider, id, name, val, val2);
364366
365 try response_headers.append("location", T.doneUrl);367 try response_headers.append("location", T.doneUrl);
366 response_status.* = .found;368 response_status.* = .found;
...@@ -399,11 +401,10 @@ fn redirectUri(request_headers: *const http.HeadersMap, alloc: std.mem.Allocator...@@ -399,11 +401,10 @@ fn redirectUri(request_headers: *const http.HeadersMap, alloc: std.mem.Allocator
399 return try nio.fmt.allocPrint(alloc, "{s}://{s}{s}", .{ proto, host, callbackPath });401 return try nio.fmt.allocPrint(alloc, "{s}://{s}{s}", .{ proto, host, callbackPath });
400}402}
401403
402fn fixId(alloc: std.mem.Allocator, id: std.json.Value) !string {404fn fixId(id: json.ValueIndex) !string {
403 return switch (id) {405 return switch (id.v()) {
404 .string => |v| v,406 .string => |v| v.to(),
405 .integer => |v| try nio.fmt.allocPrint(alloc, "{d}", .{v}),407 .number => |v| v.to(),
406 .float => |v| try nio.fmt.allocPrint(alloc, "{d}", .{v}),
407 else => unreachable,408 else => unreachable,
408 };409 };
409}410}
zig.mod+1
...@@ -11,3 +11,4 @@ dependencies:...@@ -11,3 +11,4 @@ dependencies:
11 - src: git https://github.com/nektro/zig-whatwg-url11 - src: git https://github.com/nektro/zig-whatwg-url
12 - src: git https://github.com/nektro/zig-net-http12 - src: git https://github.com/nektro/zig-net-http
13 - src: git https://github.com/nektro/zig-nio13 - src: git https://github.com/nektro/zig-nio
14 - src: git https://github.com/nektro/zig-json