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

main
ts 175 lines 8.66 KB
Raw
1 import * as cip from "@distilled.cloud/aws/cognito-identity-provider";
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 { UserPool } from "./UserPool.ts";
7 import {
8 UserPoolAdmin,
9 type ListGroupsRequest,
10 type ListUsersRequest,
11 type UserPoolAdminClient,
12 } from "./UserPoolAdmin.ts";
13
14 /** The injected identifier field, in the distilled wire type. */
15 type UserPoolIdField = Pick<cip.AdminGetUserRequest, "UserPoolId">;
16
17 /**
18 * HTTP implementation of {@link UserPoolAdmin}: grants the admin
19 * `cognito-idp:*` actions on the bound pool's ARN and calls the Cognito
20 * HTTP API with the function's IAM credentials.
21 */
22 export const UserPoolAdminHttp = Layer.effect(
23 UserPoolAdmin,
24 Effect.gen(function* () {
25 const bind = yield* AWSBindings.Bind;
26 const adminCreateUser = yield* AWSBindings.withCredentials(cip.adminCreateUser);
27 const adminGetUser = yield* AWSBindings.withCredentials(cip.adminGetUser);
28 const adminSetUserPassword = yield* AWSBindings.withCredentials(cip.adminSetUserPassword);
29 const adminUpdateUserAttributes = yield* AWSBindings.withCredentials(
30 cip.adminUpdateUserAttributes,
31 );
32 const adminDeleteUserAttributes = yield* AWSBindings.withCredentials(
33 cip.adminDeleteUserAttributes,
34 );
35 const adminDeleteUser = yield* AWSBindings.withCredentials(cip.adminDeleteUser);
36 const adminConfirmSignUp = yield* AWSBindings.withCredentials(cip.adminConfirmSignUp);
37 const adminDisableUser = yield* AWSBindings.withCredentials(cip.adminDisableUser);
38 const adminEnableUser = yield* AWSBindings.withCredentials(cip.adminEnableUser);
39 const adminResetUserPassword = yield* AWSBindings.withCredentials(cip.adminResetUserPassword);
40 const adminInitiateAuth = yield* AWSBindings.withCredentials(cip.adminInitiateAuth);
41 const adminRespondToAuthChallenge = yield* AWSBindings.withCredentials(
42 cip.adminRespondToAuthChallenge,
43 );
44 const adminUserGlobalSignOut = yield* AWSBindings.withCredentials(cip.adminUserGlobalSignOut);
45 const adminAddUserToGroup = yield* AWSBindings.withCredentials(cip.adminAddUserToGroup);
46 const adminRemoveUserFromGroup = yield* AWSBindings.withCredentials(
47 cip.adminRemoveUserFromGroup,
48 );
49 const adminListGroupsForUser = yield* AWSBindings.withCredentials(cip.adminListGroupsForUser);
50 const adminSetUserMFAPreference = yield* AWSBindings.withCredentials(
51 cip.adminSetUserMFAPreference,
52 );
53 const adminLinkProviderForUser = yield* AWSBindings.withCredentials(
54 cip.adminLinkProviderForUser,
55 );
56 const adminDisableProviderForUser = yield* AWSBindings.withCredentials(
57 cip.adminDisableProviderForUser,
58 );
59 const adminGetDevice = yield* AWSBindings.withCredentials(cip.adminGetDevice);
60 const adminListDevices = yield* AWSBindings.withCredentials(cip.adminListDevices);
61 const adminForgetDevice = yield* AWSBindings.withCredentials(cip.adminForgetDevice);
62 const adminUpdateDeviceStatus = yield* AWSBindings.withCredentials(cip.adminUpdateDeviceStatus);
63 const adminListUserAuthEvents = yield* AWSBindings.withCredentials(cip.adminListUserAuthEvents);
64 const adminUpdateAuthEventFeedback = yield* AWSBindings.withCredentials(
65 cip.adminUpdateAuthEventFeedback,
66 );
67 const listUsers = yield* AWSBindings.withCredentials(cip.listUsers);
68 const listUsersInGroup = yield* AWSBindings.withCredentials(cip.listUsersInGroup);
69 const listGroups = yield* AWSBindings.withCredentials(cip.listGroups);
70
71 return Effect.fn(function* <P extends UserPool>(pool: P) {
72 const UserPoolId = yield* pool.userPoolId;
73 if (!globalThis.__ALCHEMY_RUNTIME__) {
74 yield* bind`AWS.Cognito.UserPoolAdmin(${pool})`({
75 policyStatements: [
76 {
77 Effect: "Allow",
78 Action: [
79 "cognito-idp:AdminAddUserToGroup",
80 "cognito-idp:AdminConfirmSignUp",
81 "cognito-idp:AdminCreateUser",
82 "cognito-idp:AdminDeleteUser",
83 "cognito-idp:AdminDeleteUserAttributes",
84 "cognito-idp:AdminDisableProviderForUser",
85 "cognito-idp:AdminDisableUser",
86 "cognito-idp:AdminEnableUser",
87 "cognito-idp:AdminForgetDevice",
88 "cognito-idp:AdminGetDevice",
89 "cognito-idp:AdminGetUser",
90 "cognito-idp:AdminInitiateAuth",
91 "cognito-idp:AdminLinkProviderForUser",
92 "cognito-idp:AdminListDevices",
93 "cognito-idp:AdminListGroupsForUser",
94 "cognito-idp:AdminListUserAuthEvents",
95 "cognito-idp:AdminRemoveUserFromGroup",
96 "cognito-idp:AdminResetUserPassword",
97 "cognito-idp:AdminRespondToAuthChallenge",
98 "cognito-idp:AdminSetUserMFAPreference",
99 "cognito-idp:AdminSetUserPassword",
100 "cognito-idp:AdminUpdateAuthEventFeedback",
101 "cognito-idp:AdminUpdateDeviceStatus",
102 "cognito-idp:AdminUpdateUserAttributes",
103 "cognito-idp:AdminUserGlobalSignOut",
104 "cognito-idp:ListGroups",
105 "cognito-idp:ListUsers",
106 "cognito-idp:ListUsersInGroup",
107 ],
108 Resource: [pool.userPoolArn],
109 },
110 ],
111 });
112 }
113 const methods = cognitoMethods("AWS.Cognito.UserPoolAdmin", pool.LogicalId);
114 const withPool = methods.injecting(
115 Effect.map(UserPoolId, (id): UserPoolIdField => ({ UserPoolId: id })),
116 );
117 const adminClient: UserPoolAdminClient = {
118 adminCreateUser: withPool("adminCreateUser", adminCreateUser),
119 adminGetUser: withPool("adminGetUser", adminGetUser),
120 adminSetUserPassword: withPool("adminSetUserPassword", adminSetUserPassword),
121 adminUpdateUserAttributes: withPool("adminUpdateUserAttributes", adminUpdateUserAttributes),
122 adminDeleteUserAttributes: withPool("adminDeleteUserAttributes", adminDeleteUserAttributes),
123 adminDeleteUser: withPool("adminDeleteUser", adminDeleteUser),
124 adminConfirmSignUp: withPool("adminConfirmSignUp", adminConfirmSignUp),
125 adminDisableUser: withPool("adminDisableUser", adminDisableUser),
126 adminEnableUser: withPool("adminEnableUser", adminEnableUser),
127 adminResetUserPassword: withPool("adminResetUserPassword", adminResetUserPassword),
128 adminInitiateAuth: withPool("adminInitiateAuth", adminInitiateAuth),
129 adminRespondToAuthChallenge: withPool(
130 "adminRespondToAuthChallenge",
131 adminRespondToAuthChallenge,
132 ),
133 adminUserGlobalSignOut: withPool("adminUserGlobalSignOut", adminUserGlobalSignOut),
134 adminAddUserToGroup: withPool("adminAddUserToGroup", adminAddUserToGroup),
135 adminRemoveUserFromGroup: withPool("adminRemoveUserFromGroup", adminRemoveUserFromGroup),
136 adminListGroupsForUser: withPool("adminListGroupsForUser", adminListGroupsForUser),
137 adminSetUserMFAPreference: withPool("adminSetUserMFAPreference", adminSetUserMFAPreference),
138 adminLinkProviderForUser: withPool("adminLinkProviderForUser", adminLinkProviderForUser),
139 adminDisableProviderForUser: withPool(
140 "adminDisableProviderForUser",
141 adminDisableProviderForUser,
142 ),
143 adminGetDevice: withPool("adminGetDevice", adminGetDevice),
144 adminListDevices: withPool("adminListDevices", adminListDevices),
145 adminForgetDevice: withPool("adminForgetDevice", adminForgetDevice),
146 adminUpdateDeviceStatus: withPool("adminUpdateDeviceStatus", adminUpdateDeviceStatus),
147 adminListUserAuthEvents: withPool("adminListUserAuthEvents", adminListUserAuthEvents),
148 adminUpdateAuthEventFeedback: withPool(
149 "adminUpdateAuthEventFeedback",
150 adminUpdateAuthEventFeedback,
151 ),
152 listUsersInGroup: withPool("listUsersInGroup", listUsersInGroup),
153 // optional-request list operations stay bespoke (the helper's
154 // wrapped methods take a required request object)
155 listUsers: Effect.fn(`AWS.Cognito.UserPoolAdmin.listUsers(${pool.LogicalId})`)(function* (
156 request: ListUsersRequest = {},
157 ) {
158 return yield* listUsers({
159 ...request,
160 UserPoolId: yield* UserPoolId,
161 });
162 }),
163 listGroups: Effect.fn(`AWS.Cognito.UserPoolAdmin.listGroups(${pool.LogicalId})`)(function* (
164 request: ListGroupsRequest = {},
165 ) {
166 return yield* listGroups({
167 ...request,
168 UserPoolId: yield* UserPoolId,
169 });
170 }),
171 };
172 return adminClient;
173 });
174 }),
175 );