Skip to content

Commit

Permalink
[Feat] GitHub Auth
Browse files Browse the repository at this point in the history
Created github authentication mechanisms using Passport.js.
References #17.
  • Loading branch information
angel-penchev committed Oct 13, 2021
1 parent 2d2e260 commit bb050e5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dotenv": "^10.0.0",
"express-session": "^1.17.2",
"passport-discord": "^0.1.4",
"passport-github2": "^0.1.12",
"passport-google-oauth20": "^2.0.0",
"pg": "^8.7.1",
"reflect-metadata": "^0.1.13",
Expand Down
2 changes: 2 additions & 0 deletions server/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AuthController } from './controllers/auth.controller';
import { AuthDiscordStrategy } from './strategies/auth.discord.strategy';
import { AuthGithubStrategy } from './strategies/auth.github.strategy';
import { AuthGoogleStrategy } from './strategies/auth.google.strategy';
import { AuthService } from './services/auth.service';
import { Module } from '@nestjs/common';
Expand All @@ -15,6 +16,7 @@ import { UsersService } from 'src/users/services/users.service';
providers: [
AuthGoogleStrategy,
AuthDiscordStrategy,
AuthGithubStrategy,
SessionSerializerUtil,
UsernameUtil,
AuthService,
Expand Down
7 changes: 7 additions & 0 deletions server/src/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export class AuthController {
return;
}

@Get('/github/login')
@UseGuards(AuthGithubGuard)
getGithubLogin(@Res() res: Response) {
res.sendStatus(HttpStatus.OK);
return;
}

@Get('/status')
@UseGuards(AuthSessionGuard)
getStatus(@Req() req: Request) {
Expand Down
13 changes: 13 additions & 0 deletions server/src/auth/guards/auth.github.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ExecutionContext, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class AuthGithubGuard extends AuthGuard('github') {
async canActivate(context: ExecutionContext) {
const canActivate = (await super.canActivate(context)) as boolean;
const request = context.switchToHttp().getRequest();

await super.logIn(request);
return canActivate;
}
}
40 changes: 40 additions & 0 deletions server/src/auth/strategies/auth.github.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Profile, Strategy } from 'passport-github2';

import { AuthService } from '../services/auth.service';
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { UsernameUtil } from '../utils/auth.username.util';
import { configObject } from 'src/configuration';

@Injectable()
export class AuthGithubStrategy extends PassportStrategy(Strategy) {
constructor(
private readonly authService: AuthService,
private readonly usernameUtil: UsernameUtil,
) {
super(configObject.github);
}

validate(
accessToken: string,
refreshToken: string,
profile: Profile,
done: any,
) {
const {
username: githubUsername,
id: githubId,
displayName: name,
photos,
} = profile;

// As github usernames are not ensured to be unique, random characters are added.
const username = this.usernameUtil.addRandomSequence(githubUsername);

const avatar = photos[0].value;
const userDetails = { username, githubId, name, avatar };

const user = this.authService.validateUser(userDetails);
done(null, user);
}
}
6 changes: 6 additions & 0 deletions server/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ const configuration = () => ({
callbackApi: process.env.DISCORD_CALLBACK_API,
scope: ['identify'],
},
github: {
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackApi: process.env.GOOGLE_CALLBACK_API,
scope: ['profile'],
},
});

const configObject = configuration();
Expand Down

0 comments on commit bb050e5

Please sign in to comment.