1import * as ci from "@distilled.cloud/aws/cognito-identity";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Binding from "../../Binding.ts";
5import { isBindingHost } from "../Lambda/Function.ts";
6import { cognitoMethods } from "./BindingHttp.ts";
7import type { IdentityPool } from "./IdentityPool.ts";
8import {
9 IdentityPoolAuth,
10 type GetIdRequest,
11 type IdentityPoolAuthClient,
12} from "./IdentityPoolAuth.ts";
13
14/**
15 * HTTP implementation of {@link IdentityPoolAuth}. The credentials-vending
16 * flows are unauthenticated (Cognito does not evaluate IAM for them), so the
17 * deploy-time half only records the binding — no policy statements are
18 * attached.
19 */
20export const IdentityPoolAuthHttp = Layer.effect(
21 IdentityPoolAuth,
22 Effect.gen(function* () {
23 const getId = yield* ci.getId;
24 const getCredentialsForIdentity = yield* ci.getCredentialsForIdentity;
25 const getOpenIdToken = yield* ci.getOpenIdToken;
26 const unlinkIdentity = yield* ci.unlinkIdentity;
27
28 return Effect.fn(function* <P extends IdentityPool>(pool: P) {
29 const IdentityPoolId = yield* pool.identityPoolId;
30 if (!globalThis.__ALCHEMY_RUNTIME__) {
31 const host = yield* Binding.Host;
32 if (isBindingHost(host)) {
33 // No IAM is required for the public identity flows; the binding is
34 // recorded so the identity pool deploys before the function.
35 yield* host.bind`Allow(${host}, AWS.Cognito.IdentityPoolAuth(${pool}))`(
36 { policyStatements: [] },
37 );
38 }
39 }
40 const methods = cognitoMethods(
41 "AWS.Cognito.IdentityPoolAuth",
42 pool.LogicalId,
43 );
44 const authClient: IdentityPoolAuthClient = {
45 // optional-request operation stays bespoke (the helper's wrapped
46 // methods take a required request object)
47 getId: Effect.fn(
48 `AWS.Cognito.IdentityPoolAuth.getId(${pool.LogicalId})`,
49 )(function* (request: GetIdRequest = {}) {
50 return yield* getId({
51 ...request,
52 IdentityPoolId: yield* IdentityPoolId,
53 });
54 }),
55 getCredentialsForIdentity: methods.plain(
56 "getCredentialsForIdentity",
57 getCredentialsForIdentity,
58 ),
59 getOpenIdToken: methods.plain("getOpenIdToken", getOpenIdToken),
60 unlinkIdentity: methods.plain("unlinkIdentity", unlinkIdentity),
61 };
62 return authClient;
63 });
64 }),
65);