| 1 | import * as Cloudflare from "@/Cloudflare/index.ts"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 4 | |
| 5 | /** |
| 6 | * Binds {@link Cloudflare.Access.GetIdentityProvider} so the deployed Worker |
| 7 | * can look up an Access IdP at runtime through the scoped token the binding |
| 8 | * mints and attaches. |
| 9 | */ |
| 10 | export default class IdpLookupWorker extends Cloudflare.Worker<IdpLookupWorker>()( |
| 11 | "IdpLookupWorker", |
| 12 | { |
| 13 | main: import.meta.url, |
| 14 | }, |
| 15 | Effect.gen(function* () { |
| 16 | const findIdp = yield* Cloudflare.Access.GetIdentityProvider({ |
| 17 | name: "alchemy-zt-idp-worker-lookup", |
| 18 | }); |
| 19 | |
| 20 | return { |
| 21 | fetch: Effect.gen(function* () { |
| 22 | const idp = yield* findIdp().pipe(Effect.orDie); |
| 23 | return yield* HttpServerResponse.json({ |
| 24 | identityProviderId: idp?.identityProviderId ?? null, |
| 25 | type: idp?.type ?? null, |
| 26 | }); |
| 27 | }), |
| 28 | }; |
| 29 | }).pipe(Effect.provide(Cloudflare.Access.GetIdentityProviderHttp)), |
| 30 | ) {} |