zat.dev/zds
atproto pds · mirror of https://tangled.org/zat.dev/zds
zds 0.25.1 Latest 2 packages, all images
0
0
//! 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("<main class=\"card\">");
try mark(w, common.host);
try w.writeAll("<h1>Sign in</h1>");
try continueTo(w, common.client);
try w.writeAll("<form class=\"stack\" method=\"post\" action=\"/oauth/authorize\">");
try hidden(w, "request_uri", common.request_uri);
try w.writeAll("<button class=\"btn primary passkey\" type=\"button\" data-passkey>" ++ passkey_art ++ "<span class=\"label\">Sign in with a passkey</span></button>");
try w.writeAll("<p class=\"or\">or use your password</p>");
try w.print(
\\<div class="field"><label for="username">Username</label><div class="input"><span aria-hidden="true">@</span><input id="username" name="username" value="{f}" placeholder="{f}" autocomplete="username webauthn" autocapitalize="none" autocorrect="off" spellcheck="false" required></div></div>
, .{ esc(view.username), esc(view.placeholder) });
try passwordField(w);
try w.writeAll("<p class=\"note\">Only enter your password on sites you trust.</p>");
try errorLine(w, view.err);
try statusLine(w);
try rememberBox(w, view.remember);
try w.writeAll("<button class=\"btn\">Sign in</button>");
try cancel(w, "Cancel");
try w.writeAll("</form>" ++ passkey_offer ++ "</main>");
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("<main class=\"card\">");
try mark(w, common.host);
try w.writeAll("<h1>Sign in</h1>");
try continueTo(w, common.client);
try w.writeAll("<form class=\"stack\" method=\"post\" action=\"/oauth/authorize\">");
try hidden(w, "request_uri", common.request_uri);
try hidden(w, "username", view.account.handle);
try w.writeAll("<div class=\"row\">");
try accountMedia(w, view.account, "");
try accountText(w, view.account);
try w.writeAll("</div>");
if (view.account.has_passkeys) {
try w.print("<button class=\"btn primary passkey\" type=\"button\" data-passkey data-identifier=\"{f}\">" ++ passkey_art ++ "<span class=\"label\">Continue with passkey</span></button>", .{esc(view.account.handle)});
}
try w.print("<a class=\"btn{s}\" href=\"{f}&step=password\" data-reveal-password{s}>" ++ key_art ++ "Use password</a>", .{
if (view.account.has_passkeys) "" else " primary",
esc(common.page_url),
if (view.password_open) " hidden" else "",
});
try w.print("<div class=\"stack\" id=\"password-step\"{s}>", .{if (view.password_open) "" else " hidden"});
try passwordField(w);
try w.writeAll("<p class=\"note\">Only enter your password on sites you trust.</p>");
try errorLine(w, view.err);
try rememberBox(w, view.remember);
try w.writeAll("<button class=\"btn primary\">Sign in</button></div>");
try statusLine(w);
try cancel(w, "Cancel");
try w.writeAll("</form>");
if (!view.account.has_passkeys) try w.writeAll(passkey_offer);
try w.print("<a class=\"notyou\" href=\"{f}&step=signin\">Not you? Use another account</a>", .{esc(common.page_url)});
try w.writeAll("</main>");
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("<main class=\"card\">");
try mark(w, common.host);
try w.print("<h1>Continue to <b>{f}</b></h1>", .{esc(common.client.label)});
try w.writeAll("<div class=\"stack\">");
try w.print("<a class=\"row\" href=\"{f}&step=consent\">", .{esc(common.page_url)});
try accountMedia(w, account, "");
try accountText(w, account);
try w.writeAll(chevron ++ "</a>");
try w.print("<a class=\"row\" href=\"{f}&step=signin\"><span class=\"media\">" ++ person_art ++ "</span><span class=\"text\"><span class=\"title other\">Another account</span></span>" ++ chevron ++ "</a>", .{esc(common.page_url)});
try w.writeAll("<form class=\"stack\" method=\"post\" action=\"/oauth/authorize\">");
try hidden(w, "request_uri", common.request_uri);
try hidden(w, "csrf", csrf);
try cancel(w, "Cancel");
try w.writeAll("<button class=\"link\" name=\"browser_action\" value=\"forget\" formnovalidate>Forget this account on this device</button>");
try w.writeAll("</form></div></main>");
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("<main class=\"card consent\">");
try mark(w, common.host);
try w.writeAll("<h1>Authorize</h1><p class=\"who\">");
try accountMedia(w, view.account, "");
try w.print("as <b>@{f}</b></p>", .{esc(view.account.handle)});
try w.writeAll("<div class=\"asker\">");
try appMedia(w, common.client);
try w.print("<span class=\"t\"><b>{f}</b><span>wants access to your account</span></span>", .{esc(common.client.label)});
try w.writeAll("<button class=\"help\" type=\"button\" aria-haspopup=\"dialog\" aria-controls=\"details\" aria-label=\"Details about this request\" data-details>" ++ info_icon ++ "</button></div>");
if (view.summary.full_identity) {
try w.writeAll("<p class=\"warning\" role=\"note\">" ++ warning_icon ++ "<span>This app asks for full control of your identity. It could permanently break or take over your account. Only allow apps you trust completely.</span></p>");
}
try w.writeAll("<p class=\"lede\" id=\"lede\">It will be able to</p><ul class=\"perms\" aria-labelledby=\"lede\">");
for (view.summary.rows) |row| {
const warn = if (row.warning) " class=\"warn\"" else "";
if (row.more.len == 0) {
try w.print("<li{s}><div class=\"perm static\"><span class=\"ico\">{s}</span><span><b>{f}</b><small>{f}</small></span></div></li>", .{ warn, rowIcon(row.icon), esc(row.title), esc(row.detail) });
continue;
}
try w.print("<li{s}><details><summary class=\"perm\"><span class=\"ico\">{s}</span><span><b>{f}</b><small>{f}</small></span><span class=\"pchev\" aria-hidden=\"true\"></span></summary><div class=\"more\"><ul>", .{ warn, rowIcon(row.icon), esc(row.title), esc(row.detail) });
for (row.more) |line| try w.print("<li>{f}</li>", .{esc(line)});
try w.writeAll("</ul></div></details></li>");
}
try w.writeAll("</ul>");
try terms(w, common.client);
try w.writeAll("<form class=\"stack\" method=\"post\" action=\"/oauth/authorize\">");
try hidden(w, "request_uri", common.request_uri);
try hidden(w, "csrf", view.csrf);
try w.writeAll("<button class=\"btn primary\" name=\"browser_action\" value=\"continue\">Authorize</button>");
try w.writeAll("<button class=\"btn\" name=\"deny\" value=\"1\" formnovalidate>Deny</button></form>");
try detailsDialog(w, common.client, view);
try w.writeAll("</main>");
try tail(w);
return out.toOwnedSlice();
}
fn appMedia(w: *Writer, client: Client) !void {
if (client.icon_url) |url| {
try w.print("<span class=\"media app\" data-image=\"{f}\" data-image-fit=\"logo\">" ++ apps_art ++ "</span>", .{esc(url)});
} else {
try w.writeAll("<span class=\"media app\">" ++ apps_art ++ "</span>");
}
}
/// 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("<main class=\"card back\">");
try mark(w, common.host);
try w.writeAll("<div class=\"handoff\" aria-hidden=\"true\"><span class=\"media home\"><span data-icon-slot=\"zds.header.brand\">zds</span></span><span class=\"track\"></span>");
try appMedia(w, common.client);
try w.writeAll("</div>");
try w.print("<h1>Returning to {f}</h1>", .{esc(common.client.label)});
try w.print(
"<a class=\"btn primary\" href=\"{f}\" data-return hidden>Continue</a>" ++
"<noscript><a class=\"btn primary\" href=\"{f}\">Continue</a></noscript>",
.{ esc(location), esc(location) },
);
try w.writeAll("</main>");
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("<main class=\"card\">");
try mark(w, host);
try w.print("<h1>{f}</h1><p class=\"sub\">{f}</p><p class=\"code\">{f}</p>", .{ esc(title), esc(message), esc(code) });
try w.writeAll("</main>");
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(
\\<!doctype html>
\\<html lang="en">
\\<head>
\\<meta charset="utf-8">
\\<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
\\<meta name="referrer" content="same-origin">
\\<title>{f} · {f}</title>
\\<link rel="icon" href="/favicon.svg" type="image/svg+xml">
\\<script>{s}</script><style>{s}</style><style>{s}</style>{s}
\\</head>
\\<body>
\\{s}
, .{ esc(title), esc(host), appearance_js, appearance_css, page_css, skin.authorizeStyle(), license });
}
fn tail(w: *Writer) !void {
try w.writeAll("<script>" ++ page_js ++ "</script>");
try w.writeAll(skin.script());
try w.writeAll("\n</body>\n</html>\n");
}
fn mark(w: *Writer, host: []const u8) !void {
try w.print("<a class=\"mark\" href=\"/\">{f}</a>", .{esc(host)});
}
fn continueTo(w: *Writer, client: Client) !void {
try w.print("<p class=\"sub\">to continue to <b>{f}</b></p>", .{esc(client.label)});
}
fn hidden(w: *Writer, name: []const u8, value: []const u8) !void {
try w.print("<input type=\"hidden\" name=\"{s}\" value=\"{f}\">", .{ name, esc(value) });
}
fn passwordField(w: *Writer) !void {
try w.writeAll("<div class=\"field\"><label for=\"password\">Password</label><div class=\"input\"><input id=\"password\" name=\"password\" type=\"password\" placeholder=\"Enter your password\" autocomplete=\"current-password\" required></div></div>");
}
fn rememberBox(w: *Writer, checked: bool) !void {
try w.print("<label class=\"check\"><input type=\"checkbox\" name=\"remember_browser\" value=\"1\"{s}> Remember this account on this device</label>", .{if (checked) " checked" else ""});
}
fn cancel(w: *Writer, text: []const u8) !void {
try w.print("<button class=\"btn quiet\" name=\"deny\" value=\"1\" formnovalidate>{s}</button>", .{text});
}
fn errorLine(w: *Writer, err: ?[]const u8) !void {
const message = err orelse return;
try w.print("<p class=\"error\" role=\"alert\">" ++ error_icon ++ "<span>{f}</span></p>", .{esc(message)});
}
/// Where passkey outcomes are reported without leaving the page.
fn statusLine(w: *Writer) !void {
try w.writeAll("<p class=\"error\" role=\"alert\" data-status hidden>" ++ error_icon ++ "<span></span></p>");
}
fn accountMedia(w: *Writer, account: Account, extra_class: []const u8) !void {
if (account.avatar_url) |url| {
try w.print("<span class=\"media{s}\" data-image=\"{f}\">" ++ person_art ++ "</span>", .{ extra_class, esc(url) });
} else {
try w.print("<span class=\"media{s}\">" ++ person_art ++ "</span>", .{extra_class});
}
}
fn accountText(w: *Writer, account: Account) !void {
try w.print("<span class=\"text\"><span class=\"title name\">{f}</span><span class=\"value handle\">@{f}</span></span>", .{
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("<p class=\"terms\">Subject to the app's ");
if (tos) |url| try w.print("<a href=\"{f}\" rel=\"noopener noreferrer\" target=\"_blank\">terms</a>", .{esc(url)});
if (tos != null and policy != null) try w.writeAll(" and ");
if (policy) |url| try w.print("<a href=\"{f}\" rel=\"noopener noreferrer\" target=\"_blank\">privacy policy</a>", .{esc(url)});
try w.writeAll(".</p>");
}
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("<dialog class=\"sheet\" id=\"details\" aria-labelledby=\"details-title\"><div class=\"grab\" aria-hidden=\"true\"></div><header class=\"sh\">");
try appMedia(w, client);
try w.print("<span class=\"t\"><b id=\"details-title\" tabindex=\"-1\" autofocus>{f}</b><span>Requested permissions</span></span>", .{esc(client.label)});
try w.writeAll("<form method=\"dialog\"><button class=\"close\" aria-label=\"Close\">" ++ close_icon ++ "</button></form></header><div class=\"sb\">");
try w.print("<section class=\"sec\"><h3>App</h3><dl class=\"kv\"><dt>Client ID</dt><dd class=\"id\">{f}</dd>", .{breakable(client.client_id)});
if (client.name) |name| try w.print("<dt>Calls itself</dt><dd>{f}</dd>", .{esc(name)});
try w.writeAll("</dl></section>");
if (summary.sets.len > 0) {
try w.writeAll("<section class=\"sec\"><h3>Permission sets</h3>");
for (summary.sets) |set| {
try w.print("<div class=\"line\"><span class=\"verb\">{f}</span><span class=\"nsid\">{f}</span>", .{ esc(set.title), nsid(set.nsid) });
if (set.detail.len > 0) try w.print("<p class=\"note\">{f}</p>", .{esc(set.detail)});
if (set.expanded.len > 0) {
try w.print("<details class=\"incl\"><summary>Includes {d} scope{s}</summary><span class=\"chips\">", .{ set.expanded.len, if (set.expanded.len == 1) "" else "s" });
for (set.expanded) |scope| try w.print("<code>{f}</code>", .{breakable(scope)});
try w.writeAll("</span></details>");
}
try w.writeAll("</div>");
}
try w.writeAll("</section>");
}
if (summary.records.len > 0 or summary.any_records != null) {
try w.writeAll("<section class=\"sec\"><h3>Records in your repo</h3>");
if (summary.any_records) |verbs| try w.print("<div class=\"line\"><span class=\"verb\">{f}</span><span class=\"chips\"><code>any record type</code></span></div>", .{esc(verbs)});
for (summary.records) |line| {
try w.print("<div class=\"line\"><span class=\"verb\">{f}</span><span class=\"chips\">", .{esc(line.verbs)});
for (line.collections) |collection| try w.print("<code>{f}</code>", .{nsid(collection)});
try w.writeAll("</span></div>");
}
try w.writeAll("</section>");
}
if (summary.files.len > 0) {
try w.writeAll("<section class=\"sec\"><h3>Files</h3><div class=\"line\"><span class=\"verb\">Upload</span><span class=\"chips\">");
for (summary.files) |kind| try w.print("<code>{f}</code>", .{esc(kind)});
try w.writeAll("</span></div></section>");
}
if (summary.spaces.len > 0) {
try w.writeAll("<section class=\"sec\"><h3>Spaces</h3>");
for (summary.spaces) |space| {
try w.print("<div class=\"space\"><div class=\"stype\"><span class=\"nsid\">{f}</span></div>", .{nsidBold(space.nsid)});
for (space.access) |access| try spaceAccess(w, access, view.authorities);
try w.writeAll("</div>");
}
try w.writeAll("<p class=\"note\">Viewing a space covers all of it; writes are limited to the record types listed. Only the owner decides who else can read.</p></section>");
}
if (summary.services.len > 0) {
try w.writeAll("<section class=\"sec\"><h3>Services</h3>");
for (summary.services) |call| {
if (std.mem.eql(u8, call.aud, "*")) {
try w.writeAll("<div class=\"svc\"><span class=\"aud\">Any service</span><span class=\"chips\">");
} else {
try w.print("<div class=\"svc\"><span class=\"id\">{f}</span><span class=\"chips\">", .{breakable(call.aud)});
}
for (call.methods) |method| try w.print("<code>{f}</code>", .{nsid(method)});
try w.writeAll("</span></div>");
}
try w.writeAll("</section>");
}
if (summary.other.len > 0) {
try w.writeAll("<section class=\"sec\"><h3>Other</h3><span class=\"chips\">");
for (summary.other) |scope| try w.print("<code>{f}</code>", .{breakable(scope)});
try w.writeAll("</span></section>");
}
try w.print("<div class=\"raw\"><button type=\"button\" data-copy=\"{f}\">Copy raw scopes</button><span class=\"copied\" role=\"status\"></span></div>", .{esc(view.scope)});
try w.writeAll("</div></dialog>");
}
fn spaceAccess(w: *Writer, access: scope_display.SpaceAccess, authorities: []const Authority) !void {
switch (access.owner) {
.you => {
try w.writeAll("<div class=\"owner\"><span class=\"pill\">You</span><span class=\"key\">");
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 <code>{f}</code>", .{esc(key)});
try w.writeAll("</span></div>");
},
.anyone => try w.writeAll("<div class=\"owner\"><span class=\"pill other\">Anyone's</span><span class=\"key\">where the owner gave you access</span></div>"),
.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("<div class=\"owner\"><span class=\"pill other\">@{f}</span><span class=\"key\">their space</span></div>", .{esc(name)});
} else {
try w.print("<div class=\"owner\"><span class=\"pill other\">One account's</span><span class=\"key id\">{f}</span></div>", .{breakable(access.authority)});
}
},
}
try w.writeAll("<ul class=\"caps\">");
if (access.view) |view| try w.print("<li><span class=\"verb\">View</span><span>{f}</span></li>", .{esc(view)});
if (access.write_verbs) |verbs| {
try w.writeAll("<li><span class=\"verb\">Write</span><span>");
for (access.write_collections) |collection| try w.print("<code>{f}</code> ", .{nsid(collection)});
try w.print("· {f}</span></li>", .{esc(verbs)});
}
if (access.manage) |manage| try w.print("<li><span class=\"verb\">Manage</span><span>{f}</span></li>", .{esc(manage)});
try w.writeAll("</ul>");
}
/// 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("<i>");
try (Escaped{ .value = self.value[0 .. dot + 1] }).format(w);
try w.writeAll(if (self.bold) "</i><b>" else "</i>");
try (Escaped{ .value = self.value[dot + 1 ..] }).format(w);
if (self.bold) try w.writeAll("</b>");
}
};
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("<wbr>"),
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", "<path d=\"M4 7a2 2 0 0 1 2-2h4l2 2h6a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2z\"/>"),
.files => consentSlot("files", "<path d=\"M21 12.5 12.6 21a5 5 0 0 1-7.1-7.1l8.5-8.5a3.3 3.3 0 1 1 4.7 4.7l-8.5 8.5a1.7 1.7 0 0 1-2.4-2.4l7.8-7.8\"/>"),
.space => consentSlot("space", "<rect x=\"5\" y=\"11\" width=\"14\" height=\"10\" rx=\"2\"/><path d=\"M8 11V8a4 4 0 0 1 8 0v3\"/>"),
.service => consentSlot("service", "<path d=\"M9 2v4M15 2v4M6 6h12v4a6 6 0 0 1-12 0zM12 16v6\"/>"),
.read => consentSlot("read", "<path d=\"M2 12s3.6-7 10-7 10 7 10 7-3.6 7-10 7S2 12 2 12z\"/><circle cx=\"12\" cy=\"12\" r=\"3\"/>"),
.report => consentSlot("report", "<path d=\"M5 21V4M5 4h11l-2 4 2 4H5\"/>"),
.permission_set => consentSlot("permission-set", "<path d=\"M12 3 4 6v6c0 4.4 3.4 8 8 9 4.6-1 8-4.6 8-9V6z\"/><path d=\"m9 12 2 2 4-4\"/>"),
.account => consentSlot("account", "<rect x=\"3\" y=\"5\" width=\"18\" height=\"14\" rx=\"2\"/><path d=\"m3 7 9 6 9-6\"/>"),
.identity => consentSlot("identity", "<rect x=\"3\" y=\"5\" width=\"18\" height=\"14\" rx=\"2\"/><circle cx=\"9\" cy=\"11\" r=\"2\"/><path d=\"M6 16c.6-1.6 1.7-2.4 3-2.4s2.4.8 3 2.4M15 10h3M15 13h3\"/>"),
};
}
fn consentSlot(comptime name: []const u8, comptime body: []const u8) []const u8 {
return "<svg data-icon-slot=\"zds.consent." ++ name ++ "\" class=\"art\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.8\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\">" ++ body ++ "</svg>";
}
fn svg(comptime body: []const u8) []const u8 {
return "<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.8\" stroke-linecap=\"round\" stroke-linejoin=\"round\" focusable=\"false\">" ++ body ++ "</svg>";
}
fn slot(comptime name: []const u8, comptime class: []const u8, comptime body: []const u8) []const u8 {
return "<svg data-icon-slot=\"" ++ name ++ "\" class=\"" ++ class ++ "\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\">" ++ body ++ "</svg>";
}
// 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 = "<p class=\"offer\">" ++ passkey_art ++ "<span>Sign in with a passkey next time. <a href=\"/account/manage\" target=\"_blank\" rel=\"noopener\">Add one</a></span></p>";
const passkey_art = slot("zds.account.passkeys", "art", "<path d=\"M12 10a2 2 0 0 0-2 2c0 1.02-.1 2.51-.26 4\"/><path d=\"M14 13.12c0 2.38 0 6.38-1 8.88\"/><path d=\"M17.29 21.02c.12-.6.43-2.3.5-3.02\"/><path d=\"M2 12a10 10 0 0 1 18-6\"/><path d=\"M2 16h.01\"/><path d=\"M21.8 16c.2-2 .131-5.354 0-6\"/><path d=\"M5 19.5C5.5 18 6 15 6 12a6 6 0 0 1 .34-2\"/><path d=\"M8.65 22c.21-.66.45-1.32.57-2\"/><path d=\"M9 6.8a6 6 0 0 1 9 5.2v2\"/>");
const key_art = slot("zds.account.app-passwords", "art", "<path d=\"M2.586 17.414A2 2 0 0 0 2 18.828V21a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h1a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h.172a2 2 0 0 0 1.414-.586l.814-.814a6.5 6.5 0 1 0-4-4z\"/><circle cx=\"16.5\" cy=\"7.5\" r=\".5\" fill=\"currentColor\"/>");
const apps_art = slot("zds.nav.apps", "art", "<rect width=\"18\" height=\"18\" x=\"3\" y=\"3\" rx=\"2\"/><path d=\"M3 9h18\"/><path d=\"M9 21V9\"/>");
const person_art = slot("zds.nav.account", "art", "<circle cx=\"12\" cy=\"8\" r=\"5\"/><path d=\"M20 21a8 8 0 0 0-16 0\"/>");
const chevron = "<svg class=\"chev\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"m9 18 6-6-6-6\"/></svg>";
const error_icon = "<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><circle cx=\"12\" cy=\"12\" r=\"10\"/><line x1=\"12\" x2=\"12\" y1=\"8\" y2=\"12\"/><line x1=\"12\" x2=\"12.01\" y1=\"16\" y2=\"16\"/></svg>";
const close_icon = "<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M6 6l12 12M18 6 6 18\"/></svg>";
const info_icon = "<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.8\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><circle cx=\"12\" cy=\"12\" r=\"9\"/><path d=\"M12 11v5\"/><circle cx=\"12\" cy=\"8\" r=\".6\" fill=\"currentColor\"/></svg>";
const warning_icon = "<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3\"/><path d=\"M12 9v4\"/><path d=\"M12 17h.01\"/></svg>";
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 = "<b>evil</b>" },
};
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 = "\"><script>" });
defer std.testing.allocator.free(html);
try std.testing.expect(std.mem.indexOf(u8, html, "autofocus") == null);
try std.testing.expect(std.mem.indexOf(u8, html, "autocomplete=\"username webauthn\" autocapitalize=\"none\" autocorrect=\"off\" spellcheck=\"false\"") != null);
for ([_][]const u8{ "name=\"username\"", "name=\"password\"", "name=\"remember_browser\"", "name=\"deny\"", "name=\"request_uri\" value=\"urn:ietf:params:oauth:request_uri:req\"" }) |field| {
try std.testing.expect(std.mem.indexOf(u8, html, field) != null);
}
try std.testing.expect(std.mem.indexOf(u8, html, "value=\""><script>\"") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "Forgot") == null);
try std.testing.expect(std.mem.indexOf(u8, html, "class=\"offer\"") != null);
}
test "the known-account card offers a passkey only to an account without one" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const without = try known(arena.allocator(), test_common, .{ .account = .{ .handle = "alice.test" } });
try std.testing.expect(std.mem.indexOf(u8, without, "class=\"offer\"") != null);
try std.testing.expect(std.mem.indexOf(u8, without, "href=\"/account/manage\" target=\"_blank\"") != null);
const with = try known(arena.allocator(), test_common, .{ .account = .{ .handle = "alice.test", .has_passkeys = true } });
try std.testing.expect(std.mem.indexOf(u8, with, "class=\"offer\"") == null);
}
test "the return page keeps the scripted link hidden and provides a no-script fallback" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const html = try returning(arena.allocator(), test_common, "https://app.example/callback?code=abc&state=x");
try std.testing.expect(std.mem.indexOf(u8, html, "<h1>Returning to app.example</h1>") != null);
const href = "href=\"https://app.example/callback?code=abc&state=x\"";
try std.testing.expect(std.mem.indexOf(u8, html, href ++ " data-return hidden") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "<noscript><a class=\"btn primary\" " ++ href ++ ">Continue</a></noscript>") != null);
}
test "the consent card names the app by domain and keeps its self-declared name in the details" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const scope = "atproto identity:* repo:app.bsky.feed.post?action=create space:app.example.board?authority=self&skey=self&collection=app.example.post&action=read&action=create";
const html = try consent(arena.allocator(), test_common, .{
.account = .{ .handle = "alice.test" },
.summary = try scope_display.describe(arena.allocator(), scope, &.{}, "app.example"),
.scope = scope,
.csrf = "0000000000000000000000000000000000000000000000000000000000000000",
});
try std.testing.expect(std.mem.indexOf(u8, html, "<span class=\"t\"><b>app.example</b>") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "<dt>Calls itself</dt><dd><b>evil</b></dd>") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "<b>evil</b>") == null);
try std.testing.expect(std.mem.indexOf(u8, html, "class=\"warning\"") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "value=\"continue\"") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "class=\"terms\"") == null);
try std.testing.expect(std.mem.indexOf(u8, html, "class=\"offer\"") == null);
// The sheet: raw scopes to copy, record chips and space capabilities.
try std.testing.expect(std.mem.indexOf(u8, html, "data-copy=\"atproto identity:* repo:app.bsky.feed.post?action=create space:app.example.board?authority=self&skey=self") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "<code><i>app.bsky.feed.</i>post</code>") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "<span class=\"pill\">You</span><span class=\"key\">key <code>self</code>") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "<span class=\"verb\">View</span><span>everything in it</span>") != null);
}