@@ -23,6 +23,16 @@ export type VerifyFunctionWithRequest = (
23
23
verified : passport . AuthenticateCallback ,
24
24
) => void
25
25
26
+ export interface AuthenticateOptions extends passport . AuthenticateOptions {
27
+ /**
28
+ * OAuth 2.0 Resource Indicator(s) to use for the request either for the
29
+ * authorization request or token endpoint request, depending on whether it's
30
+ * part of {@link Strategy.authenticate} options during the initial redirect or
31
+ * callback phase.
32
+ */
33
+ resource ?: string | string [ ]
34
+ }
35
+
26
36
/**
27
37
* Retrieve an openid-client DPoPHandle for a given request.
28
38
*/
@@ -62,6 +72,11 @@ interface StrategyOptionsBase {
62
72
* request parameter unless specified elsewhere.
63
73
*/
64
74
scope ?: string
75
+ /**
76
+ * OAuth 2.0 Resource Indicator(s). This will be used as the `resource`
77
+ * authorization request parameter unless specified through other means.
78
+ */
79
+ resource ?: string | string [ ]
65
80
/**
66
81
* Whether the strategy will use PAR. Default is `false`.
67
82
*/
@@ -91,6 +106,16 @@ export interface StrategyOptionsWithRequest extends StrategyOptionsBase {
91
106
passReqToCallback : true
92
107
}
93
108
109
+ function setResource ( params : URLSearchParams , resource : string | string [ ] ) {
110
+ if ( Array . isArray ( resource ) ) {
111
+ for ( const value of resource ) {
112
+ params . append ( 'resource' , value )
113
+ }
114
+ } else {
115
+ params . set ( 'resource' , resource )
116
+ }
117
+ }
118
+
94
119
export class Strategy implements passport . Strategy {
95
120
/**
96
121
* Name of the strategy
@@ -132,6 +157,10 @@ export class Strategy implements passport.Strategy {
132
157
* @internal
133
158
*/
134
159
_scope ?: string
160
+ /**
161
+ * @internal
162
+ */
163
+ _resource : StrategyOptionsBase [ 'resource' ]
135
164
136
165
constructor ( options : StrategyOptions , verify : VerifyFunction )
137
166
constructor (
@@ -162,18 +191,16 @@ export class Strategy implements passport.Strategy {
162
191
this . _verify = verify
163
192
this . _callbackURL = options . callbackURL
164
193
this . _passReqToCallback = options . passReqToCallback
194
+ this . _resource = options . resource
165
195
}
166
196
167
- // prettier-ignore
168
197
/**
169
198
* Return extra parameters to be included an authorization request.
170
199
*/
171
- authorizationRequestParams <
172
- TOptions extends
173
- passport . AuthenticateOptions = passport . AuthenticateOptions ,
174
- > (
200
+ authorizationRequestParams < TOptions extends AuthenticateOptions > (
175
201
// @ts -ignore
176
- req : express . Request , options : TOptions ,
202
+ req : express . Request ,
203
+ options : TOptions ,
177
204
) : URLSearchParams | Record < string , string > | undefined {
178
205
let params = new URLSearchParams ( )
179
206
@@ -189,31 +216,35 @@ export class Strategy implements passport.Strategy {
189
216
params . set ( 'prompt' , options . prompt )
190
217
}
191
218
219
+ if ( options ?. resource ) {
220
+ setResource ( params , options . resource )
221
+ }
222
+
192
223
return params
193
224
}
194
225
195
- // prettier-ignore
196
226
/**
197
227
* Return extra parameters to be included in the authorization code grant
198
228
* token endpoint request.
199
229
*/
200
- authorizationCodeGrantParameters <
201
- TOptions extends
202
- passport . AuthenticateOptions = passport . AuthenticateOptions ,
203
- > (
230
+ authorizationCodeGrantParameters < TOptions extends AuthenticateOptions > (
204
231
// @ts -ignore
205
- req : express . Request , options : TOptions ,
232
+ req : express . Request ,
233
+ options : TOptions ,
206
234
) : URLSearchParams | Record < string , string > | undefined {
207
- return { }
235
+ let params = new URLSearchParams ( )
236
+
237
+ if ( options ?. resource ) {
238
+ setResource ( params , options . resource )
239
+ }
240
+
241
+ return params
208
242
}
209
243
210
244
/**
211
245
* @internal
212
246
*/
213
- async authorizationRequest <
214
- TOptions extends
215
- passport . AuthenticateOptions = passport . AuthenticateOptions ,
216
- > (
247
+ async authorizationRequest < TOptions extends AuthenticateOptions > (
217
248
this : passport . StrategyCreated <
218
249
Strategy ,
219
250
Strategy & passport . StrategyCreatedStatic
@@ -252,6 +283,10 @@ export class Strategy implements passport.Strategy {
252
283
redirectTo . searchParams . set ( 'scope' , this . _scope )
253
284
}
254
285
286
+ if ( this . _resource && ! redirectTo . searchParams . has ( 'resource' ) ) {
287
+ setResource ( redirectTo . searchParams , this . _resource )
288
+ }
289
+
255
290
const DPoP = await this . _DPoP ?.( req )
256
291
257
292
if ( DPoP && ! redirectTo . searchParams . has ( 'dpop_jkt' ) ) {
@@ -312,10 +347,7 @@ export class Strategy implements passport.Strategy {
312
347
/**
313
348
* @internal
314
349
*/
315
- async authorizationCodeGrant <
316
- TOptions extends
317
- passport . AuthenticateOptions = passport . AuthenticateOptions ,
318
- > (
350
+ async authorizationCodeGrant < TOptions extends AuthenticateOptions > (
319
351
this : passport . StrategyCreated <
320
352
Strategy ,
321
353
Strategy & passport . StrategyCreatedStatic
@@ -412,10 +444,7 @@ export class Strategy implements passport.Strategy {
412
444
/**
413
445
* Authenticate request.
414
446
*/
415
- authenticate <
416
- TOptions extends
417
- passport . AuthenticateOptions = passport . AuthenticateOptions ,
418
- > (
447
+ authenticate < TOptions extends AuthenticateOptions > (
419
448
this : passport . StrategyCreated <
420
449
Strategy ,
421
450
Strategy & passport . StrategyCreatedStatic
0 commit comments