@@ -31,14 +31,22 @@ export type ClientRegistrationHandlerOptions = {
31
31
* Registration endpoints are particularly sensitive to abuse and should be rate limited.
32
32
*/
33
33
rateLimit ?: Partial < RateLimitOptions > | false ;
34
+
35
+ /**
36
+ * Whether to generate a client ID before calling the client registration endpoint.
37
+ *
38
+ * If not set, defaults to true.
39
+ */
40
+ clientIdGeneration ?: boolean ;
34
41
} ;
35
42
36
43
const DEFAULT_CLIENT_SECRET_EXPIRY_SECONDS = 30 * 24 * 60 * 60 ; // 30 days
37
44
38
45
export function clientRegistrationHandler ( {
39
46
clientsStore,
40
47
clientSecretExpirySeconds = DEFAULT_CLIENT_SECRET_EXPIRY_SECONDS ,
41
- rateLimit : rateLimitConfig
48
+ rateLimit : rateLimitConfig ,
49
+ clientIdGeneration = true ,
42
50
} : ClientRegistrationHandlerOptions ) : RequestHandler {
43
51
if ( ! clientsStore . registerClient ) {
44
52
throw new Error ( "Client registration store does not support registering clients" ) ;
@@ -78,7 +86,6 @@ export function clientRegistrationHandler({
78
86
const isPublicClient = clientMetadata . token_endpoint_auth_method === 'none'
79
87
80
88
// Generate client credentials
81
- const clientId = crypto . randomUUID ( ) ;
82
89
const clientSecret = isPublicClient
83
90
? undefined
84
91
: crypto . randomBytes ( 32 ) . toString ( 'hex' ) ;
@@ -89,14 +96,17 @@ export function clientRegistrationHandler({
89
96
const secretExpiryTime = clientsDoExpire ? clientIdIssuedAt + clientSecretExpirySeconds : 0
90
97
const clientSecretExpiresAt = isPublicClient ? undefined : secretExpiryTime
91
98
92
- let clientInfo : OAuthClientInformationFull = {
99
+ let clientInfo : Omit < OAuthClientInformationFull , "client_id" > & { client_id ?: string } = {
93
100
...clientMetadata ,
94
- client_id : clientId ,
95
101
client_secret : clientSecret ,
96
102
client_id_issued_at : clientIdIssuedAt ,
97
103
client_secret_expires_at : clientSecretExpiresAt ,
98
104
} ;
99
105
106
+ if ( clientIdGeneration ) {
107
+ clientInfo . client_id = crypto . randomUUID ( ) ;
108
+ }
109
+
100
110
clientInfo = await clientsStore . registerClient ! ( clientInfo ) ;
101
111
res . status ( 201 ) . json ( clientInfo ) ;
102
112
} catch ( error ) {
0 commit comments