路由控制器框架中的Passport.Authenticate()



我是JavaScript/TypeScript开发的新手,目前我正在使用单签名登录的Express应用程序。Express App使用路由控制器框架来处理请求,并应使用Passport-SAML进行身份验证。我已经设法使身份验证使用标准快速路线:

export class SsoRoutes {
    public router: Router;
    constructor() {
        this.router = Router();
    }

    this.router.get('/login-sso', passport.authenticate('saml'));
    this.router.post('/login-sso/consume', passport.authenticate('saml', {
        failureRedirect: '/',
        failureFlash: true,
        session: false
    }), function (req, res) {
        // handle callback
    });
}

,但我无法弄清楚如何在路由控制器框架中使用passport.authenticate(...)方法。有人可以向我解释吗?

我选择的解决方案是创建自己的中间件,该中间件将处理passport.authenticate()(请在此处查看如何进行)。然后,您可以将自己的中间件与@UseBefore()装饰器一起使用。

@Get("/login-sso")
@UseBefore(yourFirstMiddleware)
loginSso() {
    // ... something you want to do after passport.authenticate()
}

和第二个端点的相似之处:

@Post("/login-sso/consume")
@UseBefore(yourSecondMiddleware)
loginSso() {
    // ... some other action you want to do after
}

有关其他解决方案检查您正在使用的框架的文档。

直接在路由器设置中使用PassportJS方法时,请求/响应/下一个功能将从闭合中"神奇地"消耗。因此,如果您提取并将其应用于其他类,则需要明确提供它们。

在路由器类中

...
this.router.get('/login', (req, res, next) => this.authenticate(req, res, next)); // Called by user
this.router.get('/callback', (req, res, next) => this.callback(req, res, next)); // Called by OAuth2 provider
...
/**
 * Authenticate the user
 * @param req
 * @param res
 * @param next
 */
private authenticate(req: Request, res: Response, next: NextFunction){
    this.logger.debug('Performing authentication');
    this.customController.authenticate(req, res, next);
}
/**
 * Callback after OAuth2 provider has authenticated the user
 * @param req
 * @param res
 * @param next
 */
private callback(req: Request, res: Response, next: NextFunction){
    this.logger.debug('Callback from OAuth provider');
    this.customController.callback(req, res, next);
}

在自定义控制器

/**
 * Executes the authentication using passportJS
 */
public executeAuthenticate(req: Request, res: Response, next: NextFunction): void {
    this.logger.debug('Authenticate using passport');
    passport.authenticate('<strategy>', { scope: ['email', 'profile'] })(req, res, next);  // <== Here! See that the function is called using the parameters (req,res,next)
}
/**
 * Callback after login completion
 * @param req 
 * @param res 
 * @param next 
 */
public callback(req: Request, res: Response, next: NextFunction): void {
    this.logger.debug('Callback from oAuth provider');
    // Ask passportJS to verify that the user is indeed logged in
    passport.authenticate('<strategy>', (err, user, info)=> {
        this.logger.debug('Authentication check done');
        if (err) {
            this.logger.debug('Authentication error');
            return next(err);
        }
        if (!user) { 
            this.logger.debug('Could not extract user');
            return next('Could not find user object');
        }
        this.logger.debug('Authentication succeeded');
        return res.json(user);
    })(req, res, next); // <== Here! See that the function is called using the parameters (req,res,next)
}

最新更新