Skip to content

Commit 4289ec5

Browse files
committed
feat(source-control): confirm Tangled owner before publishing
1 parent 3791f19 commit 4289ec5

2 files changed

Lines changed: 80 additions & 23 deletions

File tree

‎packages/source-control-tangled/src/server/TangledSourceControlProvider.test.ts‎

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -463,27 +463,40 @@ describe("TangledSourceControlProvider.make", () => {
463463
}).pipe(serve(tg));
464464
});
465465

466-
it.effect("creates a repository and returns its clone URLs", () => {
467-
const tg = fakeTg({
468-
"repo create repo": {
469-
stdout: encodeJson({ name: "repo", author: "alice.example", knot: "knot1.tangled.sh" }),
470-
},
471-
});
472-
return Effect.gen(function* () {
473-
const provider = yield* TangledSourceControlProvider.make;
474-
const urls = yield* provider.createRepository({
475-
cwd: "/repo",
476-
repository: "alice.example/repo",
477-
visibility: "public",
478-
});
479-
assert.deepStrictEqual(urls, {
480-
nameWithOwner: "alice.example/repo",
481-
url: "https://tangled.org/alice.example/repo",
482-
sshUrl: "[email protected]:alice.example/repo",
466+
it.effect.each(["alice.example", "did:plc:alice"])(
467+
"creates a repository for the confirmed owner %s",
468+
(owner) => {
469+
const tg = fakeTg({
470+
"auth status": {
471+
stdout: encodeJson({
472+
authenticated: true,
473+
handle: "alice.example",
474+
did: "did:plc:alice",
475+
}),
476+
},
477+
"repo create repo": {
478+
stdout: encodeJson({ name: "repo", author: "alice.example", knot: "knot1.tangled.sh" }),
479+
},
483480
});
484-
assert.deepStrictEqual(tg.calls, [["repo", "create", "repo"]]);
485-
}).pipe(serve(tg));
486-
});
481+
return Effect.gen(function* () {
482+
const provider = yield* TangledSourceControlProvider.make;
483+
const urls = yield* provider.createRepository({
484+
cwd: "/repo",
485+
repository: `${owner}/repo`,
486+
visibility: "public",
487+
});
488+
assert.deepStrictEqual(urls, {
489+
nameWithOwner: `${owner}/repo`,
490+
url: `https://tangled.org/${owner}/repo`,
491+
sshUrl: `[email protected]:${owner}/repo`,
492+
});
493+
assert.deepStrictEqual(tg.calls, [
494+
["auth", "status"],
495+
["repo", "create", "repo"],
496+
]);
497+
}).pipe(serve(tg));
498+
},
499+
);
487500

488501
it.effect("rejects malformed repository paths before invoking tg", () => {
489502
const tg = fakeTg({});
@@ -508,8 +521,9 @@ describe("TangledSourceControlProvider.make", () => {
508521
}).pipe(serve(tg));
509522
});
510523

511-
it.effect("says so when tg created the repository under a different account", () => {
524+
it.effect("rejects a different signed-in handle before creating a repository", () => {
512525
const tg = fakeTg({
526+
"auth status": { stdout: encodeJson({ authenticated: true, handle: "alice.example" }) },
513527
"repo create repo": {
514528
stdout: encodeJson({ name: "repo", author: "alice.example", knot: "knot1.tangled.sh" }),
515529
},
@@ -519,12 +533,14 @@ describe("TangledSourceControlProvider.make", () => {
519533
const error = yield* provider
520534
.createRepository({ cwd: "/repo", repository: "someone-else/repo", visibility: "public" })
521535
.pipe(Effect.flip);
522-
assert.match(error.detail, /not someone-else/u);
536+
assert.match(error.detail, /someone-else/u);
537+
assert.deepStrictEqual(tg.calls, [["auth", "status"]]);
523538
}).pipe(serve(tg));
524539
});
525540

526-
it.effect("rejects clone URLs when tg created the repository under a different DID", () => {
541+
it.effect("rejects a different signed-in DID before creating a repository", () => {
527542
const tg = fakeTg({
543+
"auth status": { stdout: encodeJson({ authenticated: true, did: "did:plc:bob" }) },
528544
"repo create repo": {
529545
stdout: encodeJson({ name: "repo", author: "did:plc:bob", knot: "knot1.tangled.sh" }),
530546
},
@@ -540,6 +556,28 @@ describe("TangledSourceControlProvider.make", () => {
540556
.pipe(Effect.flip);
541557
assert.match(error.detail, /did:plc:bob/u);
542558
assert.match(error.detail, /did:plc:alice/u);
559+
assert.deepStrictEqual(tg.calls, [["auth", "status"]]);
560+
}).pipe(serve(tg));
561+
});
562+
563+
it.effect.each([
564+
{ authenticated: false, handle: "alice.example" },
565+
{ authenticated: true },
566+
{ authenticated: true, status: "expired", handle: "alice.example" },
567+
{ authenticated: true, status: "unknown", handle: "alice.example" },
568+
])("does not create when the signed-in owner cannot be confirmed: %j", (account) => {
569+
const tg = fakeTg({ "auth status": { stdout: encodeJson(account) } });
570+
return Effect.gen(function* () {
571+
const provider = yield* TangledSourceControlProvider.make;
572+
const error = yield* provider
573+
.createRepository({
574+
cwd: "/repo",
575+
repository: "alice.example/repo",
576+
visibility: "public",
577+
})
578+
.pipe(Effect.flip);
579+
assert.match(error.detail, /Cannot confirm/u);
580+
assert.deepStrictEqual(tg.calls, [["auth", "status"]]);
543581
}).pipe(serve(tg));
544582
});
545583

‎packages/source-control-tangled/src/server/TangledSourceControlProvider.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,25 @@ export const make = Effect.gen(function* () {
271271
"Name a Tangled repository as handle/repo.",
272272
);
273273
}
274+
const account = yield* cli
275+
.runJson({
276+
cwd: input.cwd,
277+
args: ["auth", "status"],
278+
schema: TangledAuthStatusSchema,
279+
})
280+
.pipe(Effect.mapError((cause) => fail("createRepository", input.cwd, cause)));
281+
if (
282+
!account.authenticated ||
283+
account.status === "expired" ||
284+
account.status === "unknown" ||
285+
(owner !== account.handle && owner !== account.did)
286+
) {
287+
return yield* notNamed(
288+
"createRepository",
289+
input.cwd,
290+
`Cannot confirm Tangled is signed in as ${owner} (reported account: ${account.handle ?? account.did ?? "unknown"}). Run \`tg auth login ${owner}\` and retry.`,
291+
);
292+
}
274293
// Tangled has no private repositories, so `visibility` is not something `tg` can be asked
275294
// for; a knot may still refuse the name, which surfaces as this operation's error.
276295
const created = yield* cli

0 commit comments

Comments
 (0)