| ... | @@ -7,6 +7,7 @@ const pek = @import("pek"); | ... | @@ -7,6 +7,7 @@ const pek = @import("pek"); |
| 7 | const zfetch = @import("zfetch"); | 7 | const zfetch = @import("zfetch"); |
| 8 | const extras = @import("extras"); | 8 | const extras = @import("extras"); |
| 9 | const url = @import("url"); | 9 | const url = @import("url"); |
| | 10 | const http = @import("http"); |
| 10 | const Base = @This(); | 11 | const Base = @This(); |
| 11 | | 12 | |
| 12 | pub const Provider = struct { | 13 | pub 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 = &.{}; |
| 256 | | 257 | |
| 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 | } |
| 273 | | 274 | |
| 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 | } |
| 328 | | 329 | |
| 329 | fn 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 { | 330 | fn 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 | } |
| 346 | | 347 | |
| 347 | fn fail(response_status: *std.http.Status, body_writer: anytype, comptime err: string, args: anytype) !void { | 348 | fn 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 | } |