The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import * as Predicate from "effect/Predicate"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | |
| 14 | export class CustomPageUnexpectedResponse extends Data.TaggedError("CustomPageUnexpectedResponse")<{ |
| 15 | readonly message: string; |
| 16 | readonly cause?: unknown; |
| 17 | }> {} |
| 18 | |
| 19 | export type CustomPageType = "identity_denied" | "forbidden"; |
| 20 | |
| 21 | export type CustomPageProps = { |
| 22 | /** |
| 23 | * Display name for the custom page. Used as a stable identifier so the |
| 24 | * provider can locate the page during adoption / state recovery. If |
| 25 | * omitted, a unique name is generated from the stack/stage/logical id. |
| 26 | * |
| 27 | * @default ${app}-${stage}-${id} |
| 28 | */ |
| 29 | name?: string; |
| 30 | /** |
| 31 | * The type of Access event the page is shown for. `identity_denied` is |
| 32 | * shown when a user's identity is rejected by policy; `forbidden` is shown |
| 33 | * when access is blocked outright. Changing the type replaces the page. |
| 34 | */ |
| 35 | type: CustomPageType; |
| 36 | /** |
| 37 | * The custom HTML served for the page. |
| 38 | */ |
| 39 | customHtml: string; |
| 40 | }; |
| 41 | |
| 42 | export type CustomPage = Resource< |
| 43 | "Cloudflare.Access.CustomPage", |
| 44 | CustomPageProps, |
| 45 | { |
| 46 | /** UUID of the custom page assigned by Cloudflare. */ |
| 47 | customPageId: string; |
| 48 | /** Cloudflare account that owns the custom page. */ |
| 49 | accountId: string; |
| 50 | /** Display name reported by Cloudflare. */ |
| 51 | name: string; |
| 52 | /** The Access event type the page is shown for. */ |
| 53 | type: CustomPageType; |
| 54 | }, |
| 55 | never, |
| 56 | Providers |
| 57 | >; |
| 58 | |
| 59 | /** |
| 60 | * A Cloudflare Zero Trust Access custom page. Replaces the default Access |
| 61 | * block pages (`identity_denied` / `forbidden`) with custom HTML, which can |
| 62 | * then be selected on an Access application. |
| 63 | * ### Creating a Custom Page |
| 64 | * **Example:** Custom forbidden page |
| 65 | * ```typescript |
| 66 | * const page = yield* Cloudflare.Access.CustomPage("Forbidden", { |
| 67 | * type: "forbidden", |
| 68 | * customHtml: "<html><body><h1>Access denied</h1></body></html>", |
| 69 | * }); |
| 70 | * ``` |
| 71 | * |
| 72 | * **Example:** Custom identity-denied page with an explicit name |
| 73 | * ```typescript |
| 74 | * const page = yield* Cloudflare.Access.CustomPage("Denied", { |
| 75 | * name: "corp-identity-denied", |
| 76 | * type: "identity_denied", |
| 77 | * customHtml: "<html><body><h1>Who are you?</h1></body></html>", |
| 78 | * }); |
| 79 | * ``` |
| 80 | * |
| 81 | * ### Updating the HTML |
| 82 | * **Example:** HTML and name converge in place |
| 83 | * ```typescript |
| 84 | * const page = yield* Cloudflare.Access.CustomPage("Forbidden", { |
| 85 | * type: "forbidden", |
| 86 | * customHtml: "<html><body><h1>Still denied</h1></body></html>", |
| 87 | * }); |
| 88 | * ``` |
| 89 | * |
| 90 | * @resource |
| 91 | * @product Access |
| 92 | * @category Cloudflare One (Zero Trust) |
| 93 | */ |
| 94 | export const CustomPage = Resource<CustomPage>("Cloudflare.Access.CustomPage"); |
| 95 | |
| 96 | export const isCustomPage = (value: unknown): value is CustomPage => |
| 97 | Predicate.hasProperty(value, "Type") && value.Type === "Cloudflare.Access.CustomPage"; |
| 98 | |
| 99 | export const CustomPageProvider = () => |
| 100 | Provider.succeed(CustomPage, { |
| 101 | stables: ["customPageId", "accountId", "type"], |
| 102 | // Account-scoped collection (pattern b). The list response items already |
| 103 | // carry every Attribute field (uid/name/type) — read's Attributes don't |
| 104 | // include customHtml — so no per-item get is needed to match the read shape. |
| 105 | list: Effect.fn(function* () { |
| 106 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 107 | return yield* zeroTrust.listAccessCustomPages.pages({ accountId }).pipe( |
| 108 | Stream.runCollect, |
| 109 | Effect.map((chunk) => |
| 110 | Array.from(chunk).flatMap((page) => |
| 111 | (page.result ?? []) |
| 112 | .filter((p): p is typeof p & { uid: string } => p.uid != null) |
| 113 | .map((p) => toAttrs(p, accountId)), |
| 114 | ), |
| 115 | ), |
| 116 | ); |
| 117 | }), |
| 118 | diff: Effect.fn(function* ({ news, output }) { |
| 119 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 120 | if (!isResolved(news)) return undefined; |
| 121 | if ((output?.accountId ?? accountId) !== accountId) { |
| 122 | return { action: "replace" } as const; |
| 123 | } |
| 124 | // The page type cannot change in place. |
| 125 | if (output && news.type !== output.type) { |
| 126 | return { action: "replace" } as const; |
| 127 | } |
| 128 | // name/customHtml converge via PUT. |
| 129 | }), |
| 130 | read: Effect.fn(function* ({ id, output, olds }) { |
| 131 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 132 | const acct = output?.accountId ?? accountId; |
| 133 | if (output?.customPageId) { |
| 134 | const direct = yield* zeroTrust |
| 135 | .getAccessCustomPage({ |
| 136 | accountId: acct, |
| 137 | customPageId: output.customPageId, |
| 138 | }) |
| 139 | .pipe(Effect.catchTag("AccessCustomPageNotFound", () => Effect.undefined)); |
| 140 | if (direct && direct.uid) { |
| 141 | return toAttrs(direct, acct); |
| 142 | } |
| 143 | } |
| 144 | const name = yield* createPageName(id, olds?.name ?? output?.name); |
| 145 | const existing = yield* findPageByName(acct, name); |
| 146 | if (!existing || !existing.uid) return undefined; |
| 147 | const full = yield* zeroTrust |
| 148 | .getAccessCustomPage({ accountId: acct, customPageId: existing.uid }) |
| 149 | .pipe(Effect.catchTag("AccessCustomPageNotFound", () => Effect.undefined)); |
| 150 | if (!full || !full.uid) return undefined; |
| 151 | return toAttrs(full, acct); |
| 152 | }), |
| 153 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 154 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 155 | const acct = output?.accountId ?? accountId; |
| 156 | const name = yield* createPageName(id, news.name); |
| 157 | |
| 158 | // Observe — prefer the cached id, fall back to a name lookup so we |
| 159 | // recover from out-of-band deletes and state-persistence failures. |
| 160 | let observed: ObservedPage | undefined; |
| 161 | if (output?.customPageId) { |
| 162 | observed = yield* zeroTrust |
| 163 | .getAccessCustomPage({ |
| 164 | accountId: acct, |
| 165 | customPageId: output.customPageId, |
| 166 | }) |
| 167 | .pipe(Effect.catchTag("AccessCustomPageNotFound", () => Effect.undefined)); |
| 168 | } |
| 169 | if (!observed || !observed.uid) { |
| 170 | const byName = yield* findPageByName(acct, name); |
| 171 | if (byName?.uid) { |
| 172 | observed = yield* zeroTrust |
| 173 | .getAccessCustomPage({ accountId: acct, customPageId: byName.uid }) |
| 174 | .pipe(Effect.catchTag("AccessCustomPageNotFound", () => Effect.undefined)); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Ensure — create the page when missing. Tolerate a same-named create |
| 179 | // race by re-observing. |
| 180 | if (!observed || !observed.uid) { |
| 181 | const created = yield* zeroTrust |
| 182 | .createAccessCustomPage({ |
| 183 | accountId: acct, |
| 184 | name, |
| 185 | type: news.type, |
| 186 | customHtml: news.customHtml, |
| 187 | }) |
| 188 | .pipe( |
| 189 | Effect.catch((err) => |
| 190 | Effect.gen(function* () { |
| 191 | const existing = yield* findPageByName(acct, name); |
| 192 | if (existing && existing.uid) return existing; |
| 193 | return yield* err; |
| 194 | }), |
| 195 | ), |
| 196 | ); |
| 197 | if (!created.uid) { |
| 198 | return yield* new CustomPageUnexpectedResponse({ |
| 199 | message: "CustomPage: created page missing uid", |
| 200 | }); |
| 201 | } |
| 202 | return toAttrs({ ...created, type: news.type }, acct); |
| 203 | } |
| 204 | |
| 205 | // Sync — converge name/html via PUT only on a real delta. The GET |
| 206 | // returns the full customHtml so the comparison is observed-vs-desired. |
| 207 | if ( |
| 208 | observed.name !== name || |
| 209 | observed.type !== news.type || |
| 210 | observed.customHtml !== news.customHtml |
| 211 | ) { |
| 212 | const updated = yield* zeroTrust.updateAccessCustomPage({ |
| 213 | accountId: acct, |
| 214 | customPageId: observed.uid, |
| 215 | name, |
| 216 | type: news.type, |
| 217 | customHtml: news.customHtml, |
| 218 | }); |
| 219 | observed = { |
| 220 | uid: updated.uid ?? observed.uid, |
| 221 | name: updated.name ?? name, |
| 222 | type: updated.type ?? news.type, |
| 223 | }; |
| 224 | } |
| 225 | |
| 226 | return toAttrs(observed, acct); |
| 227 | }), |
| 228 | delete: Effect.fn(function* ({ output }) { |
| 229 | yield* zeroTrust |
| 230 | .deleteAccessCustomPage({ |
| 231 | accountId: output.accountId, |
| 232 | customPageId: output.customPageId, |
| 233 | }) |
| 234 | .pipe(Effect.catchTag("AccessCustomPageNotFound", () => Effect.void)); |
| 235 | }), |
| 236 | }); |
| 237 | |
| 238 | const createPageName = (id: string, name: string | undefined) => |
| 239 | Effect.gen(function* () { |
| 240 | if (name) return name; |
| 241 | return yield* createPhysicalName({ id }); |
| 242 | }); |
| 243 | |
| 244 | const findPageByName = (acct: string, name: string) => |
| 245 | zeroTrust.listAccessCustomPages.items({ accountId: acct }).pipe( |
| 246 | Stream.filter((p) => p.name === name), |
| 247 | Stream.runHead, |
| 248 | Effect.map(Option.getOrUndefined), |
| 249 | Effect.orElseSucceed(() => undefined), |
| 250 | ); |
| 251 | |
| 252 | const toAttrs = (observed: ObservedPage, accountId: string) => ({ |
| 253 | customPageId: observed.uid!, |
| 254 | accountId, |
| 255 | name: observed.name ?? "", |
| 256 | type: (observed.type ?? "forbidden") as CustomPageType, |
| 257 | }); |
| 258 | |
| 259 | type ObservedPage = { |
| 260 | uid?: string | null; |
| 261 | name?: string | null; |
| 262 | type?: string | null; |
| 263 | customHtml?: string | null; |
| 264 | }; |