The best way to control Claude Code, Codex and more from any computer or phone
| 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 | findOwnedOidcProfile, |
| 12 | getOidcProfile, |
| 13 | hasOwnershipMarker, |
| 14 | lastSegment, |
| 15 | listOidcProfiles, |
| 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 InboundOidcSsoProfileIdpConfig = { |
| 30 | /** IdP issuer URL. Discovery is derived from this URI. */ |
| 31 | issuerUri?: string; |
| 32 | /** HTTPS change-password URL. */ |
| 33 | changePasswordUri?: string; |
| 34 | }; |
| 35 | |
| 36 | export type InboundOidcSsoProfileRpConfig = { |
| 37 | /** OAuth2 client id. */ |
| 38 | clientId?: string; |
| 39 | /** OAuth2 client secret (input only). */ |
| 40 | clientSecret?: string; |
| 41 | }; |
| 42 | |
| 43 | export type InboundOidcSsoProfileProps = { |
| 44 | /** |
| 45 | * Customer (`customers/C0123abc` or `customers/my_customer`). |
| 46 | * Immutable — changing it replaces the profile. |
| 47 | * @default "customers/my_customer" |
| 48 | */ |
| 49 | customer?: string; |
| 50 | /** |
| 51 | * Human-readable name. Profiles have no labels field, so Alchemy |
| 52 | * stamps ownership into `displayName` for `list` / nuke. |
| 53 | */ |
| 54 | displayName?: string; |
| 55 | /** |
| 56 | * OIDC identity provider configuration. |
| 57 | */ |
| 58 | idpConfig?: InboundOidcSsoProfileIdpConfig; |
| 59 | /** |
| 60 | * OIDC relying-party configuration. `clientSecret` is input-only. |
| 61 | */ |
| 62 | rpConfig?: InboundOidcSsoProfileRpConfig; |
| 63 | }; |
| 64 | |
| 65 | export type InboundOidcSsoProfile = Resource< |
| 66 | "GCP.CloudIdentity.InboundOidcSsoProfile", |
| 67 | InboundOidcSsoProfileProps, |
| 68 | { |
| 69 | /** Resource name `inboundOidcSsoProfiles/{id}`. */ |
| 70 | name: string; |
| 71 | /** Profile id (last path segment). */ |
| 72 | profileId: string; |
| 73 | /** Customer. */ |
| 74 | customer: string | undefined; |
| 75 | /** Display name with the Alchemy ownership prefix stripped. */ |
| 76 | displayName: string | undefined; |
| 77 | /** IdP configuration. */ |
| 78 | idpConfig: InboundOidcSsoProfileIdpConfig | undefined; |
| 79 | /** RP configuration (secret omitted). */ |
| 80 | rpConfig: |
| 81 | | { |
| 82 | clientId?: string; |
| 83 | redirectUris?: string[]; |
| 84 | } |
| 85 | | undefined; |
| 86 | }, |
| 87 | never, |
| 88 | Providers |
| 89 | >; |
| 90 | |
| 91 | /** |
| 92 | * An inbound OIDC SSO profile for a Google enterprise customer. |
| 93 | * |
| 94 | * Profiles have no labels field, so Alchemy stamps ownership into |
| 95 | * `displayName` for `list` / nuke. `customer` is identity; display |
| 96 | * name and IdP / RP config update in place. |
| 97 | * |
| 98 | * ### Creating a Profile |
| 99 | * **Example:** Basic OIDC IdP |
| 100 | * ```typescript |
| 101 | * const profile = yield* GCP.CloudIdentity.InboundOidcSsoProfile("Okta", { |
| 102 | * displayName: "Okta", |
| 103 | * idpConfig: { issuerUri: "https://idp.example.com" }, |
| 104 | * rpConfig: { clientId: "google-rp", clientSecret: "secret" }, |
| 105 | * }); |
| 106 | * ``` |
| 107 | * |
| 108 | * @resource |
| 109 | * @category CloudIdentity |
| 110 | */ |
| 111 | export const InboundOidcSsoProfile = Resource<InboundOidcSsoProfile>( |
| 112 | "GCP.CloudIdentity.InboundOidcSsoProfile", |
| 113 | ); |
| 114 | |
| 115 | export class InboundOidcSsoProfileNotResolved extends Data.TaggedError( |
| 116 | "GCP.CloudIdentity.InboundOidcSsoProfileNotResolved", |
| 117 | )<{ |
| 118 | resourceName: string; |
| 119 | }> {} |
| 120 | |
| 121 | const rpOf = (rp: cloudidentity.OidcRpConfig | undefined) => { |
| 122 | if (rp === undefined) return undefined; |
| 123 | return { |
| 124 | clientId: rp.clientId, |
| 125 | redirectUris: rp.redirectUris, |
| 126 | }; |
| 127 | }; |
| 128 | |
| 129 | const toAttrs = (profile: cloudidentity.InboundOidcSsoProfile) => { |
| 130 | const name = profile.name ?? ""; |
| 131 | return { |
| 132 | name, |
| 133 | profileId: lastSegment(name), |
| 134 | customer: profile.customer, |
| 135 | displayName: parseOwnership(profile.displayName).text, |
| 136 | idpConfig: profile.idpConfig |
| 137 | ? { |
| 138 | issuerUri: profile.idpConfig.issuerUri, |
| 139 | changePasswordUri: profile.idpConfig.changePasswordUri, |
| 140 | } |
| 141 | : undefined, |
| 142 | rpConfig: rpOf(profile.rpConfig), |
| 143 | }; |
| 144 | }; |
| 145 | |
| 146 | const observeProfile = (input: { id: string; name?: string }) => |
| 147 | Effect.gen(function* () { |
| 148 | const byName = yield* getOidcProfile(input.name ?? ""); |
| 149 | if (byName !== undefined) return byName; |
| 150 | return yield* findOwnedOidcProfile(input.id); |
| 151 | }); |
| 152 | |
| 153 | export const InboundOidcSsoProfileProvider = () => |
| 154 | Provider.succeed(InboundOidcSsoProfile, { |
| 155 | stables: ["name", "profileId", "customer"], |
| 156 | |
| 157 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 158 | if (!isResolved(news)) return undefined; |
| 159 | const previous = olds?.customer ?? output?.customer; |
| 160 | const next = news.customer !== undefined ? normalizeCustomer(news.customer) : previous; |
| 161 | return replaceOnIdentity({ |
| 162 | previousParent: previous, |
| 163 | nextParent: next, |
| 164 | }); |
| 165 | }), |
| 166 | |
| 167 | read: Effect.fn(function* ({ id, output }) { |
| 168 | const existing = yield* observeProfile({ id, name: output?.name }); |
| 169 | if (existing === undefined) return undefined; |
| 170 | const attrs = toAttrs(existing); |
| 171 | return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs); |
| 172 | }), |
| 173 | |
| 174 | list: () => |
| 175 | Effect.gen(function* () { |
| 176 | const profiles = yield* listOidcProfiles(); |
| 177 | return profiles.filter((profile) => hasOwnershipMarker(profile.displayName)).map(toAttrs); |
| 178 | }), |
| 179 | |
| 180 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 181 | const ownership = yield* ownershipLabels(id); |
| 182 | const customer = normalizeCustomer(news.customer ?? output?.customer); |
| 183 | const displayName = encodeOwnershipLine( |
| 184 | ownership, |
| 185 | yield* toPhysicalId(id, news.displayName, output?.displayName, MAX_DISPLAY_NAME_LENGTH), |
| 186 | MAX_DISPLAY_NAME_LENGTH, |
| 187 | ); |
| 188 | const desired: cloudidentity.InboundOidcSsoProfile = { |
| 189 | customer, |
| 190 | displayName, |
| 191 | idpConfig: news.idpConfig, |
| 192 | rpConfig: news.rpConfig, |
| 193 | }; |
| 194 | |
| 195 | let current = yield* observeProfile({ id, name: output?.name }); |
| 196 | |
| 197 | if (current === undefined) { |
| 198 | const created = yield* cloudidentity |
| 199 | .createInboundOidcSsoProfiles({ body: desired }) |
| 200 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 201 | if (created !== undefined) { |
| 202 | yield* waitForOperation(created).pipe( |
| 203 | Effect.catchTag("GCP.CloudIdentity.OperationPending", () => Effect.void), |
| 204 | ); |
| 205 | const createdName = resourceNameFromOperation(created); |
| 206 | if (createdName !== undefined) { |
| 207 | current = yield* getOidcProfile(createdName); |
| 208 | } |
| 209 | } |
| 210 | if (current === undefined) { |
| 211 | current = yield* waitUntilPresent( |
| 212 | observeProfile({ id, name: output?.name }), |
| 213 | displayName, |
| 214 | ).pipe( |
| 215 | Effect.catchTag("GCP.CloudIdentity.OperationPending", () => observeProfile({ id })), |
| 216 | ); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (current === undefined) { |
| 221 | return yield* new InboundOidcSsoProfileNotResolved({ |
| 222 | resourceName: output?.name ?? displayName, |
| 223 | }); |
| 224 | } |
| 225 | |
| 226 | const name = current.name ?? output?.name ?? ""; |
| 227 | const displayChanged = !sameText(current.displayName, displayName); |
| 228 | const idpChanged = !sameJson(current.idpConfig, news.idpConfig ?? {}); |
| 229 | const rpChanged = |
| 230 | news.rpConfig !== undefined && |
| 231 | (!sameText(current.rpConfig?.clientId, news.rpConfig.clientId) || |
| 232 | news.rpConfig.clientSecret !== undefined); |
| 233 | const updateMask = updateMaskOf( |
| 234 | displayChanged ? "display_name" : undefined, |
| 235 | idpChanged ? "idp_config" : undefined, |
| 236 | rpChanged ? "rp_config" : undefined, |
| 237 | ); |
| 238 | |
| 239 | if (updateMask.length > 0 && name.length > 0) { |
| 240 | const patched = yield* cloudidentity.patchInboundOidcSsoProfiles({ |
| 241 | name, |
| 242 | updateMask, |
| 243 | body: { |
| 244 | displayName, |
| 245 | idpConfig: news.idpConfig, |
| 246 | rpConfig: news.rpConfig, |
| 247 | }, |
| 248 | }); |
| 249 | yield* waitForOperation(patched).pipe( |
| 250 | Effect.catchTag("GCP.CloudIdentity.OperationPending", () => Effect.void), |
| 251 | ); |
| 252 | current = (yield* getOidcProfile(name)) ?? current; |
| 253 | } |
| 254 | |
| 255 | return toAttrs(current); |
| 256 | }), |
| 257 | |
| 258 | delete: Effect.fn(function* ({ output }) { |
| 259 | if (output.name.length === 0) return; |
| 260 | const deleted = yield* cloudidentity |
| 261 | .deleteInboundOidcSsoProfiles({ name: output.name }) |
| 262 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 263 | if (deleted !== undefined) { |
| 264 | yield* waitForOperation(deleted, { notFoundOk: true }); |
| 265 | } |
| 266 | }), |
| 267 | }); |