From bd3a5452ae3f88dcefd030bef36915079062308e Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 31 May 2026 15:47:41 -0700 Subject: [PATCH] switch from extras.parse_json to zig-json --- licenses.txt | 3 +++ oauth2.zig | 33 +++++++++++++++++---------------- zig.mod | 1 + 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/licenses.txt b/licenses.txt index 0cff7fad996191b079b937e6a96c573aeda5759c..796c7bf198f4a8e81b1549633a1dba2fc40a9699 100644 --- a/licenses.txt +++ b/licenses.txt @@ -2,6 +2,8 @@ MIT: = https://spdx.org/licenses/MIT - This - git https://github.com/nektro/zig-extras +- git https://github.com/nektro/zig-json +- git https://github.com/nektro/zig-sys-darwin - git https://github.com/nektro/zig-sys-linux - git https://github.com/nektro/zig-time - git https://github.com/nektro/zig-tracer @@ -10,6 +12,7 @@ MIT: MPL-2.0: = https://spdx.org/licenses/MPL-2.0 +- git https://github.com/nektro/zig-intrusive-parser - git https://github.com/nektro/zig-net-http - git https://github.com/nektro/zig-net - git https://github.com/nektro/zig-nfs diff --git a/oauth2.zig b/oauth2.zig index db14bc5cecb01967136730cac7ad89eddd40d3ee..1fd71091ec389902e9590d2cad2b8f324273f8fe 100644 --- a/oauth2.zig +++ b/oauth2.zig @@ -8,6 +8,7 @@ const extras = @import("extras"); const url = @import("url"); const http = @import("http"); const nio = @import("nio"); +const json = @import("json"); const Base = @This(); pub const Provider = struct { @@ -332,17 +333,17 @@ pub fn Handlers(comptime T: type) type { const body_content = try req.reader().readAllAlloc(alloc, 1024 * 1024 * 5); if (req.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req.response.status), body_content }); if (req.response.status != .ok) return error.OauthBadToken; - const val = try extras.parse_json(alloc, body_content); - - const tt = val.value.object.get("token_type").?.string; + const val = try json.parseFromSlice(alloc, "body.json", body_content, .{ .maximum_depth = 100, .support_trailing_commas = true }); + val.acquire(); + const tt = val.root.object().getS("token_type").?; if (!std.mem.eql(u8, tt, "bearer")) return fail(response_status, body_writer, "oauth2: invalid token type: {s}", .{tt}); - - const at = val.value.object.get("access_token") orelse return try fail(response_status, body_writer, "Identity Provider Login Error!\n{s}", .{body_content}); + const at = val.root.object().getS("access_token") orelse return try fail(response_status, body_writer, "Identity Provider Login Error!\n{s}", .{body_content}); + val.release(); var req2 = try http_client.open(.GET, try std.Uri.parse(client.provider.me_url), .{ .server_header_buffer = &buf, .headers = .{ - .authorization = .{ .override = try nio.fmt.allocPrint(alloc, "Bearer {s}", .{at.string}) }, + .authorization = .{ .override = try nio.fmt.allocPrint(alloc, "Bearer {s}", .{at}) }, }, .extra_headers = &.{ .{ .name = "Accept", .value = "application/json" }, @@ -356,11 +357,12 @@ pub fn Handlers(comptime T: type) type { const body_content2 = try req2.reader().readAllAlloc(alloc, 1024 * 1024 * 5); if (req2.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req2.response.status), body_content2 }); if (req2.response.status != .ok) return error.OauthBadUserinfo; - const val2 = try extras.parse_json(alloc, body_content2); - - const id = try fixId(alloc, val2.value.object.get(client.provider.id_prop).?); - const name = val2.value.object.get(client.provider.name_prop).?.string; - try T.saveInfo(response_headers, alloc, client.provider, id, name, val.value, val2.value); + const val2 = try json.parseFromSlice(alloc, "body2.json", body_content2, .{ .maximum_depth = 100, .support_trailing_commas = true }); + val2.acquire(); + const id = try fixId(val2.root.object().getAny(client.provider.id_prop).?); + const name = val2.root.object().getS(client.provider.name_prop).?; + val2.release(); + try T.saveInfo(response_headers, alloc, client.provider, id, name, val, val2); try response_headers.append("location", T.doneUrl); response_status.* = .found; @@ -399,11 +401,10 @@ fn redirectUri(request_headers: *const http.HeadersMap, alloc: std.mem.Allocator return try nio.fmt.allocPrint(alloc, "{s}://{s}{s}", .{ proto, host, callbackPath }); } -fn fixId(alloc: std.mem.Allocator, id: std.json.Value) !string { - return switch (id) { - .string => |v| v, - .integer => |v| try nio.fmt.allocPrint(alloc, "{d}", .{v}), - .float => |v| try nio.fmt.allocPrint(alloc, "{d}", .{v}), +fn fixId(id: json.ValueIndex) !string { + return switch (id.v()) { + .string => |v| v.to(), + .number => |v| v.to(), else => unreachable, }; } diff --git a/zig.mod b/zig.mod index 1d3493174564c31278e07823a6f7d570817c4e05..cc1473b87db0e338c02b190ab10b1d27fdcfb2db 100644 --- a/zig.mod +++ b/zig.mod @@ -11,3 +11,4 @@ dependencies: - src: git https://github.com/nektro/zig-whatwg-url - src: git https://github.com/nektro/zig-net-http - src: git https://github.com/nektro/zig-nio + - src: git https://github.com/nektro/zig-json -- 2.54.0