@@ -21,6 +21,11 @@ export interface OAuthClientProvider {
21
21
*/
22
22
get clientMetadata ( ) : OAuthClientMetadata ;
23
23
24
+ /**
25
+ * Returns a OAuth2 state parameter.
26
+ */
27
+ state ?( ) : string | Promise < string > ;
28
+
24
29
/**
25
30
* Loads information about this OAuth client, as registered already with the
26
31
* server, or returns `undefined` if the client is not registered with the
@@ -162,10 +167,13 @@ export async function auth(
162
167
}
163
168
}
164
169
170
+ const state = provider . state ? await provider . state ( ) : undefined ;
171
+
165
172
// Start new authorization flow
166
173
const { authorizationUrl, codeVerifier } = await startAuthorization ( authorizationServerUrl , {
167
174
metadata,
168
175
clientInformation,
176
+ state,
169
177
redirectUrl : provider . redirectUrl ,
170
178
scope : scope || provider . clientMetadata . scope ,
171
179
} ) ;
@@ -301,11 +309,13 @@ export async function startAuthorization(
301
309
clientInformation,
302
310
redirectUrl,
303
311
scope,
312
+ state,
304
313
} : {
305
314
metadata ?: OAuthMetadata ;
306
315
clientInformation : OAuthClientInformation ;
307
316
redirectUrl : string | URL ;
308
317
scope ?: string ;
318
+ state ?: string ;
309
319
} ,
310
320
) : Promise < { authorizationUrl : URL ; codeVerifier : string } > {
311
321
const responseType = "code" ;
@@ -347,6 +357,10 @@ export async function startAuthorization(
347
357
) ;
348
358
authorizationUrl . searchParams . set ( "redirect_uri" , String ( redirectUrl ) ) ;
349
359
360
+ if ( state ) {
361
+ authorizationUrl . searchParams . set ( "state" , state ) ;
362
+ }
363
+
350
364
if ( scope ) {
351
365
authorizationUrl . searchParams . set ( "scope" , scope ) ;
352
366
}
0 commit comments