The best way to control Claude Code, Codex and more from any computer or phone

main
ts 82 lines 3.65 KB
Raw
1 import * as ci from "@distilled.cloud/aws/cognito-identity";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import { cognitoMethods } from "./BindingHttp.ts";
6 import type { IdentityPool } from "./IdentityPool.ts";
7 import { IdentityPoolAdmin, type IdentityPoolAdminClient } from "./IdentityPoolAdmin.ts";
8
9 /** The injected identifier field, in the distilled wire type. */
10 type IdentityPoolIdField = Pick<ci.ListIdentitiesInput, "IdentityPoolId">;
11
12 /**
13 * HTTP implementation of {@link IdentityPoolAdmin}: grants the
14 * `cognito-identity:*` identity-management actions on the bound pool's ARN
15 * and calls the Cognito Identity HTTP API with the function's IAM
16 * credentials.
17 */
18 export const IdentityPoolAdminHttp = Layer.effect(
19 IdentityPoolAdmin,
20 Effect.gen(function* () {
21 const bind = yield* AWSBindings.Bind;
22 const describeIdentity = yield* AWSBindings.withCredentials(ci.describeIdentity);
23 const listIdentities = yield* AWSBindings.withCredentials(ci.listIdentities);
24 const deleteIdentities = yield* AWSBindings.withCredentials(ci.deleteIdentities);
25 const lookupDeveloperIdentity = yield* AWSBindings.withCredentials(ci.lookupDeveloperIdentity);
26 const mergeDeveloperIdentities = yield* AWSBindings.withCredentials(
27 ci.mergeDeveloperIdentities,
28 );
29 const unlinkDeveloperIdentity = yield* AWSBindings.withCredentials(ci.unlinkDeveloperIdentity);
30 const getOpenIdTokenForDeveloperIdentity = yield* AWSBindings.withCredentials(
31 ci.getOpenIdTokenForDeveloperIdentity,
32 );
33
34 return Effect.fn(function* <P extends IdentityPool>(pool: P) {
35 const IdentityPoolId = yield* pool.identityPoolId;
36 if (!globalThis.__ALCHEMY_RUNTIME__) {
37 yield* bind`AWS.Cognito.IdentityPoolAdmin(${pool})`({
38 policyStatements: [
39 {
40 Effect: "Allow",
41 Action: [
42 "cognito-identity:GetOpenIdTokenForDeveloperIdentity",
43 "cognito-identity:ListIdentities",
44 "cognito-identity:LookupDeveloperIdentity",
45 "cognito-identity:MergeDeveloperIdentities",
46 "cognito-identity:UnlinkDeveloperIdentity",
47 ],
48 Resource: [pool.identityPoolArn],
49 },
50 {
51 Effect: "Allow",
52 // These act on identity IDs, which have no ARN form —
53 // Cognito Identity does not support resource-level
54 // scoping for them.
55 Action: ["cognito-identity:DeleteIdentities", "cognito-identity:DescribeIdentity"],
56 Resource: ["*"],
57 },
58 ],
59 });
60 }
61 const methods = cognitoMethods("AWS.Cognito.IdentityPoolAdmin", pool.LogicalId);
62 const withPool = methods.injecting(
63 Effect.map(IdentityPoolId, (id): IdentityPoolIdField => ({
64 IdentityPoolId: id,
65 })),
66 );
67 const adminClient: IdentityPoolAdminClient = {
68 describeIdentity: methods.plain("describeIdentity", describeIdentity),
69 deleteIdentities: methods.plain("deleteIdentities", deleteIdentities),
70 listIdentities: withPool("listIdentities", listIdentities),
71 lookupDeveloperIdentity: withPool("lookupDeveloperIdentity", lookupDeveloperIdentity),
72 mergeDeveloperIdentities: withPool("mergeDeveloperIdentities", mergeDeveloperIdentities),
73 unlinkDeveloperIdentity: withPool("unlinkDeveloperIdentity", unlinkDeveloperIdentity),
74 getOpenIdTokenForDeveloperIdentity: withPool(
75 "getOpenIdTokenForDeveloperIdentity",
76 getOpenIdTokenForDeveloperIdentity,
77 ),
78 };
79 return adminClient;
80 });
81 }),
82 );