Использование JwtStrategy в Nest js, когда secretOrKey недоступен - PullRequest
0 голосов
/ 02 мая 2020

Я пытаюсь интегрировать AWS Cognito в мое приложение Nest js, потому что Nodejs приложения не поддерживают использование secretOrKey с AWS Cognito, я ищу способ реализовать это с помощью secretOrKeyProvider, но мне нужна помощь, чтобы понять, если да, то как я могу это сделать.

Это моя текущая реализация JwtStrategy:

import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { ClaimVerifyResult, handler } from './jwt.verify';
import { AuthService } from './auth.service';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: true,
      secretOrKeyProvider: (_, token, done) => {
        //token here is valid
        //token has not expired
        //calling {handler} function here will return a valid user entity
        //calling done here with the valid user will still result in unauthorized
        return token;
      },
    });
  }

  public async validate(
    payload: any,
    done: (err: Error | null, result: ClaimVerifyResult) => void,
  ) {
    console.log('validating');
    const userInfo = await handler(payload);
    if (!userInfo) {
      return done(new UnauthorizedException(), null);
    }
    done(null, userInfo);
  }
}

Я могу получить обратную связь secretOrKeyProvider , но я не могу заставить функцию validate быть вызванной.

...