33 * then refresh, revocation, `UserInfo` and logout.
44 */
55import * as base64 from './base64.ts' ;
6- import { asciiLowercase } from './challenge.ts' ;
6+ import { asciiLowercase , stepUpRequirement } from './challenge.ts' ;
77import { CLIENT_ID , scope , userInfo } from './claims.ts' ;
88import { AuthError } from './error.ts' ;
99import type { Reply } from './http.ts' ;
@@ -121,15 +121,24 @@ function formEncoded(value: string): string {
121121export 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
0 commit comments