Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 94 additions & 9 deletions BE/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions BE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/axios": "^4.0.1",
"@nestjs/common": "^11.0.1",
"@nestjs/config": "^4.0.2",
"@nestjs/core": "^11.0.1",
"@nestjs/platform-express": "^11.0.1",
"axios": "^1.13.2",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1"
},
Expand Down
9 changes: 8 additions & 1 deletion BE/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { ConfigModule } from '@nestjs/config';

@Module({
imports: [],
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
AuthModule,
],
controllers: [AppController],
providers: [AppService],
})
Expand Down
27 changes: 27 additions & 0 deletions BE/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Controller, Get, Query, Res } from '@nestjs/common';
import type { Response } from 'express';
import { AuthService } from './auth.service';

@Controller()
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Get('api/auth/login')
login(@Res() res: Response) {
const params = new URLSearchParams({
client_id: process.env.GITHUB_CLIENT_ID ?? '',
redirect_uri: process.env.GITHUB_REDIRECT_URI ?? '',
scope: 'read:org',
});

return res.redirect(`https://github.com/login/oauth/authorize?${params.toString()}`);
}

@Get('auth/github/callback')
async callback(@Query('code') code?: string) {
if (!code) {
return { success: false, message: '코드가 없습니당' };
}
return await this.authService.verifyCohortByGithubOAuth(code);
}
}
12 changes: 12 additions & 0 deletions BE/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { GithubOAuthClient } from './github-oauth.client';

@Module({
imports: [HttpModule],
controllers: [AuthController],
providers: [AuthService, GithubOAuthClient],
})
export class AuthModule {}
25 changes: 25 additions & 0 deletions BE/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common';
import { GithubOAuthClient } from './github-oauth.client';
import { resolveCohortFromOrgs } from './cohort-rules';

@Injectable()
export class AuthService {
constructor(private readonly github: GithubOAuthClient) {}

async verifyCohortByGithubOAuth(code: string) {
// ✅ code -> access token
const accessToken = await this.github.exchangeCodeForToken(code);

// ✅ access token으로 내 org 목록 조회
const orgLogins = await this.github.getUserOrgs(accessToken);

// ✅ org 목록 -> cohort 판정
const cohort = resolveCohortFromOrgs(orgLogins);

return {
accessTokenReceived: Boolean(accessToken),
orgLogins,
cohort,
};
}
}
19 changes: 19 additions & 0 deletions BE/src/auth/cohort-rules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const COHORT_ORG_MAP: Record<number, string[]> = {
6: ['boostcampwm-2021'],
7: ['boostcampwm-2022'],
8: ['boostcampwm2023'],
9: ['boostcampwm-2024'],
10: ['boostcampwm2025'],
11: ['0x05-hex-five'],
};

export function resolveCohortFromOrgs(orgLogins: string[]): number | null {
for (const [cohortStr, orgs] of Object.entries(COHORT_ORG_MAP)) {
const cohort = Number(cohortStr);
if (orgs.some((org) => orgLogins.includes(org))) {
return cohort;
}
}

return null;
}
48 changes: 48 additions & 0 deletions BE/src/auth/github-oauth.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';

type TokenResponse = {
access_token: string;
token_type: string;
scope: string;
};

@Injectable()
export class GithubOAuthClient {
constructor(private readonly http: HttpService) {}

async exchangeCodeForToken(code: string): Promise<string> {
// redirect_uri로 전달받은 일회성 인가 코드 -> Access Token 발급받아오기
const res = await firstValueFrom(
this.http.post<TokenResponse>(
'https://github.com/login/oauth/access_token',
{
client_id: process.env.GITHUB_CLIENT_ID,
client_secret: process.env.GITHUB_CLIENT_SECRET,
redirect_uri: process.env.GITHUB_REDIRECT_URI,
code,
},
{
headers: {
Accept: 'application/json',
},
},
),
);

return res.data.access_token;
}

async getUserOrgs(accessToken: string): Promise<string[]> {
const res = await firstValueFrom(
this.http.get<Array<{ login: string }>>('https://api.github.com/user/orgs', {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: 'application/vnd.github+json',
},
}),
);
return res.data.map((o) => o.login);
}
}