| ... | ... | @@ -8,6 +8,7 @@ const extras = @import("extras"); |
| 8 | 8 | const url = @import("url"); |
| 9 | 9 | const http = @import("http"); |
| 10 | 10 | const nio = @import("nio"); |
| 11 | const json = @import("json"); |
| 11 | 12 | const Base = @This(); |
| 12 | 13 | |
| 13 | 14 | pub const Provider = struct { |
| ... | ... | @@ -332,17 +333,17 @@ pub fn Handlers(comptime T: type) type { |
| 332 | 333 | const body_content = try req.reader().readAllAlloc(alloc, 1024 * 1024 * 5); |
| 333 | 334 | if (req.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req.response.status), body_content }); |
| 334 | 335 | 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").?; |
| 338 | 339 | 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(); |
| 341 | 342 | |
| 342 | 343 | var req2 = try http_client.open(.GET, try std.Uri.parse(client.provider.me_url), .{ |
| 343 | 344 | .server_header_buffer = &buf, |
| 344 | 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 | 348 | .extra_headers = &.{ |
| 348 | 349 | .{ .name = "Accept", .value = "application/json" }, |
| ... | ... | @@ -356,11 +357,12 @@ pub fn Handlers(comptime T: type) type { |
| 356 | 357 | const body_content2 = try req2.reader().readAllAlloc(alloc, 1024 * 1024 * 5); |
| 357 | 358 | if (req2.response.status != .ok) std.log.scoped(.oauth).debug("{s}: {s}", .{ @tagName(req2.response.status), body_content2 }); |
| 358 | 359 | 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); |
| 364 | 366 | |
| 365 | 367 | try response_headers.append("location", T.doneUrl); |
| 366 | 368 | response_status.* = .found; |
| ... | ... | @@ -399,11 +401,10 @@ fn redirectUri(request_headers: *const http.HeadersMap, alloc: std.mem.Allocator |
| 399 | 401 | return try nio.fmt.allocPrint(alloc, "{s}://{s}{s}", .{ proto, host, callbackPath }); |
| 400 | 402 | } |
| 401 | 403 | |
| 402 | | fn 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}), |
| 404 | fn fixId(id: json.ValueIndex) !string { |
| 405 | return switch (id.v()) { |
| 406 | .string => |v| v.to(), |
| 407 | .number => |v| v.to(), |
| 407 | 408 | else => unreachable, |
| 408 | 409 | }; |
| 409 | 410 | } |