| author | |
| committer | |
| log | b9eee6396695f32b59a9eada10524e488c0d60c1 |
| tree | 4f5d3be9689d1c3071d5d413e6a6c39e8998a28c |
| parent | 9153a5a3b11eec15482d9b610a607a55f7e67a35 |
| signature |
5 files changed, 37 insertions(+), 10 deletions(-)
README.md+1-1| ... | ... | @@ -3,7 +3,7 @@ |
| 3 | 3 |  |
| 4 | 4 | [](https://github.com/nektro/zig-oauth2/blob/master/LICENSE) |
| 5 | 5 | [](https://github.com/sponsors/nektro) |
| 6 | [](https://ziglang.org/) | |
| 6 | [](https://ziglang.org/) | |
| 7 | 7 | [](https://github.com/nektro/zigmod) |
| 8 | 8 | |
| 9 | 9 | HTTP handler functions to allow you to easily add OAuth2 login support to your Zig application. |
build.zig+1-1| ... | ... | @@ -14,7 +14,7 @@ pub fn build(b: *std.Build) void { |
| 14 | 14 | }), |
| 15 | 15 | }); |
| 16 | 16 | deps.addAllTo(tests); |
| 17 | tests.linkLibC(); | |
| 17 | tests.root_module.link_libc = true; | |
| 18 | 18 | tests.use_llvm = !disable_llvm; |
| 19 | 19 | tests.use_lld = !disable_llvm; |
| 20 | 20 | b.getInstallStep().dependOn(&tests.step); |
oauth2.zig+4-6| ... | ... | @@ -9,6 +9,8 @@ const url = @import("url"); |
| 9 | 9 | const http = @import("http"); |
| 10 | 10 | const nio = @import("nio"); |
| 11 | 11 | const json = @import("json"); |
| 12 | const builtin = @import("builtin"); | |
| 13 | const root = @import("root"); | |
| 12 | 14 | const Base = @This(); |
| 13 | 15 | |
| 14 | 16 | pub const Provider = struct { |
| ... | ... | @@ -266,11 +268,6 @@ pub fn clientByProviderId(clients: []const Client, name: string) ?Client { |
| 266 | 268 | } |
| 267 | 269 | |
| 268 | 270 | pub fn Handlers(comptime T: type) type { |
| 269 | comptime std.debug.assert(@hasDecl(T, "isLoggedIn")); | |
| 270 | comptime std.debug.assert(@hasDecl(T, "doneUrl")); | |
| 271 | comptime std.debug.assert(@hasDecl(T, "saveInfo")); | |
| 272 | comptime std.debug.assert(@hasDecl(T, "callbackPath")); | |
| 273 | ||
| 274 | 271 | return struct { |
| 275 | 272 | const Self = @This(); |
| 276 | 273 | pub var clients: []Client = &.{}; |
| ... | ... | @@ -298,8 +295,9 @@ pub fn Handlers(comptime T: type) type { |
| 298 | 295 | const client = clientByProviderId(Self.clients, state) orelse return try fail(response_status, body_writer, "error: No handler found for provider: {s}\n", .{state}); |
| 299 | 296 | const code = query.get("code") orelse return try fail(response_status, body_writer, "", .{}); |
| 300 | 297 | |
| 298 | const io = if (!builtin.is_test) root.io else std.Options.debug_io; | |
| 301 | 299 | var buf: [4096]u8 = @splat(0); |
| 302 | var http_client: std.http.Client = .{ .allocator = alloc }; | |
| 300 | var http_client: std.http.Client = .{ .allocator = alloc, .io = io }; | |
| 303 | 301 | defer http_client.deinit(); |
| 304 | 302 | |
| 305 | 303 | var params = url.SearchParams.init(alloc); |
test.zig+27-2| ... | ... | @@ -1,7 +1,32 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const oauth2 = @import("oauth2"); |
| 3 | const nio = @import("nio"); | |
| 4 | const http = @import("http"); | |
| 5 | const json = @import("json"); | |
| 3 | 6 | |
| 4 | // TODO: how do we make this better? | |
| 5 | 7 | test { |
| 6 | std.testing.refAllDeclsRecursive(oauth2); | |
| 8 | _ = &oauth2.Provider; | |
| 9 | _ = &oauth2.Client; | |
| 10 | std.testing.refAllDecls(oauth2.providers); | |
| 11 | std.testing.refAllDecls(oauth2.dynamic_providers); | |
| 12 | _ = &oauth2.providerById; | |
| 13 | _ = &oauth2.clientByProviderId; | |
| 14 | _ = &oauth2.pek_domain; | |
| 15 | } | |
| 16 | test { | |
| 17 | const S = struct { | |
| 18 | pub const callbackPath = "/-/callback"; | |
| 19 | pub const doneUrl = "/"; | |
| 20 | ||
| 21 | pub fn isLoggedIn(_: *http.ServerRequest, _: std.mem.Allocator) !bool { | |
| 22 | return false; | |
| 23 | } | |
| 24 | pub fn saveInfo(_: *http.HeadersMap, _: std.mem.Allocator, _: oauth2.Provider, _: []const u8, _: []const u8, _: json.Document, _: json.Document) !void { | |
| 25 | // | |
| 26 | } | |
| 27 | }; | |
| 28 | const H = oauth2.Handlers(S); | |
| 29 | std.testing.refAllDecls(oauth2.Handlers(S)); | |
| 30 | _ = @TypeOf(H.login(undefined, nio.NullWriter{}, undefined, undefined, undefined, undefined, undefined)); | |
| 31 | _ = @TypeOf(H.callback(undefined, nio.NullWriter{}, undefined, undefined, undefined, undefined, undefined)); | |
| 7 | 32 | } |
zig.mod+4| ... | ... | @@ -12,3 +12,7 @@ dependencies: |
| 12 | 12 | - src: git https://github.com/nektro/zig-net-http |
| 13 | 13 | - src: git https://github.com/nektro/zig-nio |
| 14 | 14 | - src: git https://github.com/nektro/zig-json |
| 15 | root_dependencies: | |
| 16 | - src: git https://github.com/nektro/zig-nio | |
| 17 | - src: git https://github.com/nektro/zig-net-http | |
| 18 | - src: git https://github.com/nektro/zig-json |