authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-26 20:30:55 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-26 20:30:55 -07:00
logdaedcedeb7b2bf9720b851a23044f10ea1dbfeba
tree25ef798f5ee42d33c9e9858c6a88758666652d6d
parent9ccbbac07fbd2b1a809bfb0efee976fb32cd6827
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

use nio.fmt


2 files changed, 13 insertions(+), 11 deletions(-)

oauth2.zig+12-11
...@@ -7,6 +7,7 @@ const pek = @import("pek");...@@ -7,6 +7,7 @@ const pek = @import("pek");
7const extras = @import("extras");7const extras = @import("extras");
8const url = @import("url");8const url = @import("url");
9const http = @import("http");9const http = @import("http");
10const nio = @import("nio");
10const Base = @This();11const Base = @This();
1112
12pub const Provider = struct {13pub const Provider = struct {
...@@ -223,9 +224,9 @@ pub fn providerById(alloc: std.mem.Allocator, name: string) !?Provider {...@@ -223,9 +224,9 @@ pub fn providerById(alloc: std.mem.Allocator, name: string) !?Provider {
223 if (std.mem.eql(u8, didp.id, p_id)) {224 if (std.mem.eql(u8, didp.id, p_id)) {
224 return Provider{225 return Provider{
225 .id = name,226 .id = name,
226 .authorize_url = try std.fmt.allocPrint(alloc, didp.authorize_url, args),227 .authorize_url = try nio.fmt.allocPrint(alloc, didp.authorize_url, args),
227 .token_url = try std.fmt.allocPrint(alloc, didp.token_url, args),228 .token_url = try nio.fmt.allocPrint(alloc, didp.token_url, args),
228 .me_url = try std.fmt.allocPrint(alloc, didp.me_url, args),229 .me_url = try nio.fmt.allocPrint(alloc, didp.me_url, args),
229 .scope = didp.scope,230 .scope = didp.scope,
230 .name_prop = didp.name_prop,231 .name_prop = didp.name_prop,
231 .name_prefix = didp.name_prefix,232 .name_prefix = didp.name_prefix,
...@@ -239,9 +240,9 @@ pub fn providerById(alloc: std.mem.Allocator, name: string) !?Provider {...@@ -239,9 +240,9 @@ pub fn providerById(alloc: std.mem.Allocator, name: string) !?Provider {
239 if (std.mem.eql(u8, didp.id, p_id)) {240 if (std.mem.eql(u8, didp.id, p_id)) {
240 return Provider{241 return Provider{
241 .id = name,242 .id = name,
242 .authorize_url = try std.fmt.allocPrint(alloc, didp.authorize_url, args),243 .authorize_url = try nio.fmt.allocPrint(alloc, didp.authorize_url, args),
243 .token_url = try std.fmt.allocPrint(alloc, didp.token_url, args),244 .token_url = try nio.fmt.allocPrint(alloc, didp.token_url, args),
244 .me_url = try std.fmt.allocPrint(alloc, didp.me_url, args),245 .me_url = try nio.fmt.allocPrint(alloc, didp.me_url, args),
245 .scope = didp.scope,246 .scope = didp.scope,
246 .name_prop = didp.name_prop,247 .name_prop = didp.name_prop,
247 .name_prefix = didp.name_prefix,248 .name_prefix = didp.name_prefix,
...@@ -314,7 +315,7 @@ pub fn Handlers(comptime T: type) type {...@@ -314,7 +315,7 @@ pub fn Handlers(comptime T: type) type {
314 var req = try http_client.open(.POST, try std.Uri.parse(client.provider.token_url), .{315 var req = try http_client.open(.POST, try std.Uri.parse(client.provider.token_url), .{
315 .server_header_buffer = &buf,316 .server_header_buffer = &buf,
316 .headers = .{317 .headers = .{
317 .authorization = .{ .override = try std.fmt.allocPrint(alloc, "Basic {s}", .{try extras.base64EncodeAlloc(alloc, try std.mem.join(alloc, ":", &.{ client.id, client.secret }))}) },318 .authorization = .{ .override = try nio.fmt.allocPrint(alloc, "Basic {s}", .{try extras.base64EncodeAlloc(alloc, try std.mem.join(alloc, ":", &.{ client.id, client.secret }))}) },
318 .content_type = .{ .override = "application/x-www-form-urlencoded" },319 .content_type = .{ .override = "application/x-www-form-urlencoded" },
319 },320 },
320 .extra_headers = &.{321 .extra_headers = &.{
...@@ -341,7 +342,7 @@ pub fn Handlers(comptime T: type) type {...@@ -341,7 +342,7 @@ pub fn Handlers(comptime T: type) type {
341 var req2 = try http_client.open(.GET, try std.Uri.parse(client.provider.me_url), .{342 var req2 = try http_client.open(.GET, try std.Uri.parse(client.provider.me_url), .{
342 .server_header_buffer = &buf,343 .server_header_buffer = &buf,
343 .headers = .{344 .headers = .{
344 .authorization = .{ .override = try std.fmt.allocPrint(alloc, "Bearer {s}", .{at.string}) },345 .authorization = .{ .override = try nio.fmt.allocPrint(alloc, "Bearer {s}", .{at.string}) },
345 },346 },
346 .extra_headers = &.{347 .extra_headers = &.{
347 .{ .name = "Accept", .value = "application/json" },348 .{ .name = "Accept", .value = "application/json" },
...@@ -395,14 +396,14 @@ fn redirectUri(request_headers: *const http.HeadersMap, alloc: std.mem.Allocator...@@ -395,14 +396,14 @@ fn redirectUri(request_headers: *const http.HeadersMap, alloc: std.mem.Allocator
395 const maybe_tls = std.mem.eql(u8, xproto, "https");396 const maybe_tls = std.mem.eql(u8, xproto, "https");
396 const proto: string = if (maybe_tls) "https" else "http";397 const proto: string = if (maybe_tls) "https" else "http";
397 const host = request_headers.find("host").?;398 const host = request_headers.find("host").?;
398 return try std.fmt.allocPrint(alloc, "{s}://{s}{s}", .{ proto, host, callbackPath });399 return try nio.fmt.allocPrint(alloc, "{s}://{s}{s}", .{ proto, host, callbackPath });
399}400}
400401
401fn fixId(alloc: std.mem.Allocator, id: std.json.Value) !string {402fn fixId(alloc: std.mem.Allocator, id: std.json.Value) !string {
402 return switch (id) {403 return switch (id) {
403 .string => |v| v,404 .string => |v| v,
404 .integer => |v| try std.fmt.allocPrint(alloc, "{d}", .{v}),405 .integer => |v| try nio.fmt.allocPrint(alloc, "{d}", .{v}),
405 .float => |v| try std.fmt.allocPrint(alloc, "{d}", .{v}),406 .float => |v| try nio.fmt.allocPrint(alloc, "{d}", .{v}),
406 else => unreachable,407 else => unreachable,
407 };408 };
408}409}
zig.mod+1
...@@ -10,3 +10,4 @@ dependencies:...@@ -10,3 +10,4 @@ dependencies:
10 - src: git https://github.com/nektro/zig-extras10 - src: git https://github.com/nektro/zig-extras
11 - src: git https://github.com/nektro/zig-whatwg-url11 - src: git https://github.com/nektro/zig-whatwg-url
12 - src: git https://github.com/nektro/zig-net-http12 - src: git https://github.com/nektro/zig-net-http
13 - src: git https://github.com/nektro/zig-nio