Skip to content

Commit 1648ea8

Browse files
committed
fix(desktop): accept pairing links when refreshing credentials
1 parent e4e1491 commit 1648ea8

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎apps/desktop/src/backend/DesktopAttachedBackend.test.ts‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const fixture = Effect.gen(function* () {
5555
const entered = yield* Deferred.make<void>();
5656
const release = yield* Deferred.make<void>();
5757
const authorizations = yield* Ref.make<Array<{ port: string; bearer: string | undefined }>>([]);
58+
const exchanges = yield* Ref.make<Array<{ port: string; credential: string | null }>>([]);
5859
const identity = yield* Ref.make("attached-environment");
5960
const encrypted = yield* Ref.make<string[]>([]);
6061
const requests = yield* Ref.make<string[]>([]);
@@ -91,6 +92,15 @@ const fixture = Effect.gen(function* () {
9192
{ port, bearer: request.headers.authorization },
9293
]);
9394
yield* Ref.update(requests, (items) => [...items, path]);
95+
if (path === "/oauth/token") {
96+
if (request.body._tag !== "Uint8Array")
97+
return yield* Effect.die("Expected token exchange form");
98+
const form = new URLSearchParams(new TextDecoder().decode(request.body.body));
99+
yield* Ref.update(exchanges, (items) => [
100+
...items,
101+
{ port, credential: form.get("subject_token") },
102+
]);
103+
}
94104
const body =
95105
path === "/oauth/token"
96106
? {
@@ -162,6 +172,7 @@ const fixture = Effect.gen(function* () {
162172
entered,
163173
release,
164174
authorizations,
175+
exchanges,
165176
};
166177
});
167178

@@ -180,7 +191,28 @@ describe("DesktopAttachedBackend ownership and credentials", () => {
180191
assert.notInclude(JSON.stringify(yield* settings.get), "fake-bearer");
181192
assert.deepEqual(yield* Ref.get(f.encrypted), ["fake-bearer-4773"]);
182193
assert.equal((yield* attached.probe).environmentId, "attached-environment");
183-
yield* attached.refreshCredential("renewed-owner");
194+
for (const { credential, expected } of [
195+
{ credential: "renewed-owner", expected: "renewed-owner" },
196+
{
197+
credential: " http://127.0.0.1:4773/pair#token=fragment-owner ",
198+
expected: "fragment-owner",
199+
},
200+
{
201+
credential: "http://127.0.0.1:4773/pair?token=query-owner",
202+
expected: "query-owner",
203+
},
204+
]) {
205+
const before = yield* attached.getState;
206+
yield* attached.refreshCredential(credential);
207+
assert.deepEqual((yield* Ref.get(f.exchanges)).at(-1), {
208+
port: "4773",
209+
credential: expected,
210+
});
211+
const after = yield* attached.getState;
212+
if (before.mode !== "attached" || after.mode !== "attached")
213+
return yield* Effect.die("Expected attachment");
214+
assert.notEqual(after.attachmentRevision, before.attachmentRevision);
215+
}
184216
assert.equal((yield* attached.probe).environmentId, "attached-environment");
185217
yield* settings.setLocalEnvironmentEnabled(false);
186218
assert.equal((yield* attached.getState).mode, "attached");
@@ -190,13 +222,23 @@ describe("DesktopAttachedBackend ownership and credentials", () => {
190222
}),
191223
);
192224
it.effect(
193-
"rejects a replacement server before sending the saved bearer or refreshing credentials",
225+
"rejects a different pairing target or replacement server before sending credentials",
194226
() =>
195227
Effect.gen(function* () {
196228
const f = yield* fixture;
197229
yield* Effect.gen(function* () {
198230
const attached = yield* Attached.DesktopAttachedBackend;
199231
yield* attached.attach("http://127.0.0.1:4773/pair#token=fake-owner");
232+
const original = yield* attached.getState;
233+
yield* Ref.set(f.requests, []);
234+
assert.equal(
235+
(yield* Effect.flip(
236+
attached.refreshCredential("http://127.0.0.1:4774/pair#token=foreign-owner"),
237+
))._tag,
238+
"DesktopAttachPairingUrlError",
239+
);
240+
assert.deepEqual(yield* Ref.get(f.requests), []);
241+
assert.deepEqual(yield* attached.getState, original);
200242
yield* Ref.set(f.identity, "replacement-environment");
201243
yield* Ref.set(f.requests, []);
202244
assert.equal(

‎apps/desktop/src/backend/DesktopAttachedBackend.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,20 @@ const make = Effect.gen(function* () {
377377
const current = yield* readPreference;
378378
if (current.mode !== "attached")
379379
return yield* new DesktopAttachedCredentialUnavailableError();
380+
const target = credential.includes("://")
381+
? yield* resolveDesktopAttachPairingTarget(credential)
382+
: null;
383+
if (
384+
target !== null &&
385+
(target.httpBaseUrl !== current.httpBaseUrl || target.wsBaseUrl !== current.wsBaseUrl)
386+
)
387+
return yield* new DesktopAttachPairingUrlError({
388+
reason: "Use a pairing link for the attached server address.",
389+
});
380390
const before = yield* descriptor(current.httpBaseUrl);
381391
if (before.environmentId !== current.environmentId)
382392
return yield* new DesktopAttachedIdentityMismatchError();
383-
const session = yield* exchange(current.httpBaseUrl, credential);
393+
const session = yield* exchange(current.httpBaseUrl, target?.credential ?? credential);
384394
const missingScopes = missingAdministrativeScopes(session.scope);
385395
if (missingScopes.length)
386396
return yield* new DesktopAttachAdministrativeScopeError({ missingScopes });

0 commit comments

Comments
 (0)