@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
mdx 140 lines 4.19 KB
Raw
1 ---
2 title: "Part 3: Publish a repository"
3 description: Allow anonymous clones of public repositories while preserving authenticated writes.
4 ---
5
6 import PackageManagers from "../../../../components/PackageManagers.astro";
7
8 The credential from [Part 2](/git/tutorial/part-2) protects every request.
9 Now make `acme/web` readable by anyone while keeping writes protected.
10
11 ## Mark the repository public
12
13 ```sh
14 curl --fail-with-body -u "x:$GIT_SECRET" \
15 -X PATCH "$HOST/api/v1/repos/acme/web" \
16 -H "Content-Type: application/json" \
17 -d '{"public":true}'
18 ```
19
20 `public` is stored repository metadata. It does not bypass your middleware.
21 An anonymous request still receives `401` until you teach the policy to use it.
22
23 ## Look up the repository's visibility
24
25 Create `src/public-read.ts`:
26
27 ```typescript
28 // src/public-read.ts
29 import * as Git from "@alchemy.run/git";
30 import * as Effect from "effect/Effect";
31 import * as HttpRouter from "effect/http/HttpRouter";
32 import { HttpServerRequest } from "effect/http/HttpServerRequest";
33
34 export const PublicRead = Effect.gen(function* () {
35 const registry = yield* Git.RegistryStore;
36 return Effect.gen(function* () {
37 const request = yield* HttpServerRequest;
38 if (!Git.isRead(request)) return false;
39 const { owner, repo } = yield* HttpRouter.params;
40 if (owner === undefined || repo === undefined) return false;
41 const entry = yield* registry
42 .resolve(owner.toLowerCase(), repo.toLowerCase().replace(/\.git$/, ""))
43 .pipe(Effect.catchTag("StoreError", () => Effect.succeed(undefined)));
44 return entry?.public === true;
45 });
46 });
47 ```
48
49 The outer effect acquires the registry when the routes are built. The inner
50 effect checks the current request. It permits only reads of a named, public
51 repository. Listing every repository is not a read of one public repository.
52
53 `Git.isRead` understands Git's protocol: a clone includes a POST to
54 `git-upload-pack`, while discovery for a push must remain protected.
55
56 ## Permit public reads in the middleware
57
58 Import the check:
59
60 ```diff lang="typescript"
61 // src/middleware.ts
62 import { GitSecret } from "./secret.ts";
63 +import { PublicRead } from "./public-read.ts";
64 ```
65
66 Acquire it alongside the credential:
67
68 ```diff lang="typescript"
69 // src/middleware.ts
70 const secret = yield* (yield* GitSecret).text;
71 + const publicRead = yield* PublicRead;
72 ```
73
74 Before returning `401`, allow requests that pass the public-read check:
75
76 ```diff lang="typescript"
77 // src/middleware.ts
78 if (Redacted.value(password) === Redacted.value(yield* secret)) {
79 return yield* httpEffect;
80 }
81 + if (yield* publicRead) return yield* httpEffect;
82 return HttpServerResponse.empty({
83 ```
84
85 The existing `Git.RegistryDurableObject` layer supplies this new dependency.
86 The shared credential still allows all operations; anonymous callers only get
87 the public reads you just defined.
88
89 ## Deploy the policy
90
91 <PackageManagers run="alchemy deploy" />
92
93 This updates the Worker. The public flag you set earlier remains stored in the
94 registry.
95
96 ## Clone without credentials
97
98 ```sh
99 GIT_TERMINAL_PROMPT=0 git -c credential.helper= \
100 clone "$HOST/acme/web.git" public-copy
101 git -C public-copy fsck --strict
102 ```
103
104 The clone should succeed without a password. Disabling the credential helper
105 ensures this check does not accidentally use a saved credential.
106
107 ## Verify that anonymous writes still fail
108
109 ```sh
110 GIT_TERMINAL_PROMPT=0 git -c credential.helper= -C work \
111 push origin HEAD:anonymous-write
112 ```
113
114 Expect authentication to fail and no `anonymous-write` branch to be created.
115 An authenticated push from Part 2 still works.
116
117 ## Verify that private reads still fail
118
119 Temporarily make the repository private:
120
121 ```sh
122 curl --fail-with-body -u "x:$GIT_SECRET" \
123 -X PATCH "$HOST/api/v1/repos/acme/web" \
124 -H "Content-Type: application/json" \
125 -d '{"public":false}'
126 curl -i "$HOST/api/v1/repos/acme/web"
127 ```
128
129 The anonymous read must return `401`. Restore the public setting before
130 continuing:
131
132 ```sh
133 curl --fail-with-body -u "x:$GIT_SECRET" \
134 -X PATCH "$HOST/api/v1/repos/acme/web" \
135 -H "Content-Type: application/json" \
136 -d '{"public":true}'
137 ```
138
139 [Part 4: Give users their own credentials](/git/tutorial/part-4) replaces the
140 shared credential with accounts and individual API keys.