The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AVP from "@distilled.cloud/aws/verifiedpermissions"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as AWSBindings from "../AWSBindings.ts"; |
| 5 | import { |
| 6 | type BatchIsAuthorizedRequest, |
| 7 | type BatchIsAuthorizedWithTokenRequest, |
| 8 | IsAuthorized, |
| 9 | type IsAuthorizedRequest, |
| 10 | type IsAuthorizedWithTokenRequest, |
| 11 | } from "./IsAuthorized.ts"; |
| 12 | import type { PolicyStore } from "./PolicyStore.ts"; |
| 13 | |
| 14 | export const IsAuthorizedHttp = Layer.effect( |
| 15 | IsAuthorized, |
| 16 | Effect.gen(function* () { |
| 17 | const bind = yield* AWSBindings.Bind; |
| 18 | const isAuthorized = yield* AWSBindings.withCredentials(AVP.isAuthorized); |
| 19 | const isAuthorizedWithToken = yield* AWSBindings.withCredentials(AVP.isAuthorizedWithToken); |
| 20 | const batchIsAuthorized = yield* AWSBindings.withCredentials(AVP.batchIsAuthorized); |
| 21 | const batchIsAuthorizedWithToken = yield* AWSBindings.withCredentials( |
| 22 | AVP.batchIsAuthorizedWithToken, |
| 23 | ); |
| 24 | |
| 25 | return Effect.fn(function* <S extends PolicyStore>(store: S) { |
| 26 | const PolicyStoreId = yield* store.policyStoreId; |
| 27 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 28 | yield* bind`AWS.VerifiedPermissions.IsAuthorized(${store})`({ |
| 29 | policyStatements: [ |
| 30 | { |
| 31 | Effect: "Allow", |
| 32 | // the batch operations authorize under the non-batch |
| 33 | // verifiedpermissions:IsAuthorized[WithToken] actions |
| 34 | Action: [ |
| 35 | "verifiedpermissions:IsAuthorized", |
| 36 | "verifiedpermissions:IsAuthorizedWithToken", |
| 37 | "verifiedpermissions:BatchIsAuthorized", |
| 38 | "verifiedpermissions:BatchIsAuthorizedWithToken", |
| 39 | ], |
| 40 | Resource: [store.policyStoreArn], |
| 41 | }, |
| 42 | ], |
| 43 | }); |
| 44 | } |
| 45 | const label = store.LogicalId; |
| 46 | return { |
| 47 | isAuthorized: Effect.fn(`AWS.VerifiedPermissions.IsAuthorized(${label})`)(function* ( |
| 48 | request: IsAuthorizedRequest, |
| 49 | ) { |
| 50 | const policyStoreId = yield* PolicyStoreId; |
| 51 | return yield* isAuthorized({ ...request, policyStoreId }); |
| 52 | }), |
| 53 | isAuthorizedWithToken: Effect.fn(`AWS.VerifiedPermissions.IsAuthorizedWithToken(${label})`)( |
| 54 | function* (request: IsAuthorizedWithTokenRequest) { |
| 55 | const policyStoreId = yield* PolicyStoreId; |
| 56 | return yield* isAuthorizedWithToken({ ...request, policyStoreId }); |
| 57 | }, |
| 58 | ), |
| 59 | batchIsAuthorized: Effect.fn(`AWS.VerifiedPermissions.BatchIsAuthorized(${label})`)( |
| 60 | function* (request: BatchIsAuthorizedRequest) { |
| 61 | const policyStoreId = yield* PolicyStoreId; |
| 62 | return yield* batchIsAuthorized({ ...request, policyStoreId }); |
| 63 | }, |
| 64 | ), |
| 65 | batchIsAuthorizedWithToken: Effect.fn( |
| 66 | `AWS.VerifiedPermissions.BatchIsAuthorizedWithToken(${label})`, |
| 67 | )(function* (request: BatchIsAuthorizedWithTokenRequest) { |
| 68 | const policyStoreId = yield* PolicyStoreId; |
| 69 | return yield* batchIsAuthorizedWithToken({ |
| 70 | ...request, |
| 71 | policyStoreId, |
| 72 | }); |
| 73 | }), |
| 74 | }; |
| 75 | }); |
| 76 | }), |
| 77 | ); |