@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 207 lines 7.08 KB
Raw
1 import * as cip from "@distilled.cloud/aws/cognito-identity-provider";
2 import { isResolved } from "alchemy/Diff";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import type { Providers } from "../Providers.ts";
8
9 /** An OAuth scope defined by a resource server. */
10 export interface ResourceServerScope {
11 /** Scope name; clients request it as `<identifier>/<scopeName>`. */
12 scopeName: string;
13 /** Human-readable description of the scope. */
14 scopeDescription: string;
15 }
16
17 export interface ResourceServerProps {
18 /**
19 * The ID of the user pool the resource server belongs to. Changing this
20 * triggers a replacement.
21 */
22 userPoolId: string;
23 /**
24 * Unique identifier of the resource server — conventionally an API URL
25 * like `https://api.example.com`. If omitted, a deterministic identifier
26 * is generated from the app, stage, and logical ID. Changing this
27 * triggers a replacement.
28 */
29 identifier?: string;
30 /**
31 * Friendly name of the resource server. Defaults to the identifier.
32 */
33 name?: string;
34 /**
35 * OAuth scopes (up to 100) exposed by this resource server. Clients
36 * reference them as `<identifier>/<scopeName>` in `allowedOAuthScopes`.
37 */
38 scopes?: ResourceServerScope[];
39 }
40
41 export interface ResourceServer extends Resource<
42 "AWS.Cognito.ResourceServer",
43 ResourceServerProps,
44 {
45 /** The unique identifier of the resource server. */
46 identifier: string;
47 /** The ID of the user pool the resource server belongs to. */
48 userPoolId: string;
49 /** The friendly name of the resource server. */
50 name: string;
51 },
52 never,
53 Providers
54 > {}
55
56 /**
57 * An OAuth 2.0 resource server for an Amazon Cognito user pool. Resource
58 * servers declare custom scopes that app clients can request in
59 * `client_credentials` and authorization-code flows.
60 * ### Creating a Resource Server
61 * **Example:** API with Custom Scopes
62 * ```typescript
63 * import * as Cognito from "@alchemy.run/aws/Cognito";
64 *
65 * const pool = yield* Cognito.UserPool("Users", {});
66 * const api = yield* Cognito.ResourceServer("Api", {
67 * userPoolId: pool.userPoolId,
68 * identifier: "https://api.example.com",
69 * scopes: [
70 * { scopeName: "read", scopeDescription: "Read access" },
71 * { scopeName: "write", scopeDescription: "Write access" },
72 * ],
73 * });
74 * ```
75 *
76 * **Example:** Client Requesting Resource-Server Scopes
77 * ```typescript
78 * const client = yield* Cognito.UserPoolClient("Machine", {
79 * userPoolId: pool.userPoolId,
80 * generateSecret: true,
81 * allowedOAuthFlowsUserPoolClient: true,
82 * allowedOAuthFlows: ["client_credentials"],
83 * allowedOAuthScopes: ["https://api.example.com/read"],
84 * });
85 * ```
86 *
87 * @resource
88 */
89 export const ResourceServer = Resource<ResourceServer>("AWS.Cognito.ResourceServer");
90
91 const toWireScopes = (scopes: ResourceServerScope[] | undefined) =>
92 scopes?.map((scope) => ({
93 ScopeName: scope.scopeName,
94 ScopeDescription: scope.scopeDescription,
95 }));
96
97 const canonicalScopes = (scopes: { ScopeName?: string; ScopeDescription?: string }[] | undefined) =>
98 (scopes ?? [])
99 .map((scope) => `${scope.ScopeName}:${scope.ScopeDescription}`)
100 .sort()
101 .join(",");
102
103 export const ResourceServerProvider = () =>
104 Provider.effect(
105 ResourceServer,
106 Effect.gen(function* () {
107 const createIdentifier = Effect.fn(function* (
108 id: string,
109 props: Pick<ResourceServerProps, "identifier">,
110 ) {
111 return props.identifier ?? (yield* createPhysicalName({ id, maxLength: 256 }));
112 });
113
114 const describeServer = Effect.fn(function* (userPoolId: string, identifier: string) {
115 return yield* cip
116 .describeResourceServer({
117 UserPoolId: userPoolId,
118 Identifier: identifier,
119 })
120 .pipe(
121 Effect.map((r) => r.ResourceServer),
122 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
123 );
124 });
125
126 return ResourceServer.Provider.of({
127 stables: ["identifier", "userPoolId"],
128
129 // Sub-resource keyed entirely by its user pool (userPoolId) with no global
130 // enumeration API of its own — nuke reaches it through the parent's
131 // deletion, so enumeration returns empty per the ProviderService
132 // doctrine.
133 list: () => Effect.succeed([]),
134
135 read: Effect.fn(function* ({ id, olds, output }) {
136 const userPoolId = output?.userPoolId ?? olds?.userPoolId;
137 if (userPoolId === undefined) return undefined;
138 const identifier = output?.identifier ?? (yield* createIdentifier(id, olds ?? {}));
139 const observed = yield* describeServer(userPoolId, identifier);
140 return observed === undefined
141 ? undefined
142 : {
143 identifier,
144 userPoolId,
145 name: observed.Name ?? identifier,
146 };
147 }),
148
149 diff: Effect.fn(function* ({ id, news, olds }) {
150 if (!isResolved(news)) return undefined;
151 const oldIdentifier = yield* createIdentifier(id, olds ?? {});
152 const newIdentifier = yield* createIdentifier(id, news ?? {});
153 if (oldIdentifier !== newIdentifier || olds?.userPoolId !== news?.userPoolId) {
154 return { action: "replace" } as const;
155 }
156 }),
157
158 reconcile: Effect.fn(function* ({ id, news, output, session }) {
159 const identifier = output?.identifier ?? (yield* createIdentifier(id, news));
160 const userPoolId = news.userPoolId;
161 const name = news.name ?? identifier;
162
163 // 1. OBSERVE
164 let observed = yield* describeServer(userPoolId, identifier);
165
166 // 2. ENSURE
167 if (observed === undefined) {
168 observed = yield* cip
169 .createResourceServer({
170 UserPoolId: userPoolId,
171 Identifier: identifier,
172 Name: name,
173 Scopes: toWireScopes(news.scopes),
174 })
175 .pipe(Effect.map((r) => r.ResourceServer));
176 } else {
177 // 3. SYNC — name and scopes are mutable in place.
178 const drift =
179 observed.Name !== name ||
180 canonicalScopes(observed.Scopes) !== canonicalScopes(toWireScopes(news.scopes));
181 if (drift) {
182 observed = yield* cip
183 .updateResourceServer({
184 UserPoolId: userPoolId,
185 Identifier: identifier,
186 Name: name,
187 Scopes: toWireScopes(news.scopes),
188 })
189 .pipe(Effect.map((r) => r.ResourceServer));
190 }
191 }
192
193 yield* session.note(identifier);
194 return { identifier, userPoolId, name };
195 }),
196
197 delete: Effect.fn(function* ({ output }) {
198 yield* cip
199 .deleteResourceServer({
200 UserPoolId: output.userPoolId,
201 Identifier: output.identifier,
202 })
203 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
204 }),
205 });
206 }),
207 );