@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 224 lines 7.3 KB
Raw
1 import * as iap from "@distilled.cloud/gcp/iap_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 { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 brandNameOf,
12 encodeOwnershipLine,
13 findOwnedClient,
14 getClient,
15 listOwnedClients,
16 MAX_DISPLAY_NAME_LENGTH,
17 ownedByAlchemy,
18 ownershipLabels,
19 parseClientName,
20 parseOwnership,
21 replaceOnIdentity,
22 sameText,
23 toGeneratedDisplayName,
24 waitUntilGone,
25 } from "./internal.ts";
26
27 export type BrandsIdentityAwareProxyClientProps = {
28 /**
29 * Parent IAP OAuth brand. Full name
30 * `projects/{project}/brands/{brand}` or the brand id. Immutable —
31 * changing it replaces the client. The project must belong to a
32 * Google Workspace organization, and the brand must already exist.
33 */
34 brand: string;
35 /**
36 * Human-friendly OAuth client name. IAP clients have no labels field,
37 * so Alchemy stamps ownership into a `[alchemy …]` prefix and strips
38 * it from attributes. Immutable — the API has no update, so changing
39 * this replaces the client.
40 */
41 displayName?: string;
42 /**
43 * Server-assigned OAuth client id (the `{client_id}` segment of
44 * `projects/{project}/brands/{brand}/identityAwareProxyClients/{client_id}`).
45 * Leave blank on create. Immutable — changing it replaces the client.
46 */
47 identityAwareProxyClientId?: string;
48 };
49
50 export type BrandsIdentityAwareProxyClient = Resource<
51 "GCP.IAP.BrandsIdentityAwareProxyClient",
52 BrandsIdentityAwareProxyClientProps,
53 {
54 /** Full resource name `projects/{project}/brands/{brand}/identityAwareProxyClients/{client_id}`. */
55 name: string;
56 /** Server-assigned OAuth client id. */
57 identityAwareProxyClientId: string;
58 /** Parent brand resource name. */
59 brand: string;
60 /** Project id. */
61 project: string;
62 /** User display name with the Alchemy ownership prefix stripped. */
63 displayName: string | undefined;
64 /**
65 * OAuth client secret. Returned on create; later reads reuse the
66 * previously persisted value when the API omits it.
67 */
68 secret: string | undefined;
69 },
70 never,
71 Providers
72 >;
73
74 /**
75 * An Identity-Aware Proxy owned OAuth client.
76 *
77 * IAP OAuth clients have no labels field, so Alchemy stamps ownership
78 * into `displayName` for `list` / nuke. Brand, client id, and display
79 * name are identity — the API has no patch, so changing any of them
80 * replaces the client. Creating a client requires an existing
81 * internal-only OAuth brand on a Workspace project.
82 *
83 * ### Creating an IAP OAuth Client
84 * **Example:** Named client under an existing brand
85 * ```typescript
86 * const client = yield* GCP.IAP.BrandsIdentityAwareProxyClient("Console", {
87 * brand: "projects/my-project/brands/123456",
88 * displayName: "Alchemy console",
89 * });
90 * ```
91 *
92 * **Example:** Generated display name
93 * ```typescript
94 * const client = yield* GCP.IAP.BrandsIdentityAwareProxyClient("Console", {
95 * brand: existingBrandName,
96 * });
97 * ```
98 *
99 * @resource
100 * @category IAP
101 */
102 export const BrandsIdentityAwareProxyClient = Resource<BrandsIdentityAwareProxyClient>(
103 "GCP.IAP.BrandsIdentityAwareProxyClient",
104 );
105
106 export class BrandsIdentityAwareProxyClientNotResolved extends Data.TaggedError(
107 "GCP.IAP.BrandsIdentityAwareProxyClientNotResolved",
108 )<{
109 resourceName: string;
110 }> {}
111
112 const toAttrs = (
113 client: iap.IdentityAwareProxyClient,
114 project: string,
115 previousSecret?: string,
116 ) => {
117 const name = client.name ?? "";
118 const parsed = parseClientName(name, project);
119 return {
120 name,
121 identityAwareProxyClientId: parsed.identityAwareProxyClientId,
122 brand: parsed.brand,
123 project,
124 displayName: parseOwnership(client.displayName).text,
125 secret: client.secret ?? previousSecret,
126 };
127 };
128
129 export const BrandsIdentityAwareProxyClientProvider = () =>
130 Provider.succeed(BrandsIdentityAwareProxyClient, {
131 stables: ["name", "identityAwareProxyClientId", "brand", "project", "secret"],
132
133 diff: Effect.fn(function* ({ news, olds, output }) {
134 if (!isResolved(news)) return undefined;
135 return replaceOnIdentity({
136 previousId: olds?.identityAwareProxyClientId ?? output?.identityAwareProxyClientId,
137 nextId: news.identityAwareProxyClientId,
138 previousParent: olds?.brand ?? output?.brand,
139 nextParent: news.brand,
140 extra:
141 news.displayName !== undefined &&
142 output?.displayName !== undefined &&
143 !sameText(news.displayName, output.displayName),
144 });
145 }),
146
147 read: Effect.fn(function* ({ id, olds, output }) {
148 const env = yield* GcpEnvironment.current;
149 const brand = brandNameOf(env.project, olds?.brand ?? output?.brand ?? "");
150 let existing = yield* getClient(output?.name ?? "");
151 if (existing === undefined) {
152 existing = yield* findOwnedClient(
153 env.project,
154 id,
155 brand,
156 olds?.displayName ?? output?.displayName,
157 );
158 }
159 if (existing === undefined) return undefined;
160 const attrs = toAttrs(existing, env.project, output?.secret);
161 return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
162 }),
163
164 list: () =>
165 Effect.gen(function* () {
166 const env = yield* GcpEnvironment.current;
167 const clients = yield* listOwnedClients(env.project);
168 return clients.map((client) => toAttrs(client, env.project));
169 }),
170
171 reconcile: Effect.fn(function* ({ id, news, output }) {
172 const env = yield* GcpEnvironment.current;
173 const brand = brandNameOf(env.project, news.brand);
174 const ownership = yield* ownershipLabels(id);
175 const userDisplayName = yield* toGeneratedDisplayName(
176 id,
177 news.displayName,
178 output?.displayName,
179 );
180 const displayName = encodeOwnershipLine(ownership, userDisplayName, MAX_DISPLAY_NAME_LENGTH);
181 const name =
182 output?.name ??
183 (news.identityAwareProxyClientId
184 ? `${brand}/identityAwareProxyClients/${news.identityAwareProxyClientId}`
185 : "");
186
187 let current = yield* getClient(name);
188 if (current === undefined) {
189 current = yield* findOwnedClient(env.project, id, brand, userDisplayName);
190 }
191
192 if (current === undefined) {
193 const created = yield* iap
194 .createProjectsBrandsIdentityAwareProxyClients({
195 parent: brand,
196 body: { displayName },
197 })
198 .pipe(
199 Effect.catchTag("Conflict", () =>
200 findOwnedClient(env.project, id, brand, userDisplayName),
201 ),
202 );
203 current = created ?? undefined;
204 }
205
206 if (current === undefined) {
207 return yield* new BrandsIdentityAwareProxyClientNotResolved({
208 resourceName: name || `${brand}/identityAwareProxyClients`,
209 });
210 }
211
212 return toAttrs(current, env.project, output?.secret);
213 }),
214
215 delete: Effect.fn(function* ({ output }) {
216 if (!output.name) return;
217 yield* iap
218 .deleteProjectsBrandsIdentityAwareProxyClients({
219 name: output.name,
220 })
221 .pipe(Effect.catchTag("NotFound", () => Effect.void));
222 yield* waitUntilGone(getClient(output.name));
223 }),
224 });