authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-03-04 03:55:15 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-03-04 03:55:15 -08:00
loga017ade68e7be106b1c8b9cc3b72f5fb2f92dfe8
treec363fac5a4237f8f4d3ace8dc754d08bf128fbea
parent3a4d58d06bdfbe4e6adb1219c580d07adb8a3488
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

switch to using nektro/zig-net-http


2 files changed, 6 insertions(+), 4 deletions(-)

oauth2.zig+5-4
...@@ -7,6 +7,7 @@ const pek = @import("pek");...@@ -7,6 +7,7 @@ const pek = @import("pek");
7const zfetch = @import("zfetch");7const zfetch = @import("zfetch");
8const extras = @import("extras");8const extras = @import("extras");
9const url = @import("url");9const url = @import("url");
10const http = @import("http");
10const Base = @This();11const Base = @This();
1112
12pub const Provider = struct {13pub const Provider = struct {
...@@ -254,7 +255,7 @@ pub fn Handlers(comptime T: type) type {...@@ -254,7 +255,7 @@ pub fn Handlers(comptime T: type) type {
254 const Self = @This();255 const Self = @This();
255 pub var clients: []Client = &.{};256 pub var clients: []Client = &.{};
256257
257 pub fn login(request: *std.http.Server.Request, body_writer: anytype, alloc: std.mem.Allocator, query: url.SearchParams, request_headers: *const std.StringHashMapUnmanaged(string), response_status: *std.http.Status, response_headers: *std.ArrayListUnmanaged(std.http.Header)) !void {258 pub fn login(request: *http.ServerRequest, body_writer: anytype, alloc: std.mem.Allocator, query: url.SearchParams, request_headers: *const std.StringHashMapUnmanaged(string), response_status: *http.Status, response_headers: *std.ArrayListUnmanaged(std.http.Header)) !void {
258 if (query.get("with")) |with| {259 if (query.get("with")) |with| {
259 const client = clientByProviderId(Self.clients, with) orelse return try fail(response_status, body_writer, "Client with that ID not found!\n", .{});260 const client = clientByProviderId(Self.clients, with) orelse return try fail(response_status, body_writer, "Client with that ID not found!\n", .{});
260 return try loginOne(request, alloc, T, client, T.callbackPath, request_headers, response_status, response_headers);261 return try loginOne(request, alloc, T, client, T.callbackPath, request_headers, response_status, response_headers);
...@@ -271,7 +272,7 @@ pub fn Handlers(comptime T: type) type {...@@ -271,7 +272,7 @@ pub fn Handlers(comptime T: type) type {
271 });272 });
272 }273 }
273274
274 pub fn callback(request: *std.http.Server.Request, body_writer: anytype, alloc: std.mem.Allocator, query: url.SearchParams, request_headers: *const std.StringHashMapUnmanaged(string), response_status: *std.http.Status, response_headers: *std.ArrayListUnmanaged(std.http.Header)) !void {275 pub fn callback(request: *http.ServerRequest, body_writer: anytype, alloc: std.mem.Allocator, query: url.SearchParams, request_headers: *const std.StringHashMapUnmanaged(string), response_status: *http.Status, response_headers: *std.ArrayListUnmanaged(std.http.Header)) !void {
275 _ = request;276 _ = request;
276 const state = query.get("state") orelse return try fail(response_status, body_writer, "", .{});277 const state = query.get("state") orelse return try fail(response_status, body_writer, "", .{});
277 const client = clientByProviderId(Self.clients, state) orelse return try fail(response_status, body_writer, "error: No handler found for provider: {s}\n", .{state});278 const client = clientByProviderId(Self.clients, state) orelse return try fail(response_status, body_writer, "error: No handler found for provider: {s}\n", .{state});
...@@ -326,7 +327,7 @@ pub fn Handlers(comptime T: type) type {...@@ -326,7 +327,7 @@ pub fn Handlers(comptime T: type) type {
326 };327 };
327}328}
328329
329fn loginOne(request: *std.http.Server.Request, alloc: std.mem.Allocator, comptime T: type, client: Client, callbackPath: string, request_headers: *const std.StringHashMapUnmanaged(string), response_status: *std.http.Status, response_headers: *std.ArrayListUnmanaged(std.http.Header)) !void {330fn loginOne(request: *http.ServerRequest, alloc: std.mem.Allocator, comptime T: type, client: Client, callbackPath: string, request_headers: *const std.StringHashMapUnmanaged(string), response_status: *http.Status, response_headers: *std.ArrayListUnmanaged(std.http.Header)) !void {
330 if (try T.isLoggedIn(request, alloc)) {331 if (try T.isLoggedIn(request, alloc)) {
331 try response_headers.append(alloc, .{ .name = "Location", .value = T.doneUrl });332 try response_headers.append(alloc, .{ .name = "Location", .value = T.doneUrl });
332 } else {333 } else {
...@@ -344,7 +345,7 @@ fn loginOne(request: *std.http.Server.Request, alloc: std.mem.Allocator, comptim...@@ -344,7 +345,7 @@ fn loginOne(request: *std.http.Server.Request, alloc: std.mem.Allocator, comptim
344 response_status.* = .found;345 response_status.* = .found;
345}346}
346347
347fn fail(response_status: *std.http.Status, body_writer: anytype, comptime err: string, args: anytype) !void {348fn fail(response_status: *http.Status, body_writer: anytype, comptime err: string, args: anytype) !void {
348 response_status.* = .bad_request;349 response_status.* = .bad_request;
349 try body_writer.print(err, args);350 try body_writer.print(err, args);
350}351}
zig.mod+1
...@@ -10,3 +10,4 @@ dependencies:...@@ -10,3 +10,4 @@ dependencies:
10 - src: git https://github.com/nektro/zfetch10 - src: git https://github.com/nektro/zfetch
11 - src: git https://github.com/nektro/zig-extras11 - src: git https://github.com/nektro/zig-extras
12 - src: git https://github.com/nektro/zig-whatwg-url12 - src: git https://github.com/nektro/zig-whatwg-url
13 - src: git https://github.com/nektro/zig-net-http