Skip to content

Commit

Permalink
refac(auth): loading github module optionally
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshPatel5940 committed Feb 6, 2024
1 parent ca12cc3 commit 6a17f33
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 16 deletions.
26 changes: 23 additions & 3 deletions apps/api/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Module } from '@nestjs/common'
import { Logger, Module } from '@nestjs/common'
import { AuthService } from './service/auth.service'
import { AuthController } from './controller/auth.controller'
import { JwtModule } from '@nestjs/jwt'
import { UserModule } from '../user/user.module'
import { GithubStrategy } from './github.stratergy'
import { GithubEnvService, GithubStrategy } from './github.stratergy'

@Module({
imports: [
Expand All @@ -18,7 +18,27 @@ import { GithubStrategy } from './github.stratergy'
}),
UserModule
],
providers: [AuthService, GithubStrategy],
providers: [
AuthService,
GithubEnvService,
{
provide: GithubStrategy,
useFactory: (githubEnvService: GithubEnvService) => {
if (githubEnvService.isGithubEnabled()) {
const creds = githubEnvService.getGithubCredentials()
return new GithubStrategy(
creds.clientID,
creds.clientSecret,
creds.callbackURL
)
} else {
Logger.warn('Github Login Is Not Enabled In This Environment')
return null
}
},
inject: [GithubEnvService]
}
],
controllers: [AuthController]
})
export class AuthModule {}
4 changes: 4 additions & 0 deletions apps/api/src/auth/controller/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { JwtService } from '@nestjs/jwt'
import { PrismaService } from '../../prisma/prisma.service'
import { AuthController } from './auth.controller'
import { mockDeep } from 'jest-mock-extended'
import { GithubEnvService } from '../github.stratergy'
import { ConfigService } from '@nestjs/config'

describe('AuthController', () => {
let controller: AuthController
Expand All @@ -15,6 +17,8 @@ describe('AuthController', () => {
controllers: [AuthController],
providers: [
AuthService,
GithubEnvService,
ConfigService,
{ provide: MAIL_SERVICE, useClass: MockMailService },
JwtService,
PrismaService
Expand Down
24 changes: 16 additions & 8 deletions apps/api/src/auth/controller/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import {
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
Query,
Req,
Res,
UseGuards
} from '@nestjs/common'
import { AuthService } from '../service/auth.service'
import { UserAuthenticatedResponse } from '../auth.types'
import { Public } from '../../decorators/public.decorator'
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'
import { AuthGuard } from '@nestjs/passport'
import { GithubEnvService } from '../github.stratergy'

@ApiTags('Auth Controller')
@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}
constructor(
private authService: AuthService,
private readonly githubEnvService: GithubEnvService
) {}

@Public()
@Post('send-otp/:email')
Expand Down Expand Up @@ -84,18 +90,20 @@ export class AuthController {

@Public()
@Get('github')
@UseGuards(AuthGuard('github'))
@ApiOperation({
summary: 'Github OAuth',
description:
'This endpoint validates Github OAuth. If the OAuth is valid, it returns a valid token along with the user details'
})
async githubOAuthLogin() {
/**
* NOTE:
* This function does nothing and the oauth redirect is managed my AuthGuard
* - The 'github' method inside the authguard is managed by passport
*/
async githubOAuthLogin(@Res() res) {
if (!this.githubEnvService.isGithubEnabled()) {
throw new HttpException(

Check warning on line 100 in apps/api/src/auth/controller/auth.controller.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/auth/controller/auth.controller.ts#L99-L100

Added lines #L99 - L100 were not covered by tests
'Github Login Is Not Enabled In This Environment',
HttpStatus.BAD_REQUEST
)
}

res.status(302).redirect('/api/auth/github/callback')

Check warning on line 106 in apps/api/src/auth/controller/auth.controller.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/auth/controller/auth.controller.ts#L106

Added line #L106 was not covered by tests
}

@Public()
Expand Down
36 changes: 31 additions & 5 deletions apps/api/src/auth/github.stratergy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { Profile, Strategy } from 'passport-github2'

@Injectable()
export class GithubStrategy extends PassportStrategy(Strategy, 'github') {
constructor(configService: ConfigService) {
const clientID = configService.get<string>('GITHUB_CLIENT_ID')
const clientSecret = configService.get<string>('GITHUB_CLIENT_SECRET')
const callbackURL = configService.get<string>('GITHUB_CALLBACK_URL')
constructor(clientID: string, clientSecret: string, callbackURL: string) {

Check warning on line 8 in apps/api/src/auth/github.stratergy.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/auth/github.stratergy.ts#L8

Added line #L8 was not covered by tests
super({
clientID,
clientSecret,
Expand All @@ -17,7 +14,36 @@ export class GithubStrategy extends PassportStrategy(Strategy, 'github') {
})
}

async validate(accessToken: string, _refreshToken: string, profile: Profile) {
async validate(
_accessToken: string,
_refreshToken: string,
profile: Profile

Check warning on line 20 in apps/api/src/auth/github.stratergy.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/auth/github.stratergy.ts#L20

Added line #L20 was not covered by tests
) {
return profile
}
}

// todo: find a better place to keep this
@Injectable()
export class GithubEnvService {
private clientID: string
private clientSecret: string
private callbackURL: string
constructor(private readonly configService: ConfigService) {}

isGithubEnabled(): boolean {
this.clientID = this.configService.get<string>('GITHUB_CLIENT_ID')
this.clientSecret = this.configService.get<string>('GITHUB_CLIENT_SECRET')
this.callbackURL = this.configService.get<string>('GITHUB_CALLBACK_URL')

Check warning on line 37 in apps/api/src/auth/github.stratergy.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/auth/github.stratergy.ts#L34-L37

Added lines #L34 - L37 were not covered by tests

return Boolean(this.clientID && this.clientSecret && this.callbackURL)

Check warning on line 39 in apps/api/src/auth/github.stratergy.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/auth/github.stratergy.ts#L39

Added line #L39 was not covered by tests
}

getGithubCredentials() {
return {

Check warning on line 43 in apps/api/src/auth/github.stratergy.ts

View check run for this annotation

Codecov / codecov/patch

apps/api/src/auth/github.stratergy.ts#L42-L43

Added lines #L42 - L43 were not covered by tests
clientID: this.clientID,
clientSecret: this.clientSecret,
callbackURL: this.callbackURL
}
}
}

0 comments on commit 6a17f33

Please sign in to comment.