jaredhanson / passport-oauth2

OAuth 2.0 authentication strategy for Passport and Node.js.
https://www.passportjs.org/packages/passport-oauth2/?utm_source=github&utm_medium=referral&utm_campaign=passport-oauth2&utm_content=about
MIT License
605 stars 343 forks source link

Passport-oauth2 not working on AWS lambda #154

Closed JoseDRojasA closed 2 years ago

JoseDRojasA commented 2 years ago

I'm trying to implement OAuth2 with Google in an AWS lambda function. This is my strategy implementation.

// google-strategy-guard.ts

import { Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";

@Injectable()
export class GoogleStrategyGuard extends AuthGuard('google') {
  logIn<TRequest extends { logIn: Function; } = any>(request: TRequest): Promise<void> {
    console.log('EXECUTING logIn')
    return super.logIn(request);
  }

  handleRequest<TUser = any>(err: any, user: any, info: any, context: any, status?: any): TUser {
    console.log('EXECUTING handleRequest')
    console.log('user', user);
    console.log('info', info);
    console.log('context', context);
    console.log('status', status);
    const result = super.handleRequest(err, user, info, context, status);
    console.log('result', result);
    return result;
  }

  canActivate(context) {
    console.log('Context', context);
    return super.canActivate(context);
  }
}

// google-strategy-service.ts

import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Configuration } from '@nexus/configuration';
import { UserDTO } from '@nexus/models';
import Axios from 'axios';
import { Strategy } from 'passport-oauth2';
import { GoogleOAuthUser } from './google-oauth-user';

const { authentication: { googleStrategy } } = Configuration();

const authorizationURL = 'https://accounts.google.com/o/oauth2/v2/auth';
const tokenURL =  'https://www.googleapis.com/oauth2/v4/token';
const userProfileURL = 'https://www.googleapis.com/oauth2/v3/userinfo';

@Injectable()
export class GoogleStrategyService extends PassportStrategy(Strategy, 'google') {
  constructor() {
    super({...googleStrategy, authorizationURL, tokenURL});
  }

  async validate(accessToken: string) {
    const profile: GoogleOAuthUser = await Axios.get(`${userProfileURL}?access_token=${accessToken}`);
    const { name, email, picture } = profile.data;
    const user: Partial<UserDTO> = {
      email: email,
      name,
      picture,
      accessToken,
    }
    return user;
  }
}

When I test it locally it works perfectly, but When I deploy it in AWS, It is not working.

I have 2 routes:

The first one works, It shows me the page to select my google account, when I select my google account, It redirects me to the second route (/login/google/redirect/) but I received a timeout error.

In GCP, I have the following Authorized domains

image

I also checked Authorized redirect URIs and they look good. image

I suspect the issue is on my Authorized domains but not sure how to solve it.

Thank you in advance.

nerixim commented 2 years ago

Can confirm you can use this library on Lambda (I'm using Express with API Gateway).