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:
22= https://spdx.org/licenses/MIT
33- This
44- git https://github.com/nektro/zig-extras
5- git https://github.com/nektro/zig-json
6- git https://github.com/nektro/zig-sys-darwin
57- git https://github.com/nektro/zig-sys-linux
68- git https://github.com/nektro/zig-time
79- git https://github.com/nektro/zig-tracer
......@@ -10,6 +12,7 @@ MIT:
1012
1113MPL-2.0:
1214= https://spdx.org/licenses/MPL-2.0
15- git https://github.com/nektro/zig-intrusive-parser
1316- git https://github.com/nektro/zig-net-http
1417- git https://github.com/nektro/zig-net
1518- git https://github.com/nektro/zig-nfs
oauth2.zig+17-16
......@@ -8,6 +8,7 @@ const extras = @import("extras");
88const url = @import("url");
99const http = @import("http");
1010const nio = @import("nio");
11const json = @import("json");
1112const Base = @This();
1213
1314pub const Provider = struct {
......@@ -332,17 +333,17 @@ pub fn Handlers(comptime T: type) type {
332333 const body_content = try req.reader().readAllAlloc(alloc, 1024 * 1024 * 5);
333334 if (req.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req.response.status), body_content });
334335 if (req.response.status != .ok) return error.OauthBadToken;
335 const val = try extras.parse_json(alloc, body_content);
336
337 const tt = val.value.object.get("token_type").?.string;
336 const val = try json.parseFromSlice(alloc, "body.json", body_content, .{ .maximum_depth = 100, .support_trailing_commas = true });
337 val.acquire();
338 const tt = val.root.object().getS("token_type").?;
338339 if (!std.mem.eql(u8, tt, "bearer")) return fail(response_status, body_writer, "oauth2: invalid token type: {s}", .{tt});
339
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});
340 const at = val.root.object().getS("access_token") orelse return try fail(response_status, body_writer, "Identity Provider Login Error!\n{s}", .{body_content});
341 val.release();
341342
342343 var req2 = try http_client.open(.GET, try std.Uri.parse(client.provider.me_url), .{
343344 .server_header_buffer = &buf,
344345 .headers = .{
345 .authorization = .{ .override = try nio.fmt.allocPrint(alloc, "Bearer {s}", .{at.string}) },
346 .authorization = .{ .override = try nio.fmt.allocPrint(alloc, "Bearer {s}", .{at}) },
346347 },
347348 .extra_headers = &.{
348349 .{ .name = "Accept", .value = "application/json" },
......@@ -356,11 +357,12 @@ pub fn Handlers(comptime T: type) type {
356357 const body_content2 = try req2.reader().readAllAlloc(alloc, 1024 * 1024 * 5);
357358 if (req2.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req2.response.status), body_content2 });
358359 if (req2.response.status != .ok) return error.OauthBadUserinfo;
359 const val2 = try extras.parse_json(alloc, body_content2);
360
361 const id = try fixId(alloc, val2.value.object.get(client.provider.id_prop).?);
362 const name = val2.value.object.get(client.provider.name_prop).?.string;
363 try T.saveInfo(response_headers, alloc, client.provider, id, name, val.value, val2.value);
360 const val2 = try json.parseFromSlice(alloc, "body2.json", body_content2, .{ .maximum_depth = 100, .support_trailing_commas = true });
361 val2.acquire();
362 const id = try fixId(val2.root.object().getAny(client.provider.id_prop).?);
363 const name = val2.root.object().getS(client.provider.name_prop).?;
364 val2.release();
365 try T.saveInfo(response_headers, alloc, client.provider, id, name, val, val2);
364366
365367 try response_headers.append("location", T.doneUrl);
366368 response_status.* = .found;
......@@ -399,11 +401,10 @@ fn redirectUri(request_headers: *const http.HeadersMap, alloc: std.mem.Allocator
399401 return try nio.fmt.allocPrint(alloc, "{s}://{s}{s}", .{ proto, host, callbackPath });
400402}
401403
402fn fixId(alloc: std.mem.Allocator, id: std.json.Value) !string {
403 return switch (id) {
404 .string => |v| v,
405 .integer => |v| try nio.fmt.allocPrint(alloc, "{d}", .{v}),
406 .float => |v| try nio.fmt.allocPrint(alloc, "{d}", .{v}),
404fn fixId(id: json.ValueIndex) !string {
405 return switch (id.v()) {
406 .string => |v| v.to(),
407 .number => |v| v.to(),
407408 else => unreachable,
408409 };
409410}
zig.mod+1
......@@ -11,3 +11,4 @@ dependencies:
1111 - src: git https://github.com/nektro/zig-whatwg-url
1212 - src: git https://github.com/nektro/zig-net-http
1313 - src: git https://github.com/nektro/zig-nio
14 - src: git https://github.com/nektro/zig-json