-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Added GitLab OAuth (#188)
- Loading branch information
Showing
11 changed files
with
216 additions
and
17 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
46 changes: 46 additions & 0 deletions
46
apps/api/src/config/factory/gitlab/gitlab-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 { GitlabStrategy } from '../../oauth-strategy/gitlab/gitlab.strategy' | ||
import { GitlabOAuthStrategyFactory } from './gitlab-strategy.factory' | ||
|
||
describe('GitlabOAuthStrategyFactory', () => { | ||
let factory: GitlabOAuthStrategyFactory | ||
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('should disable OAuth when credentials are not present', () => { | ||
jest.spyOn(configService, 'get').mockReturnValue('') | ||
factory = new GitlabOAuthStrategyFactory(configService) | ||
expect(factory.isOAuthEnabled()).toBe(false) | ||
}) | ||
|
||
it('should return null when OAuth is disabled', () => { | ||
const strategy = factory.createOAuthStrategy() | ||
expect(strategy).toBeNull() | ||
}) | ||
|
||
it('should enable OAuth when credentials are present', () => { | ||
jest | ||
.spyOn(configService, 'get') | ||
.mockImplementation((key) => | ||
key === 'GITLAB_CLIENT_ID' || | ||
key === 'GITLAB_CLIENT_SECRET' || | ||
key === 'GITLAB_CALLBACK_URL' | ||
? 'test' | ||
: '' | ||
) | ||
factory = new GitlabOAuthStrategyFactory(configService) | ||
expect(factory.isOAuthEnabled()).toBe(true) | ||
}) | ||
|
||
it('should create OAuth strategy when enabled', () => { | ||
const strategy = factory.createOAuthStrategy() | ||
expect(strategy).toBeInstanceOf(GitlabStrategy) | ||
}) | ||
}) |
35 changes: 35 additions & 0 deletions
35
apps/api/src/config/factory/gitlab/gitlab-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,35 @@ | ||
import { Injectable, Logger } from '@nestjs/common' | ||
import { ConfigService } from '@nestjs/config' | ||
import { OAuthStrategyFactory } from '../oauth-strategy.factory' | ||
import { GitlabStrategy } from '../../oauth-strategy/gitlab/gitlab.strategy' | ||
|
||
@Injectable() | ||
export class GitlabOAuthStrategyFactory 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>('GITLAB_CLIENT_ID') | ||
this.clientSecret = this.configService.get<string>('GITLAB_CLIENT_SECRET') | ||
this.callbackURL = this.configService.get<string>('GITLAB_CALLBACK_URL') | ||
} | ||
|
||
public isOAuthEnabled(): boolean { | ||
return Boolean(this.clientID && this.clientSecret && this.callbackURL) | ||
} | ||
|
||
public createOAuthStrategy<GitlabStrategy>(): GitlabStrategy | null { | ||
if (this.isOAuthEnabled()) { | ||
return new GitlabStrategy( | ||
this.clientID, | ||
this.clientSecret, | ||
this.callbackURL | ||
) as GitlabStrategy | ||
} else { | ||
Logger.warn( | ||
'GitLab 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/gitlab/gitlab.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 { GitlabStrategy } from './gitlab.strategy' | ||
|
||
describe('GitlabStrategy', () => { | ||
let strategy: GitlabStrategy | ||
|
||
beforeEach(() => { | ||
strategy = new GitlabStrategy('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/gitlab/gitlab.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 { Profile, Strategy } from 'passport-gitlab2' | ||
|
||
@Injectable() | ||
export class GitlabStrategy extends PassportStrategy(Strategy, 'gitlab') { | ||
constructor(clientID: string, clientSecret: string, callbackURL: string) { | ||
super({ | ||
clientID, | ||
clientSecret, | ||
callbackURL, | ||
scope: ['read_user'] | ||
}) | ||
} | ||
|
||
async validate( | ||
_accessToken: string, | ||
_refreshToken: string, | ||
profile: Profile | ||
): Promise<Profile> { | ||
return profile | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.