@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, 06:00 UTC

main
ts 247 lines 8.04 KB
Raw
1 import * as cloudidentity from "@distilled.cloud/gcp/cloudidentity_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
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 type { Providers } from "../Providers.ts";
9 import {
10 encodeOwnershipLine,
11 findOwnedSamlProfile,
12 getSamlProfile,
13 hasOwnershipMarker,
14 lastSegment,
15 listSamlProfiles,
16 MAX_DISPLAY_NAME_LENGTH,
17 normalizeCustomer,
18 ownedByAlchemy,
19 ownershipLabels,
20 parseOwnership,
21 replaceOnIdentity,
22 sameJson,
23 sameText,
24 toPhysicalId,
25 updateMaskOf,
26 } from "./internal.ts";
27 import { resourceNameFromOperation, waitForOperation, waitUntilPresent } from "./operations.ts";
28
29 export type InboundSamlSsoProfileIdpConfig = {
30 /** HTTPS SingleSignOnService URL (HTTP-Redirect binding). */
31 singleSignOnServiceUri?: string;
32 /** SAML Entity ID of the identity provider. */
33 entityId?: string;
34 /** HTTPS logout redirect URL. */
35 logoutRedirectUri?: string;
36 /** HTTPS change-password URL. */
37 changePasswordUri?: string;
38 };
39
40 export type InboundSamlSsoProfileProps = {
41 /**
42 * Customer (`customers/C0123abc` or `customers/my_customer`).
43 * Immutable — changing it replaces the profile.
44 * @default "customers/my_customer"
45 */
46 customer?: string;
47 /**
48 * Human-readable name. Profiles have no labels field, so Alchemy
49 * stamps ownership into `displayName` for `list` / nuke.
50 */
51 displayName?: string;
52 /**
53 * SAML identity provider configuration.
54 */
55 idpConfig?: InboundSamlSsoProfileIdpConfig;
56 };
57
58 export type InboundSamlSsoProfile = Resource<
59 "GCP.CloudIdentity.InboundSamlSsoProfile",
60 InboundSamlSsoProfileProps,
61 {
62 /** Resource name `inboundSamlSsoProfiles/{id}`. */
63 name: string;
64 /** Profile id (last path segment). */
65 profileId: string;
66 /** Customer. */
67 customer: string | undefined;
68 /** Display name with the Alchemy ownership prefix stripped. */
69 displayName: string | undefined;
70 /** IdP configuration. */
71 idpConfig: InboundSamlSsoProfileIdpConfig | undefined;
72 /** Google-provided SP ACS URL. */
73 assertionConsumerServiceUri: string | undefined;
74 /** Google-provided SP Entity ID. */
75 spEntityId: string | undefined;
76 },
77 never,
78 Providers
79 >;
80
81 /**
82 * An inbound SAML 2.0 SSO profile for a Google enterprise customer.
83 *
84 * Profiles have no labels field, so Alchemy stamps ownership into
85 * `displayName` for `list` / nuke. `customer` is identity; display
86 * name and IdP config update in place.
87 *
88 * ### Creating a Profile
89 * **Example:** Basic SAML IdP
90 * ```typescript
91 * const profile = yield* GCP.CloudIdentity.InboundSamlSsoProfile("Okta", {
92 * displayName: "Okta",
93 * idpConfig: {
94 * entityId: "https://idp.example.com/metadata",
95 * singleSignOnServiceUri: "https://idp.example.com/sso",
96 * },
97 * });
98 * ```
99 *
100 * @resource
101 * @category CloudIdentity
102 */
103 export const InboundSamlSsoProfile = Resource<InboundSamlSsoProfile>(
104 "GCP.CloudIdentity.InboundSamlSsoProfile",
105 );
106
107 export class InboundSamlSsoProfileNotResolved extends Data.TaggedError(
108 "GCP.CloudIdentity.InboundSamlSsoProfileNotResolved",
109 )<{
110 resourceName: string;
111 }> {}
112
113 const toAttrs = (profile: cloudidentity.InboundSamlSsoProfile) => {
114 const name = profile.name ?? "";
115 return {
116 name,
117 profileId: lastSegment(name),
118 customer: profile.customer,
119 displayName: parseOwnership(profile.displayName).text,
120 idpConfig: profile.idpConfig
121 ? {
122 singleSignOnServiceUri: profile.idpConfig.singleSignOnServiceUri,
123 entityId: profile.idpConfig.entityId,
124 logoutRedirectUri: profile.idpConfig.logoutRedirectUri,
125 changePasswordUri: profile.idpConfig.changePasswordUri,
126 }
127 : undefined,
128 assertionConsumerServiceUri: profile.spConfig?.assertionConsumerServiceUri,
129 spEntityId: profile.spConfig?.entityId,
130 };
131 };
132
133 const observeProfile = (input: { id: string; name?: string }) =>
134 Effect.gen(function* () {
135 const byName = yield* getSamlProfile(input.name ?? "");
136 if (byName !== undefined) return byName;
137 return yield* findOwnedSamlProfile(input.id);
138 });
139
140 export const InboundSamlSsoProfileProvider = () =>
141 Provider.succeed(InboundSamlSsoProfile, {
142 stables: ["name", "profileId", "customer"],
143
144 diff: Effect.fn(function* ({ news, olds, output }) {
145 if (!isResolved(news)) return undefined;
146 const previous = olds?.customer ?? output?.customer;
147 const next = news.customer !== undefined ? normalizeCustomer(news.customer) : previous;
148 return replaceOnIdentity({
149 previousParent: previous,
150 nextParent: next,
151 });
152 }),
153
154 read: Effect.fn(function* ({ id, output }) {
155 const existing = yield* observeProfile({ id, name: output?.name });
156 if (existing === undefined) return undefined;
157 const attrs = toAttrs(existing);
158 return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
159 }),
160
161 list: () =>
162 Effect.gen(function* () {
163 const profiles = yield* listSamlProfiles();
164 return profiles.filter((profile) => hasOwnershipMarker(profile.displayName)).map(toAttrs);
165 }),
166
167 reconcile: Effect.fn(function* ({ id, news, output }) {
168 const ownership = yield* ownershipLabels(id);
169 const customer = normalizeCustomer(news.customer ?? output?.customer);
170 const displayName = encodeOwnershipLine(
171 ownership,
172 yield* toPhysicalId(id, news.displayName, output?.displayName, MAX_DISPLAY_NAME_LENGTH),
173 MAX_DISPLAY_NAME_LENGTH,
174 );
175 const desired: cloudidentity.InboundSamlSsoProfile = {
176 customer,
177 displayName,
178 idpConfig: news.idpConfig,
179 };
180
181 let current = yield* observeProfile({ id, name: output?.name });
182
183 if (current === undefined) {
184 const created = yield* cloudidentity
185 .createInboundSamlSsoProfiles({ body: desired })
186 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
187 if (created !== undefined) {
188 yield* waitForOperation(created).pipe(
189 Effect.catchTag("GCP.CloudIdentity.OperationPending", () => Effect.void),
190 );
191 const createdName = resourceNameFromOperation(created);
192 if (createdName !== undefined) {
193 current = yield* getSamlProfile(createdName);
194 }
195 }
196 if (current === undefined) {
197 current = yield* waitUntilPresent(
198 observeProfile({ id, name: output?.name }),
199 displayName,
200 ).pipe(
201 Effect.catchTag("GCP.CloudIdentity.OperationPending", () => observeProfile({ id })),
202 );
203 }
204 }
205
206 if (current === undefined) {
207 return yield* new InboundSamlSsoProfileNotResolved({
208 resourceName: output?.name ?? displayName,
209 });
210 }
211
212 const name = current.name ?? output?.name ?? "";
213 const displayChanged = !sameText(current.displayName, displayName);
214 const idpChanged = !sameJson(current.idpConfig, news.idpConfig ?? {});
215 const updateMask = updateMaskOf(
216 displayChanged ? "display_name" : undefined,
217 idpChanged ? "idp_config" : undefined,
218 );
219
220 if (updateMask.length > 0 && name.length > 0) {
221 const patched = yield* cloudidentity.patchInboundSamlSsoProfiles({
222 name,
223 updateMask,
224 body: {
225 displayName,
226 idpConfig: news.idpConfig,
227 },
228 });
229 yield* waitForOperation(patched).pipe(
230 Effect.catchTag("GCP.CloudIdentity.OperationPending", () => Effect.void),
231 );
232 current = (yield* getSamlProfile(name)) ?? current;
233 }
234
235 return toAttrs(current);
236 }),
237
238 delete: Effect.fn(function* ({ output }) {
239 if (output.name.length === 0) return;
240 const deleted = yield* cloudidentity
241 .deleteInboundSamlSsoProfiles({ name: output.name })
242 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
243 if (deleted !== undefined) {
244 yield* waitForOperation(deleted, { notFoundOk: true });
245 }
246 }),
247 });