Skip to content

Commit 8cbb6c0

Browse files
PiquelChipsclaude
andcommitted
fix(sdk): review fixes to the TypeScript login client
- `Client` has a private constructor, and its secret lives in a private field, out of `config` and of anything that prints the client. - One `stepUpRequirement` reads a requirement out of a challenge or request. - The callback docs name the parameters a repeat fails. - The e2e job runs on SDK changes from this layer up. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dc6c848 commit 8cbb6c0

5 files changed

Lines changed: 42 additions & 21 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ jobs:
7777
deps:
7878
- '{Cargo.toml,**/Cargo.toml,Cargo.lock,deny.toml,package.json,**/package.json,pnpm-lock.yaml,pnpm-workspace.yaml,.github/workflows/ci.yml}'
7979
app:
80-
- '{Cargo.toml,Cargo.lock,rust-toolchain.toml,justfile,just/**,package.json,pnpm-lock.yaml,playwright.config.ts,apps/**,crates/**,tests/browser/**,infra/**,.dockerignore,.github/workflows/ci.yml}'
80+
- '{Cargo.toml,Cargo.lock,rust-toolchain.toml,justfile,just/**,package.json,pnpm-lock.yaml,playwright.config.ts,apps/**,crates/**,packages/auth/**,tests/browser/**,infra/**,.dockerignore,.github/workflows/ci.yml}'
8181
- '!**/*.md'
8282
- '!infra/piqueld.toml'
8383

‎packages/auth/src/challenge.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* `WWW-Authenticate: Bearer` challenges (RFC 6750 §3, RFC 9470 §3, RFC 9728 §5.1),
33
* written by resource servers and read back by clients, exactly as the Rust SDK does.
44
*/
5-
import type { BearerChallenge } from './protocol.ts';
5+
import type { BearerChallenge, StepUpRequirement } from './protocol.ts';
66
import { includes } from './json.ts';
77
import { Scopes } from './scope.ts';
88
import { ACR } from './vocabulary.ts';
@@ -64,6 +64,17 @@ export function formatChallenge(challenge: BearerChallenge): FormattedChallenge
6464
return { status, header };
6565
}
6666

67+
/**
68+
* The step-up requirement `source` names, such as a parsed challenge's
69+
* `acr_values` and `max_age`, without its other fields.
70+
*/
71+
export function stepUpRequirement(source: StepUpRequirement): StepUpRequirement {
72+
const requirement: StepUpRequirement = {};
73+
if (source.acr_values !== undefined) requirement.acr_values = source.acr_values;
74+
if (source.max_age !== undefined) requirement.max_age = source.max_age;
75+
return requirement;
76+
}
77+
6778
/**
6879
* Finds the Bearer challenge in a `WWW-Authenticate` value, following the RFC 9110
6980
* §11 grammar: parameters in any order with token or quoted values, and other

‎packages/auth/src/client.ts‎

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* then refresh, revocation, `UserInfo` and logout.
44
*/
55
import * as base64 from './base64.ts';
6-
import { asciiLowercase } from './challenge.ts';
6+
import { asciiLowercase, stepUpRequirement } from './challenge.ts';
77
import { CLIENT_ID, scope, userInfo } from './claims.ts';
88
import { AuthError } from './error.ts';
99
import type { Reply } from './http.ts';
@@ -121,15 +121,24 @@ function formEncoded(value: string): string {
121121
export class Client {
122122
/** The issuer it signs in with. */
123123
readonly issuer: Issuer;
124-
/** Its registration. */
125-
readonly config: ClientConfig;
124+
/** Its registration, without the secret, which stays out of logs. */
125+
readonly config: Readonly<Omit<ClientConfig, 'clientSecret'>>;
126+
readonly #secret: string | undefined;
126127

127-
/** @internal Use {@link Issuer.client}. */
128-
constructor(issuer: Issuer, config: ClientConfig) {
129-
if (!CLIENT_ID.test(config.clientId)) throw new TypeError('clientId must be a client ID');
130-
if (!URL.canParse(config.redirectUri)) throw new TypeError('redirectUri must be a URL');
128+
private constructor(issuer: Issuer, config: ClientConfig) {
129+
const { clientSecret, ...registration } = config;
130+
if (!CLIENT_ID.test(registration.clientId)) throw new TypeError('clientId must be a client ID');
131+
if (!URL.canParse(registration.redirectUri)) {
132+
throw new TypeError('redirectUri must be a URL');
133+
}
131134
this.issuer = issuer;
132-
this.config = { ...config, redirectUri: new URL(config.redirectUri).href };
135+
this.config = { ...registration, redirectUri: new URL(registration.redirectUri).href };
136+
this.#secret = clientSecret;
137+
}
138+
139+
/** @internal Use {@link Issuer.client}. */
140+
static from(issuer: Issuer, config: ClientConfig): Client {
141+
return new Client(issuer, config);
133142
}
134143

135144
/**
@@ -149,12 +158,9 @@ export class Client {
149158
state: random(),
150159
nonce: random(),
151160
verifier: random(),
152-
stepUp: {}
161+
// Only the requirement, should a whole parsed challenge be passed.
162+
stepUp: stepUpRequirement(request.stepUp ?? {})
153163
};
154-
// Only the requirement, should a whole parsed challenge be passed.
155-
const { acr_values, max_age } = request.stepUp ?? {};
156-
if (acr_values !== undefined) pending.stepUp.acr_values = acr_values;
157-
if (max_age !== undefined) pending.stepUp.max_age = max_age;
158164
const digest = await crypto.subtle.digest(
159165
'SHA-256',
160166
new TextEncoder().encode(pending.verifier)
@@ -190,8 +196,8 @@ export class Client {
190196
* the code goes anywhere, in order: `state` (`state_mismatch`); RFC 9207 `iss`,
191197
* which must be the issuer's (`issuer_mismatch`), and present when the issuer
192198
* advertises it (`missing_issuer`), error responses included; an `error` becomes an
193-
* `oauth` error; then `code` (`missing_code`). A parameter sent twice fails its
194-
* check (RFC 6749 §3.1). The code is then exchanged, and the ID token validated
199+
* `oauth` error; then `code` (`missing_code`). A `state`, `iss` or `code` sent
200+
* twice fails its check (RFC 6749 §3.1); an error response only ever fails. The code is then exchanged, and the ID token validated
195201
* against the pending login's `nonce` and `max_age`. It must meet the login's
196202
* step-up requirement (`insufficient_user_authentication`), so a step-up login
197203
* that falls short fails instead of being asked for again.
@@ -310,10 +316,11 @@ export class Client {
310316
* `client_id` in the form for a public one.
311317
*/
312318
#post(url: string, form: Record<string, string>): Promise<Reply> {
313-
const { clientId, clientSecret } = this.config;
314-
if (clientSecret === undefined)
319+
const { clientId } = this.config;
320+
if (this.#secret === undefined) {
315321
return this.issuer.http.post(url, { ...form, client_id: clientId });
316-
const credentials = btoa(`${formEncoded(clientId)}:${formEncoded(clientSecret)}`);
322+
}
323+
const credentials = btoa(`${formEncoded(clientId)}:${formEncoded(this.#secret)}`);
317324
return this.issuer.http.post(url, form, { authorization: `Basic ${credentials}` });
318325
}
319326

‎packages/auth/src/issuer.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class Issuer {
157157

158158
/** Signs users in as the client `config` describes. */
159159
client(config: ClientConfig): Client {
160-
return new Client(this, config);
160+
return Client.from(this, config);
161161
}
162162

163163
/** Validates access tokens for the resource `config` describes. */

‎packages/auth/test/client.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ test('a confidential client authenticates with HTTP Basic, a public one sends it
9191
{ ...PUBLIC, clientSecret: 'a secret/+' }
9292
);
9393
await outcome(() => confidential.client.refresh('prt_v1_old'));
94+
// The secret stays out of the client's printable state.
95+
assert.ok(!JSON.stringify(confidential.client).includes('a secret'));
96+
assert.ok(!('clientSecret' in confidential.client.config));
9497
const basic = sent(confidential.fetch, TOKEN_URL);
9598
// RFC 6749 §2.3.1 form-encodes both before base64.
9699
assert.equal(basic.headers.get('authorization'), `Basic ${btoa('notes-web:a+secret%2F%2B')}`);

0 commit comments

Comments
 (0)