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

main
mdx 171 lines 5.08 KB
Raw
1 ---
2 title: "Part 2: Control access"
3 description: Protect the existing Git host with one shared credential, then verify both accepted and rejected requests.
4 ---
5
6 import PackageManagers from "../../../../components/PackageManagers.astro";
7
8 The host from [Part 1](/git/tutorial/part-1) accepts requests from anyone.
9 Add a single shared credential so only people who have it can use the host.
10 This part changes access to the existing repositories; it does not recreate them.
11
12 ## Generate the credential
13
14 Create `src/secret.ts`:
15
16 ```typescript
17 // src/secret.ts
18 import * as Alchemy from "alchemy";
19 import * as Effect from "effect/Effect";
20
21 export const GitSecret = Effect.gen(function* () {
22 const Random = yield* Alchemy.Random;
23 return yield* Random("GitSecret");
24 });
25 ```
26
27 Alchemy creates this random value on the first deploy and keeps it stable across
28 updates. It is the password Git clients will send.
29
30 ## Check the password on each request
31
32 Create `src/middleware.ts`:
33
34 ```typescript
35 // src/middleware.ts
36 import { RuntimeContext } from "alchemy";
37 import * as Effect from "effect/Effect";
38 import * as Redacted from "effect/Redacted";
39 import * as HttpRouter from "effect/http/HttpRouter";
40 import * as HttpServerResponse from "effect/http/HttpServerResponse";
41 import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
42 import * as HttpApiSecurity from "effect/http-api/HttpApiSecurity";
43 import { GitSecret } from "./secret.ts";
44
45 export const Authentication = HttpRouter.middleware(
46 Effect.gen(function* () {
47 const secret = yield* (yield* GitSecret).text;
48 return (httpEffect) =>
49 Effect.gen(function* () {
50 const { password } = yield* HttpApiBuilder.securityDecode(
51 HttpApiSecurity.basic,
52 );
53 if (Redacted.value(password) === Redacted.value(yield* secret)) {
54 return yield* httpEffect;
55 }
56 return HttpServerResponse.empty({
57 status: 401,
58 headers: { "www-authenticate": 'Basic realm="git"' },
59 });
60 }).pipe(Effect.provide(RuntimeContext.phantom));
61 }),
62 );
63 ```
64
65 HTTP Basic has a username and password. This policy ignores the username and
66 checks the password. A matching credential continues to the Git handler; other
67 requests receive `401`. `WWW-Authenticate` tells Git to prompt for a credential.
68 The Worker supplies `RuntimeContext` per request; the phantom layer accounts for
69 that service in the middleware's type.
70
71 ## Apply the check to Git's routes
72
73 ```diff lang="typescript"
74 // src/git.ts
75 import * as Layer from "effect/Layer";
76 +import { Authentication } from "./middleware.ts";
77 ```
78
79 Replace the start of the route assembly:
80
81 ```diff lang="typescript"
82 // src/git.ts
83 -export const GitLive = Git.ApiLive.pipe(
84 +const PublicRoutes = Git.ApiLive.pipe(
85 + Layer.provide(Authentication.layer),
86 +);
87 +
88 +export const GitLive = PublicRoutes.pipe(
89 Layer.provide(Git.ApiHandlersLive),
90 ```
91
92 `PublicRoutes` means the routes exposed to Git and API clients. They now all pass
93 through your middleware, including repository management, pushes, and clones.
94
95 ## Output the credential
96
97 Add these imports to `alchemy.run.ts`:
98
99 ```diff lang="typescript"
100 // alchemy.run.ts
101 import * as Effect from "effect/Effect";
102 +import * as Output from "alchemy/Output";
103 +import * as Redacted from "effect/Redacted";
104 +import { GitSecret } from "./src/secret.ts";
105 ```
106
107 Then include the generated value in the stack's outputs:
108
109 ```diff lang="typescript"
110 // alchemy.run.ts
111 const host = yield* GitHost;
112 - return { url: host.url.as<string>() };
113 + const secret = yield* GitSecret;
114 + return {
115 + url: host.url.as<string>(),
116 + secret: Output.map(secret.text, Redacted.value),
117 + };
118 ```
119
120 This exposes the shared credential in the deployment output so you can copy it
121 for the tutorial. Anyone with this credential has full access to the host.
122
123 ## Deploy the access policy
124
125 <PackageManagers run="alchemy deploy" />
126
127 Copy the printed credential:
128
129 ```sh
130 export GIT_SECRET="paste-the-secret-output"
131 ```
132
133 Keep `$HOST` from Part 1. The Worker URL and `acme/web` repository are unchanged.
134
135 ## Verify that anonymous requests fail
136
137 ```sh
138 curl -i "$HOST/api/v1/repos"
139 ```
140
141 Expect `401` and a `WWW-Authenticate: Basic realm="git"` header. If you still see
142 the old `200` response immediately after deploying, repeat this check while the
143 update propagates; access is protected only once the new policy is serving.
144 The same request
145 with the credential succeeds:
146
147 ```sh
148 curl --fail-with-body -u "x:$GIT_SECRET" "$HOST/api/v1/repos"
149 ```
150
151 You should see the repository created in Part 1.
152
153 ## Push with the credential
154
155 ```sh
156 printf 'Access is now protected.\n' >> work/README.md
157 git -C work commit -am "Require a credential"
158 git -c credential.helper= -C work push origin main
159 ```
160
161 When prompted, enter `x` as the username and the value of `$GIT_SECRET` as the
162 password. The push should succeed. Clone with the same credentials to verify
163 that authenticated reads work too:
164
165 ```sh
166 git -c credential.helper= clone "$HOST/acme/web.git" private-copy
167 git -C private-copy fsck --strict
168 ```
169
170 Continue to [Part 3: Publish a repository](/git/tutorial/part-3), where reads of
171 public repositories become available without the shared credential.