Skip to content

Commit 53210ee

Browse files
PiquelChipsclaude
andcommitted
fix(sdk): one step-up retry for concurrent refusals, and returnTo stays on the page's origin
Every refusal within the retry window now throws, so a second concurrent request no longer leaves for another login. returnTo must be on the page's origin. The browser examples keep the session a passkey sign-in returns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 159302f commit 53210ee

3 files changed

Lines changed: 31 additions & 14 deletions

File tree

‎packages/auth/README.md‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,9 @@ const auth = browserClient(issuer, {
159159
});
160160

161161
// On every page load: finish a callback, or restore the session with prompt=none.
162-
const session = (await auth.handleCallback()) ?? (await auth.restore({ scopes: ['profile'] }));
163-
if (!session) {
164-
// On the user's click: a passkey on this page, or auth's pages with auth.login().
165-
await auth.signInWithPasskey({ scopes: ['profile'] });
166-
}
162+
let session = (await auth.handleCallback()) ?? (await auth.restore({ scopes: ['profile'] }));
163+
// On the user's click: a passkey on this page, or auth's pages with auth.login().
164+
session ??= await auth.signInWithPasskey({ scopes: ['profile'] });
167165
const notes = await session.fetch('https://notes.example/notes');
168166
```
169167

‎packages/auth/src/browser.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
*
1212
* const issuer = await Issuer.discover('https://auth.piquel.fr');
1313
* const auth = browserClient(issuer, { clientId: 'notes-web', redirectUri: `${location.origin}/callback` });
14-
* const session = (await auth.handleCallback()) ?? (await auth.restore());
15-
* if (!session) await auth.signInWithPasskey({ scopes: ['profile'] }); // or auth.login()
14+
* const session =
15+
* (await auth.handleCallback()) ??
16+
* (await auth.restore()) ??
17+
* (await auth.signInWithPasskey({ scopes: ['profile'] })); // or auth.login()
1618
* await session.fetch('https://notes.example/notes');
1719
* ```
1820
*/
@@ -183,8 +185,12 @@ export class BrowserClient {
183185
#redirect(url: string, pending: PendingLogin, request: SignInRequest): Promise<never> {
184186
const purpose: Purpose =
185187
request.prompt === 'none' ? 'restore' : request.stepUp === undefined ? 'sign-in' : 'step-up';
186-
const returnTo = request.returnTo ?? this.platform.location.href;
187-
const stored: StoredLogin = { pending, returnTo, purpose };
188+
const here = new URL(this.platform.location.href);
189+
const returnTo = new URL(request.returnTo ?? here.href, here);
190+
if (returnTo.origin !== here.origin) {
191+
throw new TypeError(`returnTo must be on this page's origin, not ${returnTo.origin}`);
192+
}
193+
const stored: StoredLogin = { pending, returnTo: returnTo.href, purpose };
188194
this.platform.storage.setItem(this.#key, JSON.stringify(stored));
189195
return this.away(url);
190196
}
@@ -289,9 +295,9 @@ export class Session {
289295
const requirement: StepUpRequirement = {};
290296
if (challenge.acr_values !== undefined) requirement.acr_values = challenge.acr_values;
291297
if (challenge.max_age !== undefined) requirement.max_age = challenge.max_age;
298+
// Every refusal within the window is the retry, concurrent ones included.
292299
const retry =
293300
this.#steppedUpAt !== undefined && this.#clock() - this.#steppedUpAt < STEP_UP_RETRY_WINDOW;
294-
this.#steppedUpAt = undefined;
295301
if (retry) {
296302
throw new AuthError(
297303
{ code: 'insufficient_user_authentication', requirement },

‎packages/auth/test/browser.test.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,23 @@ describe('a session', () => {
485485
assert.ok(stepped);
486486
assert.equal(page.location.href, PAGE);
487487
const navigations = page.navigations.length;
488-
await assert.rejects(stepped.fetch(RESOURCE), {
489-
code: 'insufficient_user_authentication'
490-
});
491-
assert.equal(page.navigations.length, navigations);
488+
// Concurrent refusals on the return are all the one retry: none leaves again.
489+
const refused = { code: 'insufficient_user_authentication' };
490+
await Promise.race([
491+
Promise.all([
492+
assert.rejects(stepped.fetch(RESOURCE), refused),
493+
assert.rejects(stepped.fetch(RESOURCE), refused)
494+
]),
495+
page.left(navigations).then(() => assert.fail('a refusal left the page again'))
496+
]);
497+
});
498+
499+
test('refuses to come back to another origin', async () => {
500+
const { page, client } = await setUp();
501+
await Promise.race([
502+
assert.rejects(client.login({ returnTo: 'https://elsewhere.example/' }), TypeError),
503+
page.left(0).then(() => assert.fail('the login left for another origin'))
504+
]);
492505
});
493506

494507
test('refreshes an expiring access token once for concurrent requests', async () => {

0 commit comments

Comments
 (0)