-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
370 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
libs/api/auth/data-access/src/lib/strategies/oauth/api-auth-strategy-google-guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { AuthGuard } from '@nestjs/passport' | ||
|
||
@Injectable() | ||
export class ApiAuthStrategyGoogleGuard extends AuthGuard('google') {} |
33 changes: 33 additions & 0 deletions
33
libs/api/auth/data-access/src/lib/strategies/oauth/api-auth-strategy-google.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { type DynamicModule, Logger, Module } from '@nestjs/common' | ||
import { ApiCoreDataAccessModule } from '@pubkey-stack/api-core-data-access' | ||
import { ApiAuthStrategyService } from '../api-auth-strategy.service' | ||
import { ApiAuthStrategyGoogle } from './api-auth-strategy-google' | ||
|
||
@Module({}) | ||
export class ApiAuthStrategyGoogleModule { | ||
static logger = new Logger(ApiAuthStrategyGoogleModule.name) | ||
static register(): DynamicModule { | ||
const enabled = this.enabled | ||
if (!enabled) { | ||
this.logger.warn(`Google Auth DISABLED`) | ||
return { module: ApiAuthStrategyGoogleModule } | ||
} | ||
this.logger.verbose(`Google Auth ENABLED`) | ||
return { | ||
module: ApiAuthStrategyGoogleModule, | ||
imports: [ApiCoreDataAccessModule], | ||
providers: [ApiAuthStrategyGoogle, ApiAuthStrategyService], | ||
} | ||
} | ||
|
||
// TODO: These should be coming from the ApiCoreConfigService instead of process.env | ||
private static get enabled(): boolean { | ||
return ( | ||
// Google auth needs to be enabled | ||
!!process.env['AUTH_GOOGLE_ENABLED'] && | ||
// And we need to have the client ID and secret set | ||
!!process.env['AUTH_GOOGLE_CLIENT_ID'] && | ||
!!process.env['AUTH_GOOGLE_CLIENT_SECRET'] | ||
) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
libs/api/auth/data-access/src/lib/strategies/oauth/api-auth-strategy-google.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { PassportStrategy } from '@nestjs/passport' | ||
import { IdentityProvider } from '@prisma/client' | ||
import { ApiCoreService } from '@pubkey-stack/api-core-data-access' | ||
import { Profile, Strategy } from 'passport-google-oauth20' | ||
import type { ApiAuthRequest } from '../../interfaces/api-auth.request' | ||
import { ApiAuthStrategyService } from '../api-auth-strategy.service' | ||
|
||
@Injectable() | ||
export class ApiAuthStrategyGoogle extends PassportStrategy(Strategy, 'google') { | ||
constructor(private core: ApiCoreService, private service: ApiAuthStrategyService) { | ||
super(core.config.authGoogleStrategyOptions) | ||
} | ||
|
||
async validate(req: ApiAuthRequest, accessToken: string, refreshToken: string, profile: Profile) { | ||
return this.service.validateRequest({ | ||
req, | ||
providerId: profile.id, | ||
provider: IdentityProvider.Google, | ||
accessToken, | ||
refreshToken, | ||
profile: createGoogleProfile(profile), | ||
}) | ||
} | ||
} | ||
|
||
function createGoogleProfile(profile: Profile) { | ||
return { | ||
externalId: profile.id, | ||
username: profile.username, | ||
avatarUrl: profile.photos?.[0].value, | ||
name: profile.displayName, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
libs/api/auth/feature/src/lib/api-auth-strategy-google.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common' | ||
|
||
import { | ||
ApiAnonJwtGuard, | ||
ApiAuthRequest, | ||
ApiAuthService, | ||
ApiAuthStrategyGoogleGuard, | ||
} from '@pubkey-stack/api-auth-data-access' | ||
import { Response } from 'express-serve-static-core' | ||
|
||
@Controller('auth/google') | ||
export class ApiAuthStrategyGoogleController { | ||
constructor(private readonly service: ApiAuthService) {} | ||
|
||
@Get() | ||
@UseGuards(ApiAuthStrategyGoogleGuard) | ||
redirect() { | ||
// This method triggers the OAuth2 flow | ||
} | ||
|
||
@Get('callback') | ||
@UseGuards(ApiAnonJwtGuard, ApiAuthStrategyGoogleGuard) | ||
async callback(@Req() req: ApiAuthRequest, @Res({ passthrough: true }) res: Response) { | ||
return this.service.userCookieRedirect(req, res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.