Passport-Azure-Ad, кажется, работает асинхронно - PullRequest
1 голос
/ 31 мая 2019

Я использую TSED - TypeScript Express Decorators (https://tsed.io), и он заменяет экспресс-код, например:

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

С аннотированным классом промежуточного программного обеспечения - https://tsed.io/docs/middlewares.html

Так что теперь вызов passport.authenticate() в методе use(), например:

@OverrideMiddleware(AuthenticatedMiddleware)
export class UserAuthMiddleware implements IMiddleware {
    constructor(@Inject() private authService: AuthService) {
    }

    public use(
        @EndpointInfo() endpoint: EndpointMetadata,
        @Request() request: express.Request,
        @Response() response: express.Response,
        @Next() next: express.NextFunction
    ) {
        const options = endpoint.get(AuthenticatedMiddleware) || {};
        this.authService.authenticate(request, response, next);  // <-- HERE
        if (!request.isAuthenticated()) {
            throw new Forbidden('Forbidden');
        }
        next();
    }
}

И тогда мой AuthService.authenticate() будет

authenticate(request: express.Request, response: express.Response, next: express.NextFunction) {
    console.log(`before passport authenticate time: ${Date.now()}`);
    Passport.authenticate('oauth-bearer', {session: false})(request, response, next);
    console.log(`after passport authenticate time : ${Date.now()}`);

}

Моя конфигурация паспорта выполняется в том же классе AuthService:

@Service()
export class AuthService implements BeforeRoutesInit, AfterRoutesInit {
    users = [];
    owner = '';

    constructor(private serverSettings: ServerSettingsService,
                @Inject(ExpressApplication) private  expressApplication: ExpressApplication) {
    }

    $beforeRoutesInit() {
        this.expressApplication.use(Passport.initialize());
    }

    $afterRoutesInit() {
        this.setup();
    }

    setup() {
        Passport.use('oauth-bearer', new BearerStrategy(jwtOptions, (token: ITokenPayload, done: VerifyCallback) => {
            // TODO - reconsider the use of an array for Users
            const findById = (id, fn) => {
                for (let i = 0, len = this.users.length; i < len; i++) {
                    const user = this.users[i];
                    if (user.oid === id) {
                        logger.info('Found user: ', user);
                        return fn(null, user);
                    }
                }
                return fn(null, null);
            };

            console.log(token, 'was the token retrieved');

            findById(token.oid, (err, user) => {
                if (err) {
                    return done(err);
                }
                if (!user) {
                    // 'Auto-registration'
                    logger.info('User was added automatically as they were new. Their oid is: ', token.oid);
                    this.users.push(token);
                    this.owner = token.oid;
                    const val = done(null, token);
                    console.log(`after strategy done authenticate time: ${Date.now()}`)
                    return val;
                }
                this.owner = token.oid;
                const val = done(null, user, token);
                console.log(`after strategy done authenticate time: ${Date.now()}`);
                return val;
            });
        }));
    }

Это все работает - моя конфигурация и настройка Azure для этого входит в систему и получает access_token для моего API, и этот токен успешно аутентифицируется, и пользовательский объект помещается в запрос.

ОДНАКО Passport.authenticate() кажется асинхронным и не завершается до окончания теста на request.isAuthenticated(). Я поместил в комментарии времени, как можно видеть. after passport authenticate time: xxx происходит через 2 миллисекунды после before.

И after strategy done authenticate time: xxx происходит через секунду после after passport authenticate time: xxx.

Так мне кажется асинхронное поведение.

Глядя в node_modules/passport/lib/middleware/authenticate.js (https://github.com/jaredhanson/passport/blob/master/lib/middleware/authenticate.js), там не упоминаются обещания или асинхронность. Однако в node_modules/passport-azure-ad/lib/bearerstrategy.js (https://github.com/AzureAD/passport-azure-ad/blob/dev/lib/bearerstrategy.js) это async.waterfall:

/*
 * We let the metadata loading happen in `authenticate` function, and use waterfall
 * to make sure the authentication code runs after the metadata loading is finished.
 */
Strategy.prototype.authenticate = function authenticateStrategy(req, options) {
  const self = this;
  var params = {};
  var optionsToValidate = {};
  var tenantIdOrName = options && options.tenantIdOrName;

  /* Some introduction to async.waterfall (from the following link):
   * http://stackoverflow.com/questions/28908180/what-is-a-simple-implementation-of-async-waterfall
   *
   *   Runs the tasks array of functions in series, each passing their results 
   * to the next in the array. However, if any of the tasks pass an error to 
   * their own callback, the next function is not executed, and the main callback
   * is immediately called with the error.
   *
   * Example:
   *
   * async.waterfall([
   *   function(callback) {
   *     callback(null, 'one', 'two');
   *   },
   *   function(arg1, arg2, callback) {
   *     // arg1 now equals 'one' and arg2 now equals 'two'
   *     callback(null, 'three');
   *   },
   *   function(arg1, callback) {
   *     // arg1 now equals 'three'
   *     callback(null, 'done');
   *   }
   * ], function (err, result) {
   *      // result now equals 'done'    
   * }); 
   */
  async.waterfall([

    // compute metadataUrl
    (next) => {
      params.metadataURL = aadutils.concatUrl(self._options.identityMetadata,
        [
          `${aadutils.getLibraryProductParameterName()}=${aadutils.getLibraryProduct()}`,
          `${aadutils.getLibraryVersionParameterName()}=${aadutils.getLibraryVersion()}`
        ]
      );

      // if we are not using the common endpoint, but we have tenantIdOrName, just ignore it
      if (!self._options._isCommonEndpoint && tenantIdOrName) {
          ...
      ...
      return self.jwtVerify(req, token, params.metadata, optionsToValidate, verified);
    }],

    (waterfallError) => { // This function gets called after the three tasks have called their 'task callbacks'
      if (waterfallError) {
        return self.failWithLog(waterfallError);
      }
      return true;
    }
  );
};

Может ли это вызвать асинхронный код? Будет ли это проблемой, если запустить в «нормальной экспресс-промежуточного программного обеспечения»? Может ли кто-нибудь подтвердить то, что я сказал, или опровергнуть то, что я сказал, и предложить решение, которое работает.


Для справки, я начал обращаться за помощью по этой проблеме Passport-Azure-Ad в своем вопросе SO - Azure AD открывает BearerStrategy "TypeError: self.success не является функцией" . Кажется, проблемы там решены.


Редактировать - заголовок, первоначально включенный «в структуру TSED», но я полагаю, что описанная проблема существует исключительно в passport-azure-ad.

1 Ответ

0 голосов
/ 03 июня 2019

Это решение обойти, как мне кажется, проблему с passport-azure-ad асинхронностью, но без возможности контролировать это. Это не тот ответ, который я хотел бы - подтвердить то, что я сказал или отрицаю то, что я сказал, и предоставлю решение, которое работает.

Ниже приведено решение для https://tsed.io фреймворка. В https://github.com/TypedProject/ts-express-decorators/issues/559 они предлагают не использовать @OverrideMiddleware(AuthenticatedMiddleware), а использовать промежуточное программное обеспечение @UseAuth. Это работает так в целях иллюстрации, что здесь не важно (я скоро проработаю обратную связь).

@OverrideMiddleware(AuthenticatedMiddleware)
export class UserAuthMiddleware implements IMiddleware {
    constructor(@Inject() private authService: AuthService) {
    }

    // NO THIS VERSION DOES NOT WORK.  I even removed checkForAuthentication() and
    // inlined the setInterval() but it made no difference
    // Before the 200 is sent WITH content, a 204 NO CONTENT is

    // HAD TO CHANGE to the setTimeout() version
    // async checkForAuthentication(request: express.Request): Promise<void> {
    //     return new Promise<void>(resolve => {

    //         let iterations = 30;
    //        const id = setInterval(() => {
    //             if (request.isAuthenticated() || iterations-- <= 0) {
    //                 clearInterval(id);
    //                 resolve();
    //             }
    //         }, 50);
    //     });
    // }

    // @async
    public use(
        @EndpointInfo() endpoint: EndpointMetadata,
        @Request() request: express.Request,
        @Response() response: express.Response,
        @Next() next: express.NextFunction
    ) {
        const options = endpoint.get(AuthenticatedMiddleware) || {};
        this.authService.authenticate(request, response, next);

        // AS DISCUSSED above this doesn't work
        // await this.checkForAuthentication(request);
        // TODO - check roles in options against AD scopes
        // if (!request.isAuthenticated()) {
        //     throw new Forbidden('Forbidden');
        // }
        // next();

        // HAD TO USE setTimeout()
        setTimeout(() => {
            if (!request.isAuthenticated()) {
                console.log(`throw forbidden`);
                throw new Forbidden('Forbidden');
            }
            next();
        }, 1500);
}

Редактировать - У меня была версия, которая использовала setInterval(), но я обнаружил, что она не работает. Я даже попытался встроить код в один метод, чтобы я мог удалить async. Казалось, что @Post, к которому прикреплен UserAuthMiddleware, немедленно завершает работу и возвращает 204 «Нет содержимого». Последовательность завершится после этого, и 200 с желаемым содержанием будут возвращены, но было слишком поздно. Я не понимаю почему.

...