【问题标题】:passport.authenticate() in routing-controllers framework路由控制器框架中的 passport.authenticate()
【发布时间】:2019-09-06 21:42:55
【问题描述】:

我是 JavaScript/TypeScript 开发的新手,我目前正在扩展一个使用单点登录登录的快速应用程序。 express 应用程序使用路由控制器框架来处理请求,并且应该使用 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(...) 方法。 谁能给我解释一下?

【问题讨论】:

    标签: typescript express passport.js


    【解决方案1】:

    我会选择的解决方案是创建您自己的中间件来处理passport.authenticate()(查看操作方法here)。然后你可以使用你自己的中间件和@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
    }
    

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

    【讨论】:

      【解决方案2】:

      当直接在路由器设置中使用 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)
      }
      

      【讨论】:

        猜你喜欢
        • 2015-04-30
        • 1970-01-01
        • 2014-01-17
        • 1970-01-01
        • 1970-01-01
        • 2014-09-08
        • 2015-03-18
        • 2012-07-25
        • 2012-07-19
        相关资源
        最近更新 更多