The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Git from "@alchemy.run/git"; |
| 2 | /** |
| 3 | * Who may call what. Native `HttpApiMiddleware` on the application's API, |
| 4 | * git's and ours: REST, the wire, the raw reads, and the GitHub facade. |
| 5 | * A user owns the repositories under their own name; anyone may read a |
| 6 | * public one. |
| 7 | */ |
| 8 | import { RuntimeContext } from "alchemy"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; |
| 11 | import * as HttpApiMiddleware from "effect/http-api/HttpApiMiddleware"; |
| 12 | import * as HttpApiSecurity from "effect/http-api/HttpApiSecurity"; |
| 13 | import * as HttpRouter from "effect/http/HttpRouter"; |
| 14 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 15 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 16 | import * as Layer from "effect/Layer"; |
| 17 | import * as Redacted from "effect/Redacted"; |
| 18 | import { Auth, Session, Unauthorized } from "./auth.ts"; |
| 19 | |
| 20 | /** A 401 that makes `git` ask for credentials. */ |
| 21 | const unauthorized = HttpServerResponse.jsonUnsafe( |
| 22 | { _tag: "Unauthorized" }, |
| 23 | { status: 401, headers: { "www-authenticate": 'Basic realm="git"' } }, |
| 24 | ); |
| 25 | |
| 26 | /** |
| 27 | * An API key in the password field of the remote names a user; otherwise |
| 28 | * the session cookie does; otherwise the request is anonymous. A user may |
| 29 | * do anything under their own owner name and use the routes that have no |
| 30 | * owner (create, list, import); anyone may read a public repository. |
| 31 | */ |
| 32 | export class Authentication extends HttpApiMiddleware.Service< |
| 33 | Authentication, |
| 34 | { provides: Session } |
| 35 | >()("app/Authentication", { error: Unauthorized }) { |
| 36 | static readonly layer = Layer.effect( |
| 37 | Authentication, |
| 38 | Effect.gen(function* () { |
| 39 | const auth = yield* Auth; |
| 40 | const registry = yield* Git.RegistryStore; |
| 41 | |
| 42 | const resolve = Effect.gen(function* () { |
| 43 | const { password } = yield* HttpApiBuilder.securityDecode(HttpApiSecurity.basic); |
| 44 | const key = Redacted.value(password); |
| 45 | if (key !== "") { |
| 46 | const verified = yield* auth.api |
| 47 | .verifyApiKey({ body: { key } }) |
| 48 | .pipe( |
| 49 | Effect.catchTag("BetterAuthApiError", () => |
| 50 | Effect.succeed({ valid: false as const, key: null }), |
| 51 | ), |
| 52 | ); |
| 53 | return verified.valid && verified.key ? { id: verified.key.referenceId } : undefined; |
| 54 | } |
| 55 | const session = yield* auth |
| 56 | .getSession() |
| 57 | .pipe(Effect.catchTag("BetterAuthApiError", () => Effect.succeed(null))); |
| 58 | return session ? { id: session.user.id, name: session.user.name } : undefined; |
| 59 | }); |
| 60 | |
| 61 | /** |
| 62 | * Anonymous may read one public repository. A repository that does |
| 63 | * not exist is the route's 404, so a 401 never confirms a private one. |
| 64 | */ |
| 65 | const publicRead = Effect.gen(function* () { |
| 66 | const params = yield* HttpRouter.params; |
| 67 | const owner = params.owner?.toLowerCase(); |
| 68 | const name = params.repo?.toLowerCase().replace(/\.git$/, ""); |
| 69 | if (owner === undefined || name === undefined) return false; |
| 70 | const entry = yield* registry |
| 71 | .resolve(owner, name) |
| 72 | .pipe(Effect.catchTag("StoreError", () => Effect.succeed(undefined))); |
| 73 | return entry === undefined || entry.public; |
| 74 | }); |
| 75 | |
| 76 | return (httpEffect) => |
| 77 | Effect.gen(function* () { |
| 78 | const user = yield* resolve; |
| 79 | const { owner } = yield* HttpRouter.params; |
| 80 | const own = owner === undefined || owner.toLowerCase() === user?.id.toLowerCase(); |
| 81 | if (user !== undefined && own) { |
| 82 | return yield* Effect.provideService(httpEffect, Session, { user }); |
| 83 | } |
| 84 | const request = yield* HttpServerRequest; |
| 85 | if (Git.isRead(request) && (yield* publicRead)) { |
| 86 | return yield* Effect.provideService(httpEffect, Session, { |
| 87 | user: user ?? null, |
| 88 | }); |
| 89 | } |
| 90 | return unauthorized; |
| 91 | }).pipe(Effect.provide(RuntimeContext.phantom)); |
| 92 | }), |
| 93 | ); |
| 94 | } |