//! HTML for the OAuth sign-in, account picker, consent and error cards. Every //! dynamic value is escaped here; the handlers in oauth.zig decide which card //! a request sees. Styles and behavior live in src/internal/web. const std = @import("std"); const http_api = @import("../../http/api.zig"); const icons = @import("../../internal/web/icons.zig"); const skin = @import("../../internal/web/skin.zig"); const scope_display = @import("scope_display.zig"); const http = std.http; const appearance_js = @embedFile("../../internal/web/appearance.js"); const appearance_css = @embedFile("../../internal/web/appearance.css"); const page_css = @embedFile("../../internal/web/authorize.css"); const page_js = @embedFile("../../internal/web/authorize.js"); const license = @embedFile("../../internal/web/lucide-license.html"); /// Who is asking, as the person should judge it: by domain. pub const Client = struct { /// The domain, the full client_id URL, or "An app on your device". label: []const u8, client_id: []const u8, /// Self-declared and unverified; only shown in the details dialog. name: ?[]const u8 = null, icon_url: ?[]const u8 = null, tos_uri: ?[]const u8 = null, policy_uri: ?[]const u8 = null, }; pub const Account = struct { handle: []const u8, display_name: ?[]const u8 = null, avatar_url: ?[]const u8 = null, has_passkeys: bool = false, }; pub const Common = struct { /// This server's host, shown at the top of every card. host: []const u8, request_uri: []const u8, /// `/oauth/authorize?request_uri=…`, for links between cards. page_url: []const u8, client: Client, }; pub const SignIn = struct { username: []const u8 = "", placeholder: []const u8, remember: bool = false, err: ?[]const u8 = null, }; pub const Known = struct { account: Account, password_open: bool = false, remember: bool = false, err: ?[]const u8 = null, }; pub const Consent = struct { account: Account, summary: scope_display.Summary, /// The exact scope string, for "Copy raw scopes". scope: []const u8, /// Handles for the accounts named as space authorities, where local. authorities: []const Authority = &.{}, csrf: []const u8, }; pub const Authority = struct { did: []const u8, handle: []const u8 }; const Writer = std.Io.Writer; pub fn signIn(allocator: std.mem.Allocator, common: Common, view: SignIn) ![]const u8 { var out: Writer.Allocating = .init(allocator); const w = &out.writer; try head(w, "Sign in", common.host); try w.writeAll("
"); try mark(w, common.host); try w.writeAll("

Sign in

"); try continueTo(w, common.client); try w.writeAll("
"); try hidden(w, "request_uri", common.request_uri); try w.writeAll(""); try w.writeAll("

or use your password

"); try w.print( \\
, .{ esc(view.username), esc(view.placeholder) }); try passwordField(w); try w.writeAll("

Only enter your password on sites you trust.

"); try errorLine(w, view.err); try statusLine(w); try rememberBox(w, view.remember); try w.writeAll(""); try cancel(w, "Cancel"); try w.writeAll("
" ++ passkey_offer ++ "
"); try tail(w); return out.toOwnedSlice(); } pub fn known(allocator: std.mem.Allocator, common: Common, view: Known) ![]const u8 { var out: Writer.Allocating = .init(allocator); const w = &out.writer; try head(w, "Sign in", common.host); try w.writeAll("
"); try mark(w, common.host); try w.writeAll("

Sign in

"); try continueTo(w, common.client); try w.writeAll("
"); try hidden(w, "request_uri", common.request_uri); try hidden(w, "username", view.account.handle); try w.writeAll("
"); try accountMedia(w, view.account, ""); try accountText(w, view.account); try w.writeAll("
"); if (view.account.has_passkeys) { try w.print("", .{esc(view.account.handle)}); } try w.print("" ++ key_art ++ "Use password", .{ if (view.account.has_passkeys) "" else " primary", esc(common.page_url), if (view.password_open) " hidden" else "", }); try w.print("
", .{if (view.password_open) "" else " hidden"}); try passwordField(w); try w.writeAll("

Only enter your password on sites you trust.

"); try errorLine(w, view.err); try rememberBox(w, view.remember); try w.writeAll("
"); try statusLine(w); try cancel(w, "Cancel"); try w.writeAll("
"); if (!view.account.has_passkeys) try w.writeAll(passkey_offer); try w.print("Not you? Use another account", .{esc(common.page_url)}); try w.writeAll("
"); try tail(w); return out.toOwnedSlice(); } pub fn picker(allocator: std.mem.Allocator, common: Common, account: Account, csrf: []const u8) ![]const u8 { var out: Writer.Allocating = .init(allocator); const w = &out.writer; try head(w, "Choose an account", common.host); try w.writeAll("
"); try mark(w, common.host); try w.print("

Continue to {f}

", .{esc(common.client.label)}); try w.writeAll("
"); try w.print("", .{esc(common.page_url)}); try accountMedia(w, account, ""); try accountText(w, account); try w.writeAll(chevron ++ ""); try w.print("" ++ person_art ++ "Another account" ++ chevron ++ "", .{esc(common.page_url)}); try w.writeAll("
"); try hidden(w, "request_uri", common.request_uri); try hidden(w, "csrf", csrf); try cancel(w, "Cancel"); try w.writeAll(""); try w.writeAll("
"); try tail(w); return out.toOwnedSlice(); } pub fn consent(allocator: std.mem.Allocator, common: Common, view: Consent) ![]const u8 { var out: Writer.Allocating = .init(allocator); const w = &out.writer; try head(w, "Authorize", common.host); try w.writeAll("
"); try mark(w, common.host); try w.writeAll("

Authorize

"); try accountMedia(w, view.account, ""); try w.print("as @{f}

", .{esc(view.account.handle)}); try w.writeAll("
"); try appMedia(w, common.client); try w.print("{f}wants access to your account", .{esc(common.client.label)}); try w.writeAll("
"); if (view.summary.full_identity) { try w.writeAll("

" ++ warning_icon ++ "This app asks for full control of your identity. It could permanently break or take over your account. Only allow apps you trust completely.

"); } try w.writeAll("

It will be able to

"); try terms(w, common.client); try w.writeAll("
"); try hidden(w, "request_uri", common.request_uri); try hidden(w, "csrf", view.csrf); try w.writeAll(""); try w.writeAll("
"); try detailsDialog(w, common.client, view); try w.writeAll("
"); try tail(w); return out.toOwnedSlice(); } fn appMedia(w: *Writer, client: Client) !void { if (client.icon_url) |url| { try w.print("" ++ apps_art ++ "", .{esc(url)}); } else { try w.writeAll("" ++ apps_art ++ ""); } } /// The return page navigates on load and offers a manual link if it stays open. pub fn returning(allocator: std.mem.Allocator, common: Common, location: []const u8) ![]const u8 { var out: Writer.Allocating = .init(allocator); const w = &out.writer; try head(w, "Returning to the app", common.host); try w.writeAll("
"); try mark(w, common.host); try w.writeAll("
zds"); try appMedia(w, common.client); try w.writeAll("
"); try w.print("

Returning to {f}

", .{esc(common.client.label)}); try w.print( "" ++ "", .{ esc(location), esc(location) }, ); try w.writeAll("
"); try tail(w); return out.toOwnedSlice(); } /// A card for requests that cannot continue: unknown, expired or used. pub fn failure(allocator: std.mem.Allocator, host: []const u8, title: []const u8, message: []const u8, code: []const u8) ![]const u8 { var out: Writer.Allocating = .init(allocator); const w = &out.writer; try head(w, title, host); try w.writeAll("
"); try mark(w, host); try w.print("

{f}

{f}

{f}

", .{ esc(title), esc(message), esc(code) }); try w.writeAll("
"); try tail(w); return out.toOwnedSlice(); } pub fn respond(request: *http_api.Request, status: http.Status, body: []const u8) !void { const headers = [_]http.Header{ .{ .name = "content-type", .value = "text/html; charset=utf-8" }, .{ .name = "cache-control", .value = "no-store" }, .{ .name = "access-control-allow-origin", .value = "*" }, .{ .name = "connection", .value = "close" }, } ++ http_api.htmlSecurityHeaders(.{ .scripts = &([_][]const u8{ appearance_js, page_js, icons.script } ++ skin.policy_scripts), .styles = &([_][]const u8{ appearance_css, page_css, icons.style } ++ skin.authorize_policy_styles), }); var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); try http_api.respond(request, status, try icons.decorate(arena.allocator(), body), &headers); } fn head(w: *Writer, title: []const u8, host: []const u8) !void { try w.print( \\ \\ \\ \\ \\ \\ \\{f} · {f} \\ \\{s} \\ \\ \\{s} , .{ esc(title), esc(host), appearance_js, appearance_css, page_css, skin.authorizeStyle(), license }); } fn tail(w: *Writer) !void { try w.writeAll(""); try w.writeAll(skin.script()); try w.writeAll("\n\n\n"); } fn mark(w: *Writer, host: []const u8) !void { try w.print("{f}", .{esc(host)}); } fn continueTo(w: *Writer, client: Client) !void { try w.print("

to continue to {f}

", .{esc(client.label)}); } fn hidden(w: *Writer, name: []const u8, value: []const u8) !void { try w.print("", .{ name, esc(value) }); } fn passwordField(w: *Writer) !void { try w.writeAll("
"); } fn rememberBox(w: *Writer, checked: bool) !void { try w.print("", .{if (checked) " checked" else ""}); } fn cancel(w: *Writer, text: []const u8) !void { try w.print("", .{text}); } fn errorLine(w: *Writer, err: ?[]const u8) !void { const message = err orelse return; try w.print("

" ++ error_icon ++ "{f}

", .{esc(message)}); } /// Where passkey outcomes are reported without leaving the page. fn statusLine(w: *Writer) !void { try w.writeAll(""); } fn accountMedia(w: *Writer, account: Account, extra_class: []const u8) !void { if (account.avatar_url) |url| { try w.print("" ++ person_art ++ "", .{ extra_class, esc(url) }); } else { try w.print("" ++ person_art ++ "", .{extra_class}); } } fn accountText(w: *Writer, account: Account) !void { try w.print("{f}@{f}", .{ esc(account.display_name orelse account.handle), esc(account.handle), }); } fn terms(w: *Writer, client: Client) !void { const tos = safeLink(client.tos_uri); const policy = safeLink(client.policy_uri); if (tos == null and policy == null) return; try w.writeAll("

Subject to the app's "); if (tos) |url| try w.print("terms", .{esc(url)}); if (tos != null and policy != null) try w.writeAll(" and "); if (policy) |url| try w.print("privacy policy", .{esc(url)}); try w.writeAll(".

"); } fn safeLink(uri: ?[]const u8) ?[]const u8 { const value = uri orelse return null; if (std.ascii.startsWithIgnoreCase(value, "https://") or std.ascii.startsWithIgnoreCase(value, "http://")) return value; return null; } /// The exact request: a bottom sheet on phones and a dialog on wider screens. fn detailsDialog(w: *Writer, client: Client, view: Consent) !void { const summary = view.summary; try w.writeAll("
"); try appMedia(w, client); try w.print("{f}Requested permissions", .{esc(client.label)}); try w.writeAll("
"); try w.print("

App

Client ID
{f}
", .{breakable(client.client_id)}); if (client.name) |name| try w.print("
Calls itself
{f}
", .{esc(name)}); try w.writeAll("
"); if (summary.sets.len > 0) { try w.writeAll("

Permission sets

"); for (summary.sets) |set| { try w.print("
{f}{f}", .{ esc(set.title), nsid(set.nsid) }); if (set.detail.len > 0) try w.print("

{f}

", .{esc(set.detail)}); if (set.expanded.len > 0) { try w.print("
Includes {d} scope{s}", .{ set.expanded.len, if (set.expanded.len == 1) "" else "s" }); for (set.expanded) |scope| try w.print("{f}", .{breakable(scope)}); try w.writeAll("
"); } try w.writeAll("
"); } try w.writeAll("
"); } if (summary.records.len > 0 or summary.any_records != null) { try w.writeAll("

Records in your repo

"); if (summary.any_records) |verbs| try w.print("
{f}any record type
", .{esc(verbs)}); for (summary.records) |line| { try w.print("
{f}", .{esc(line.verbs)}); for (line.collections) |collection| try w.print("{f}", .{nsid(collection)}); try w.writeAll("
"); } try w.writeAll("
"); } if (summary.files.len > 0) { try w.writeAll("

Files

Upload"); for (summary.files) |kind| try w.print("{f}", .{esc(kind)}); try w.writeAll("
"); } if (summary.spaces.len > 0) { try w.writeAll("

Spaces

"); for (summary.spaces) |space| { try w.print("
{f}
", .{nsidBold(space.nsid)}); for (space.access) |access| try spaceAccess(w, access, view.authorities); try w.writeAll("
"); } try w.writeAll("

Viewing a space covers all of it; writes are limited to the record types listed. Only the owner decides who else can read.

"); } if (summary.services.len > 0) { try w.writeAll("

Services

"); for (summary.services) |call| { if (std.mem.eql(u8, call.aud, "*")) { try w.writeAll("
Any service"); } else { try w.print("
{f}", .{breakable(call.aud)}); } for (call.methods) |method| try w.print("{f}", .{nsid(method)}); try w.writeAll("
"); } try w.writeAll("
"); } if (summary.other.len > 0) { try w.writeAll("

Other

"); for (summary.other) |scope| try w.print("{f}", .{breakable(scope)}); try w.writeAll("
"); } try w.print("
", .{esc(view.scope)}); try w.writeAll("
"); } fn spaceAccess(w: *Writer, access: scope_display.SpaceAccess, authorities: []const Authority) !void { switch (access.owner) { .you => { try w.writeAll("
You"); const key = access.key orelse "any key"; if (std.mem.eql(u8, key, "any key")) try w.writeAll("any key") else try w.print("key {f}", .{esc(key)}); try w.writeAll("
"); }, .anyone => try w.writeAll("
Anyone'swhere the owner gave you access
"), .account => { const handle = for (authorities) |authority| { if (std.mem.eql(u8, authority.did, access.authority)) break authority.handle; } else null; if (handle) |name| { try w.print("
@{f}their space
", .{esc(name)}); } else { try w.print("
One account's{f}
", .{breakable(access.authority)}); } }, } try w.writeAll(""); } /// An NSID with its namespace muted: `fm.plyr.` then `track`. const Nsid = struct { value: []const u8, bold: bool, pub fn format(self: Nsid, w: *Writer) Writer.Error!void { const dot = std.mem.lastIndexOfScalar(u8, self.value, '.') orelse return (Escaped{ .value = self.value }).format(w); try w.writeAll(""); try (Escaped{ .value = self.value[0 .. dot + 1] }).format(w); try w.writeAll(if (self.bold) "" else ""); try (Escaped{ .value = self.value[dot + 1 ..] }).format(w); if (self.bold) try w.writeAll(""); } }; fn nsid(value: []const u8) Nsid { return .{ .value = value, .bold = false }; } fn nsidBold(value: []const u8) Nsid { return .{ .value = value, .bold = true }; } /// Escaped text that may wrap after `/`, `?`, `&` and `=`, so a long scope or /// URL breaks between its parts and an NSID stays on one line where it fits. const Breakable = struct { value: []const u8, pub fn format(self: Breakable, w: *Writer) Writer.Error!void { for (self.value) |c| { try (Escaped{ .value = &.{c} }).format(w); switch (c) { '/', '?', '&', '=' => try w.writeAll(""), else => {}, } } } }; fn breakable(value: []const u8) Breakable { return .{ .value = value }; } /// Each permission row's icon is an operator-replaceable `zds.consent.*` slot. fn rowIcon(icon: scope_display.Icon) []const u8 { return switch (icon) { .records => consentSlot("records", ""), .files => consentSlot("files", ""), .space => consentSlot("space", ""), .service => consentSlot("service", ""), .read => consentSlot("read", ""), .report => consentSlot("report", ""), .permission_set => consentSlot("permission-set", ""), .account => consentSlot("account", ""), .identity => consentSlot("identity", ""), }; } fn consentSlot(comptime name: []const u8, comptime body: []const u8) []const u8 { return "" ++ body ++ ""; } fn svg(comptime body: []const u8) []const u8 { return "" ++ body ++ ""; } fn slot(comptime name: []const u8, comptime class: []const u8, comptime body: []const u8) []const u8 { return "" ++ body ++ ""; } // The account portal's slots and default artwork, so an operator's drawings // for passkeys, app passwords, apps and the account appear here too. /// Points an account without passkeys at where to add one; the sign-in in /// progress stays in this tab. const passkey_offer = "

" ++ passkey_art ++ "Sign in with a passkey next time. Add one

"; const passkey_art = slot("zds.account.passkeys", "art", ""); const key_art = slot("zds.account.app-passwords", "art", ""); const apps_art = slot("zds.nav.apps", "art", ""); const person_art = slot("zds.nav.account", "art", ""); const chevron = ""; const error_icon = ""; const close_icon = ""; const info_icon = ""; const warning_icon = ""; const Escaped = struct { value: []const u8, pub fn format(self: Escaped, w: *Writer) Writer.Error!void { for (self.value) |c| switch (c) { '&' => try w.writeAll("&"), '<' => try w.writeAll("<"), '>' => try w.writeAll(">"), '"' => try w.writeAll("""), '\'' => try w.writeAll("'"), else => try w.writeByte(c), }; } }; fn esc(value: []const u8) Escaped { return .{ .value = value }; } const test_common: Common = .{ .host = "pds.example", .request_uri = "urn:ietf:params:oauth:request_uri:req", .page_url = "/oauth/authorize?request_uri=urn:ietf:params:oauth:request_uri:req", .client = .{ .label = "app.example", .client_id = "https://app.example/oauth-client-metadata.json", .name = "evil" }, }; test "the sign-in card keeps the keyboard down and the form contract" { const html = try signIn(std.testing.allocator, test_common, .{ .placeholder = "yourname.pds.example", .username = "\">