kndt84 / passport-cognito

Passport strategy for AWS Cognito User Pools
https://www.npmjs.com/package/passport-cognito
MIT License
78 stars 30 forks source link

Is it possible to integrate the passport-cognito into Nest.js? #34

Open ostmond opened 4 years ago

ostmond commented 4 years ago

Hi, Is it possible to integrate the passport-cognito into Nest.js by using PassportStrategy and AuthGuard? I found the JwtStrategy worked only for Auth0 and would like to customize a CognitoStrategy extends PassportStrategy(Strategy), something like following in TypeScript:

import { Strategy } from 'passport-cognito';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class CognitoStrategy extends PassportStrategy(Strategy) {
  private readonly logger = new Logger(CognitoStrategy.name);

  constructor() {
    super( // please help here...);
  }

  async validate(payload: any) {
    this.logger.log('payload: ', payload);
    return payload;
  }
}

Thanks a lot

XenorPLxx commented 4 years ago
constructor() {
    super({
        userPoolId: '****',
        clientId: '***',
        region: '****',
      },
      function(accessToken, idToken, refreshToken, user, cb) {
        cb(null, user);
      }
    })
}

There is no need for validate function, the cb callback does all the work - the second parameter is attached as req.user.

As far as I understand this is NOT A REPLACEMENT for JWT token verification, as this strategy only allows to authorize user using username and password in the request body (same thing as new CognitoUser(userData).authenticateUser from aws-amplify). I still didn't find a way to properly validate an AWS Cognito JWT token with strategies.