@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 111 lines 3.65 KB
Raw
1 import { AuthError } from "alchemy/Auth/AuthProvider";
2 import { profileCommandHint } from "alchemy/Util/interactive";
3 import * as Effect from "effect/Effect";
4 import * as Redacted from "effect/Redacted";
5 import * as Schema from "effect/Schema";
6
7 /** Typed values stored in a Cloudflare provider profile document. */
8 export const CloudflareAuthConfigSchema = Schema.Union([
9 Schema.Struct({
10 method: Schema.Literal("stored"),
11 credentialType: Schema.Literal("apiToken"),
12 apiToken: Schema.String,
13 accountId: Schema.String,
14 }),
15 Schema.Struct({
16 method: Schema.Literal("stored"),
17 credentialType: Schema.Literal("apiKey"),
18 apiKey: Schema.String,
19 email: Schema.String,
20 accountId: Schema.String,
21 }),
22 Schema.Struct({
23 method: Schema.Literal("oauth"),
24 scopes: Schema.String.pipe(Schema.Array, Schema.mutable),
25 accountId: Schema.String,
26 clientId: Schema.optional(Schema.String),
27 access: Schema.String,
28 refresh: Schema.String,
29 expires: Schema.Number,
30 }),
31 // Released v0 OAuth grants cannot be reused, but retaining the method lets
32 // `profile refresh` restart OAuth at scope selection.
33 Schema.Struct({
34 method: Schema.Literal("oauth"),
35 }),
36 ]);
37 export type CloudflareAuthConfig = typeof CloudflareAuthConfigSchema.Type;
38
39 export type CloudflareResolvedCredentials =
40 | {
41 type: "apiToken";
42 apiToken: Redacted.Redacted<string>;
43 accountId: string;
44 source: {
45 type: CloudflareAuthConfig["method"] | "env";
46 details?: string;
47 };
48 }
49 | {
50 type: "apiKey";
51 apiKey: Redacted.Redacted<string>;
52 email: Redacted.Redacted<string>;
53 accountId: string;
54 source: {
55 type: CloudflareAuthConfig["method"] | "env";
56 details?: string;
57 };
58 }
59 | {
60 type: "oauth";
61 accessToken: Redacted.Redacted<string>;
62 expires: number;
63 accountId: string;
64 source: {
65 type: CloudflareAuthConfig["method"] | "env";
66 details?: string;
67 };
68 };
69
70 export const CLOUDFLARE_AUTH_PROVIDER_NAME = "Cloudflare";
71
72 /**
73 * Cloudflare account IDs are 32 lowercase hex characters. Placeholder
74 * values ("", "-", "dummy", …) end up interpolated into API paths and
75 * surface as baffling `InvalidRoute: Could not route to
76 * /accounts/<value>/...` errors, so reject them up front with an
77 * actionable message instead.
78 */
79 const ACCOUNT_ID_PATTERN = /^[0-9a-f]{32}$/i;
80
81 export const validateAccountId = (
82 accountId: string | undefined,
83 source: string,
84 ): Effect.Effect<string, AuthError> =>
85 Effect.gen(function* () {
86 const trimmed = accountId?.trim() ?? "";
87 const command = yield* profileCommandHint("alchemy profile edit --reconfigure Cloudflare");
88 if (trimmed.length === 0) {
89 return yield* AuthError.make({
90 message:
91 `Cloudflare account ID is missing (${source}). ` +
92 `Re-run \`${command}\` and provide your account ID ` +
93 "(found in the Cloudflare dashboard under Workers & Pages → Account details).",
94 });
95 }
96 if (!ACCOUNT_ID_PATTERN.test(trimmed)) {
97 return yield* AuthError.make({
98 message:
99 `'${trimmed}' is not a valid Cloudflare account ID (${source}) — expected 32 hex characters. ` +
100 "Copy the account ID from the Cloudflare dashboard (Workers & Pages → Account details) " +
101 `and re-run \`${command}\`.`,
102 });
103 }
104 return trimmed.toLowerCase();
105 });
106
107 /** Field-level validator reusing {@link ACCOUNT_ID_PATTERN}. */
108 export const validateAccountIdField = (v: string): string | undefined =>
109 ACCOUNT_ID_PATTERN.test(v.trim())
110 ? undefined
111 : "Expected a 32-character hex account ID (Workers & Pages → Account details)";