-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
10 changed files
with
190 additions
and
3 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
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
46 changes: 46 additions & 0 deletions
46
apps/api/src/config/factory/google/google-strategy.factory.spec.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,46 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { ConfigService } from '@nestjs/config' | ||
import { GoogleStrategy } from '../../oauth-strategy/google/google.strategy' | ||
import { GoogleOAuthStrategyFactory } from './google-strategy.factory' | ||
|
||
describe('GoogleOAuthStrategyFactory', () => { | ||
let factory: GoogleOAuthStrategyFactory | ||
let configService: ConfigService | ||
|
||
beforeEach(async () => { | ||
const moduleRef: TestingModule = await Test.createTestingModule({ | ||
providers: [{ provide: ConfigService, useValue: { get: jest.fn() } }] | ||
}).compile() | ||
configService = moduleRef.get<ConfigService>(ConfigService) | ||
}) | ||
|
||
it('disable when credentials are not present', () => { | ||
jest.spyOn(configService, 'get').mockReturnValue('') | ||
factory = new GoogleOAuthStrategyFactory(configService) | ||
expect(factory.isOAuthEnabled()).toBe(false) | ||
}) | ||
|
||
it('return null when OAuth disabled', () => { | ||
const strategy = factory.createOAuthStrategy() | ||
expect(strategy).toBeNull() | ||
}) | ||
|
||
it('enable OAuth when credentials present', () => { | ||
jest | ||
.spyOn(configService, 'get') | ||
.mockImplementation((key) => | ||
key === 'GOOGLE_CLIENT_ID' || | ||
key === 'GOOGLE_CLIENT_SECRET' || | ||
key === 'GOOGLE_CALLBACK_URL' | ||
? 'test' | ||
: '' | ||
) | ||
factory = new GoogleOAuthStrategyFactory(configService) | ||
expect(factory.isOAuthEnabled()).toBe(true) | ||
}) | ||
|
||
it('create OAuth strategy when enabled', () => { | ||
const strategy = factory.createOAuthStrategy() | ||
expect(strategy).toBeInstanceOf(GoogleStrategy) | ||
}) | ||
}) |
36 changes: 36 additions & 0 deletions
36
apps/api/src/config/factory/google/google-strategy.factory.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,36 @@ | ||
import { Injectable, Logger } from '@nestjs/common' | ||
import { ConfigService } from '@nestjs/config' | ||
import { OAuthStrategyFactory } from '../oauth-strategy.factory' | ||
import { GoogleStrategy } from '../../oauth-strategy/google/google.strategy' | ||
|
||
@Injectable() | ||
export class GoogleOAuthStrategyFactory implements OAuthStrategyFactory { | ||
private readonly clientID: string | ||
private readonly clientSecret: string | ||
private readonly callbackURL: string | ||
|
||
constructor(private readonly configService: ConfigService) { | ||
this.clientID = this.configService.get<string>('GOOGLE_CLIENT_ID') | ||
this.clientSecret = this.configService.get<string>('GOOGLE_CLIENT_SECRET') | ||
this.callbackURL = this.configService.get<string>('GOOGLE_CALLBACK_URL') | ||
} | ||
|
||
public isOAuthEnabled(): boolean { | ||
return Boolean(this.clientID && this.clientSecret && this.callbackURL) | ||
} | ||
|
||
public createOAuthStrategy<GoogleStrategy>(): GoogleStrategy | null { | ||
if (this.isOAuthEnabled()) { | ||
return new GoogleStrategy( | ||
this.clientID, | ||
this.clientSecret, | ||
this.callbackURL | ||
) as GoogleStrategy | ||
} else { | ||
Logger.warn( | ||
'Google Auth is not enabled in this environment. Refer to the https://docs.keyshade.xyz/contributing-to-keyshade/environment-variables if you would like to set it up.' | ||
) | ||
return null | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
apps/api/src/config/oauth-strategy/google/google.strategy.spec.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,17 @@ | ||
import { GoogleStrategy } from './google.strategy' | ||
|
||
describe('GoogleStrategy', () => { | ||
let strategy: GoogleStrategy | ||
|
||
beforeEach(() => { | ||
strategy = new GoogleStrategy('clientID', 'clientSecret', 'callbackURL') | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(strategy).toBeDefined() | ||
}) | ||
|
||
it('should have a validate method', () => { | ||
expect(strategy.validate).toBeDefined() | ||
}) | ||
}) |
23 changes: 23 additions & 0 deletions
23
apps/api/src/config/oauth-strategy/google/google.strategy.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,23 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { PassportStrategy } from '@nestjs/passport' | ||
import { Strategy, Profile } from 'passport-google-oauth20' | ||
|
||
@Injectable() | ||
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { | ||
constructor(clientID: string, clientSecret: string, callbackURL: string) { | ||
super({ | ||
clientID, | ||
clientSecret, | ||
callbackURL, | ||
scope: ['profile', 'email'] | ||
}) | ||
} | ||
|
||
async validate( | ||
accessToken: string, | ||
refreshToken: string, | ||
profile: Profile | ||
): Promise<Profile> { | ||
return profile | ||
} | ||
} |