@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 102 lines 3.79 KB
Raw
1 import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
2 import * as Effect from "effect/Effect";
3 import * as Option from "effect/Option";
4 import * as Redacted from "effect/Redacted";
5 import * as Stream from "effect/Stream";
6 import type { IdentityProviderAttributes, IdentityProviderType } from "./IdentityProvider.ts";
7
8 // Shared observation scaffolding for the IdentityProvider resource provider
9 // and the GetIdentityProvider data source. NOT exported from `index.ts`.
10
11 export interface ObservedIdp {
12 readonly id?: string | null;
13 readonly name: string;
14 readonly type: string;
15 readonly scimConfig?: {
16 readonly enabled?: boolean | null;
17 readonly identityUpdateBehavior?: string | null;
18 readonly scimBaseUrl?: string | null;
19 readonly seatDeprovision?: boolean | null;
20 readonly secret?: string | null;
21 readonly userDeprovision?: boolean | null;
22 } | null;
23 }
24
25 /**
26 * IdP types that can exist at most once per scope (the managed Cloudflare
27 * WARP login method and the built-in one-time PIN). They are located by
28 * type — their display name is user-irrelevant and often empty (`""` for
29 * the dashboard-provisioned `cloudflare` IdP).
30 */
31 export const isSingletonType = (
32 type: IdentityProviderType | undefined,
33 ): type is "onetimepin" | "cloudflare" => type === "onetimepin" || type === "cloudflare";
34
35 /**
36 * Read an identity provider by id, mapping "gone"
37 * (`AccessIdentityProviderNotFound`, Cloudflare error code 12135 —
38 * `access.api.error.not_found`) to `undefined`. Zone-level when `zoneId`
39 * is set, account-level otherwise.
40 */
41 export const getIdp = (zoneId: string | undefined, accountId: string, identityProviderId: string) =>
42 (zoneId !== undefined
43 ? zeroTrust.getIdentityProviderForZone({ zoneId, identityProviderId })
44 : zeroTrust.getIdentityProviderForAccount({ accountId, identityProviderId })
45 ).pipe(
46 Effect.map((idp): ObservedIdp | undefined => idp as ObservedIdp),
47 Effect.catchTag("AccessIdentityProviderNotFound", () => Effect.undefined),
48 );
49
50 /**
51 * Find the first identity provider in the scope matching a predicate.
52 */
53 export const findFirst = (
54 zoneId: string | undefined,
55 accountId: string,
56 predicate: (idp: { name: string; type: string }) => boolean,
57 ) =>
58 (zoneId !== undefined
59 ? zeroTrust.listIdentityProvidersForZone.items({ zoneId })
60 : zeroTrust.listIdentityProvidersForAccount.items({ accountId })
61 ).pipe(
62 Stream.filter(predicate),
63 Stream.runHead,
64 Effect.map(Option.getOrUndefined),
65 Effect.map((idp): ObservedIdp | undefined => idp as ObservedIdp | undefined),
66 );
67
68 /**
69 * Find an identity provider by exact name within the scope. Names are
70 * not unique on Cloudflare's side; pick the first match.
71 */
72 export const findByName = (zoneId: string | undefined, accountId: string, name: string) =>
73 findFirst(zoneId, accountId, (idp) => idp.name === name);
74
75 /**
76 * Find an identity provider by type within the scope — the identity of
77 * the {@link isSingletonType singleton} types, whose display name carries
78 * no information.
79 */
80 export const findByType = (
81 zoneId: string | undefined,
82 accountId: string,
83 type: IdentityProviderType,
84 ) => findFirst(zoneId, accountId, (idp) => idp.type === type);
85
86 export const toAttributes = (
87 idp: ObservedIdp,
88 zoneId: string | undefined,
89 accountId: string,
90 priorSecret: Redacted.Redacted<string> | undefined,
91 ): IdentityProviderAttributes => ({
92 identityProviderId: idp.id ?? "",
93 accountId,
94 zoneId,
95 name: idp.name,
96 type: idp.type as IdentityProviderType,
97 scimBaseUrl: idp.scimConfig?.scimBaseUrl ?? undefined,
98 // The SCIM secret is returned once when SCIM is enabled; afterwards the
99 // API masks it — carry the prior value forward.
100 scimSecret: idp.scimConfig?.secret ? Redacted.make(idp.scimConfig.secret) : priorSecret,
101 scimEnabled: idp.scimConfig?.enabled ?? false,
102 });