From cc92a36a58b68b3d60914e1d6d7cba072e8151d0 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Thu, 28 Nov 2024 11:21:56 +0100 Subject: [PATCH] fix: handle scope, prompt, and passReqToCallback from generic passport types Fixes #735 --- src/passport.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/passport.ts b/src/passport.ts index 788bd318..e352accf 100644 --- a/src/passport.ts +++ b/src/passport.ts @@ -177,7 +177,21 @@ export class Strategy implements passport.Strategy { // @ts-ignore req: express.Request, options: TOptions, ): URLSearchParams | Record | undefined { - return {} + let params = new URLSearchParams() + + if (options?.scope) { + if (Array.isArray(options?.scope) && options.scope.length) { + params.set('scope', options.scope.join(' ')) + } else if (typeof options?.scope === 'string' && options.scope.length) { + params.set('scope', options.scope) + } + } + + if (options?.prompt) { + params.set('prompt', options.prompt) + } + + return params } // prettier-ignore @@ -352,7 +366,7 @@ export class Strategy implements passport.Strategy { return this.success(user) } - if (this._passReqToCallback) { + if (options.passReqToCallback ?? this._passReqToCallback) { return (this._verify as VerifyFunctionWithRequest)( req, tokens, @@ -382,11 +396,17 @@ export class Strategy implements passport.Strategy { * the response type used by the client is `code` * - Its value stripped of `searchParams` and `hash` is used as the * `redirect_uri` authorization code grant token endpoint parameter + * + * This function may need to be overridden by users if its return value does + * not match the actual URL the authorization server redirected the user to. */ currentUrl(req: express.Request): URL { return new URL(`${req.protocol}://${req.host}${req.originalUrl ?? req.url}`) } + /** + * Authenticate request. + */ authenticate< TOptions extends passport.AuthenticateOptions = passport.AuthenticateOptions,