Passport-Azure-Ad似乎异步运行(仍然)

时间:2019-06-05 00:59:36

标签: node.js express azure-active-directory passport.js adal.js

在nodejs表达中间件中运行Passport策略的传统方式是:

server.get('/api/tasks', passport.authenticate('oauth-bearer', { session: false }));

我正在使用https://tsed.io(“打字稿表达装饰器”)并引用此通行证策略,操作如下:

class MyCtrl {
    @Get('/api/tasks')
    @UseBefore(passport.authenticate('oauth-bearer', { session: false }))
    get() {
         ...
    } 
}

这很好用。

您还可以将其作为独立的中间件运行。像这样:

@Post('/search')
@CustomAuth({role: 'admin', scope: ['admin']})
async search(@Req() request: Req, @BodyParams() query: TranslationApiQuery): Promise<TranslationApiModel[]> {
     ...

export function CustomAuth(options = {}): Function {
    return applyDecorators(
        UseAuth(CustomAuthMiddleware), 
         ...

@Middleware()
export class CustomAuthMiddleware implements IMiddleware {
    public use(
        @Req() request: Express.Request, @Res() response: Express.Response, @EndpointInfo() endpoint: EndpointInfo,
        @Next() next: Express.NextFunction) {
              // retrieve options given to the decorator
              const options = endpoint.get(CustomAuthMiddleware) || {};

              Passport.authenticate('oauth-bearer', {session: false}})(request, response, next);
              ...

我试图使用so-as来访问端点上定义的roles and scopes。但是passport.authenticate的这种用法不起作用。 https://github.com/AzureAD/passport-azure-ad/blob/dev/lib/bearerstrategy.js使用async.waterfall(),这将强制执行异步行为。但是没有返回承诺,所以不能等待。

我不明白为什么passport.authenticate的传统用法有效。有人可以解释为什么吗?有没有解决此问题的简便方法(无需重写bearerstrategy.js)? bearerstrategy.js是否可以轻松地重写以兼顾这两种用途?


我最初在Passport-Azure-Ad seems to run asynchronously有这个问题,但是我需要简化这个问题。另外,我写的答案被证明不是完全正确,但希望通过增加问题来保持答案。总体而言,这变得一团糟,因此似乎更容易关闭旧的并重新启动。

0 个答案:

没有答案