首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >路由控制器框架中的passport.authenticate()

路由控制器框架中的passport.authenticate()
EN

Stack Overflow用户
提问于 2019-03-08 23:18:36
回答 2查看 862关注 0票数 1

我是JavaScript/TypeScript开发的新手,目前正在使用单点登录扩展express应用程序。express应用程序使用routing-controllers框架来处理请求,并且应该使用passport-saml进行身份验证。我已经设法让身份验证与标准的快速路由一起工作:

代码语言:javascript
复制
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(...)方法。有人能给我解释一下吗?

EN

回答 2

Stack Overflow用户

发布于 2019-04-02 03:23:57

我选择的解决方案是创建您自己的中间件来处理passport.authenticate() (查看here如何做到这一点)。然后,您可以将自己的中间件与@UseBefore()装饰器一起使用。

代码语言:javascript
复制
@Get("/login-sso")
@UseBefore(yourFirstMiddleware)
loginSso() {
    // ... something you want to do after passport.authenticate()
}

对于第二个端点也是如此:

代码语言:javascript
复制
@Post("/login-sso/consume")
@UseBefore(yourSecondMiddleware)
loginSso() {
    // ... some other action you want to do after
}

对于其他解决方案,请检查您正在使用的框架的documentation

票数 1
EN

Stack Overflow用户

发布于 2019-09-06 21:42:56

当直接在路由器设置中使用passportJS方法时,request/ response/ next函数将从闭包中“神奇地”使用。因此,如果您在另一个类中提取并应用它们,则需要显式地提供它们。

路由器类中的

代码语言:javascript
复制
...
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);
}

自定义控制器中的

代码语言:javascript
复制
/**
 * 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)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55066157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档