Skip to content

Commit af0f9d6

Browse files
committed
feat(passport): easier way to use OAuth 2.0 Rich Authorization Requests without overloads
1 parent 7eb3076 commit af0f9d6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/passport.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ export interface AuthenticateOptions extends passport.AuthenticateOptions {
3131
* callback phase.
3232
*/
3333
resource?: string | string[]
34+
35+
/**
36+
* OAuth 2.0 Rich Authorization Requests to use for the authorization request.
37+
* It is ignored for token endpoint requests.
38+
*/
39+
authorizationDetails?:
40+
| client.AuthorizationDetails
41+
| client.AuthorizationDetails[]
3442
}
3543

3644
/**
@@ -72,6 +80,14 @@ interface StrategyOptionsBase {
7280
* request parameter unless specified elsewhere.
7381
*/
7482
scope?: string
83+
/**
84+
* OAuth 2.0 Rich Authorization Request(s). This will be used as the
85+
* `authorization_details` authorization request parameter unless specified
86+
* through other means.
87+
*/
88+
authorizationDetails?:
89+
| client.AuthorizationDetails
90+
| client.AuthorizationDetails[]
7591
/**
7692
* OAuth 2.0 Resource Indicator(s). This will be used as the `resource`
7793
* authorization request parameter unless specified through other means.
@@ -116,6 +132,19 @@ function setResource(params: URLSearchParams, resource: string | string[]) {
116132
}
117133
}
118134

135+
function setAuthorizationDetails(
136+
params: URLSearchParams,
137+
authorizationDetails:
138+
| client.AuthorizationDetails
139+
| client.AuthorizationDetails[],
140+
) {
141+
if (Array.isArray(authorizationDetails)) {
142+
params.set('authorization_details', JSON.stringify(authorizationDetails))
143+
} else {
144+
params.set('authorization_details', JSON.stringify([authorizationDetails]))
145+
}
146+
}
147+
119148
export class Strategy implements passport.Strategy {
120149
/**
121150
* Name of the strategy
@@ -161,6 +190,10 @@ export class Strategy implements passport.Strategy {
161190
* @internal
162191
*/
163192
_resource: StrategyOptionsBase['resource']
193+
/**
194+
* @internal
195+
*/
196+
_authorizationDetails: StrategyOptionsBase['authorizationDetails']
164197

165198
constructor(options: StrategyOptions, verify: VerifyFunction)
166199
constructor(
@@ -192,6 +225,7 @@ export class Strategy implements passport.Strategy {
192225
this._callbackURL = options.callbackURL
193226
this._passReqToCallback = options.passReqToCallback
194227
this._resource = options.resource
228+
this._authorizationDetails = options.authorizationDetails
195229
}
196230

197231
/**
@@ -220,6 +254,10 @@ export class Strategy implements passport.Strategy {
220254
setResource(params, options.resource)
221255
}
222256

257+
if (options?.authorizationDetails) {
258+
setAuthorizationDetails(params, options.authorizationDetails)
259+
}
260+
223261
return params
224262
}
225263

@@ -287,6 +325,16 @@ export class Strategy implements passport.Strategy {
287325
setResource(redirectTo.searchParams, this._resource)
288326
}
289327

328+
if (
329+
this._authorizationDetails &&
330+
!redirectTo.searchParams.has('authorization_details')
331+
) {
332+
setAuthorizationDetails(
333+
redirectTo.searchParams,
334+
this._authorizationDetails,
335+
)
336+
}
337+
290338
const DPoP = await this._DPoP?.(req)
291339

292340
if (DPoP && !redirectTo.searchParams.has('dpop_jkt')) {

0 commit comments

Comments
 (0)