diff --git a/BE/.prettierrc b/BE/.prettierrc index a20502b..d28cdfb 100644 --- a/BE/.prettierrc +++ b/BE/.prettierrc @@ -1,4 +1,10 @@ { + "semi": true, + "printWidth": 100, + "endOfLine": "auto", "singleQuote": true, - "trailingComma": "all" + "useTabs": false, + "tabWidth": 2, + "trailingComma": "all", + "arrowParens": "always" } diff --git a/BE/eslint.config.mjs b/BE/eslint.config.mjs index 4e9f827..09d6c56 100644 --- a/BE/eslint.config.mjs +++ b/BE/eslint.config.mjs @@ -17,9 +17,9 @@ export default tseslint.config( ...globals.node, ...globals.jest, }, - sourceType: 'commonjs', + sourceType: 'module', parserOptions: { - projectService: true, + project: ['./tsconfig.json'], tsconfigRootDir: import.meta.dirname, }, }, @@ -29,7 +29,36 @@ export default tseslint.config( '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-floating-promises': 'warn', '@typescript-eslint/no-unsafe-argument': 'warn', - "prettier/prettier": ["error", { endOfLine: "auto" }], + 'prettier/prettier': ['error', { endOfLine: 'auto' }], + + // 문자열: 싱글 쿼트 + quotes: ['error', 'single', { avoidEscape: true }], + + // 세미콜론 필수 + semi: ['error', 'always'], + + // 네이밍 규칙 + '@typescript-eslint/naming-convention': [ + 'error', + { + // 변수: camelCase, 상수: UPPER_CASE + selector: 'variable', + format: ['camelCase', 'UPPER_CASE'], + leadingUnderscore: 'allow', // _ 허용 (ex. private 멤버) + }, + { + // 함수: camelCase + selector: 'function', + format: ['camelCase'], + }, + { + // 클래스: PascalCase + selector: 'class', + format: ['PascalCase'], + }, + ], + + '@typescript-eslint/no-unused-vars': 'off', }, }, ); diff --git a/BE/package-lock.json b/BE/package-lock.json index 4c14bf7..c52bf08 100644 --- a/BE/package-lock.json +++ b/BE/package-lock.json @@ -9,12 +9,15 @@ "version": "0.0.1", "license": "UNLICENSED", "dependencies": { + "@nestjs/axios": "^4.0.1", "@nestjs/common": "^11.0.1", "@nestjs/config": "^4.0.2", + "@nestjs/config": "^4.0.2", "@nestjs/core": "^11.0.1", "@nestjs/platform-express": "^11.0.1", "@prisma/adapter-mariadb": "^7.1.0", "@prisma/client": "^7.1.0", + "axios": "^1.13.2", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1" }, @@ -2179,6 +2182,17 @@ "@tybys/wasm-util": "^0.10.0" } }, + "node_modules/@nestjs/axios": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@nestjs/axios/-/axios-4.0.1.tgz", + "integrity": "sha512-68pFJgu+/AZbWkGu65Z3r55bTsCPlgyKaV4BSG8yUAD72q1PPuyVRgUwFv6BxdnibTUHlyxm06FmYWNC+bjN7A==", + "license": "MIT", + "peerDependencies": { + "@nestjs/common": "^10.0.0 || ^11.0.0", + "axios": "^1.3.1", + "rxjs": "^7.0.0" + } + }, "node_modules/@nestjs/cli": { "version": "11.0.14", "resolved": "https://registry.npmjs.org/@nestjs/cli/-/cli-11.0.14.tgz", @@ -2271,6 +2285,21 @@ "rxjs": "^7.1.0" } }, + "node_modules/@nestjs/config": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-4.0.2.tgz", + "integrity": "sha512-McMW6EXtpc8+CwTUwFdg6h7dYcBUpH5iUILCclAsa+MbCEvC9ZKu4dCHRlJqALuhjLw97pbQu62l4+wRwGeZqA==", + "license": "MIT", + "dependencies": { + "dotenv": "16.4.7", + "dotenv-expand": "12.0.1", + "lodash": "4.17.21" + }, + "peerDependencies": { + "@nestjs/common": "^10.0.0 || ^11.0.0", + "rxjs": "^7.1.0" + } + }, "node_modules/@nestjs/config/node_modules/dotenv": { "version": "16.4.7", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz", @@ -4082,7 +4111,6 @@ "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "dev": true, "license": "MIT" }, "node_modules/aws-ssl-profiles": { @@ -4095,6 +4123,18 @@ "node": ">= 6.0.0" } }, + "node_modules/axios": { + "version": "1.13.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.13.2.tgz", + "integrity": "sha512-VPk9ebNqPcy5lRGuSlKx752IlDatOjT9paPlm8A7yOuW2Fbvp4X3JznJtT4f0GzGLLiWE9W8onz51SqLYwzGaA==", + "license": "MIT", + "peer": true, + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.4", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/babel-jest": { "version": "30.2.0", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-30.2.0.tgz", @@ -4759,7 +4799,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "license": "MIT", "dependencies": { "delayed-stream": "~1.0.0" @@ -5054,7 +5093,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "dev": true, "license": "MIT", "engines": { "node": ">=0.4.0" @@ -5156,6 +5194,33 @@ "url": "https://dotenvx.com" } }, + "node_modules/dotenv": { + "version": "16.4.7", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz", + "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/dotenv-expand": { + "version": "12.0.1", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-12.0.1.tgz", + "integrity": "sha512-LaKRbou8gt0RNID/9RoI+J2rvXsBRPMV7p+ElHlPhcSARbCPDYcYG2s1TIzAfWv4YSgyY5taidWzzs31lNV3yQ==", + "license": "BSD-2-Clause", + "dependencies": { + "dotenv": "^16.4.5" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/dunder-proto": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", @@ -5305,7 +5370,6 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", - "dev": true, "license": "MIT", "dependencies": { "es-errors": "^1.3.0", @@ -5918,6 +5982,26 @@ "dev": true, "license": "ISC" }, + "node_modules/follow-redirects": { + "version": "1.15.11", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.11.tgz", + "integrity": "sha512-deG2P0JfjrTxl50XGCDyfI97ZGVCxIpfKYmfyrQ54n5FO/0gfIES8C/Psl6kWVDolizcaaxZJnTS0QSMxvnsBQ==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/foreground-child": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-3.3.1.tgz", @@ -5967,7 +6051,6 @@ "version": "4.0.5", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.5.tgz", "integrity": "sha512-8RipRLol37bNs2bhoV67fiTEvdTrbMUYcFTiy3+wuuOnUog2QBHCZWXDRijWQfAkhBj2Uf5UnVaiWwA5vdd82w==", - "dev": true, "license": "MIT", "dependencies": { "asynckit": "^0.4.0", @@ -5984,7 +6067,6 @@ "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "dev": true, "license": "MIT", "engines": { "node": ">= 0.6" @@ -5994,7 +6076,6 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dev": true, "license": "MIT", "dependencies": { "mime-db": "1.52.0" @@ -6358,7 +6439,6 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", - "dev": true, "license": "MIT", "dependencies": { "has-symbols": "^1.0.3" @@ -8847,6 +8927,12 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "license": "MIT" + }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", diff --git a/BE/package.json b/BE/package.json index 4b7a1b8..22ba494 100644 --- a/BE/package.json +++ b/BE/package.json @@ -23,12 +23,14 @@ "prisma:studio": "prisma studio" }, "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", "@prisma/adapter-mariadb": "^7.1.0", "@prisma/client": "^7.1.0", + "axios": "^1.13.2", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1" }, diff --git a/BE/src/app.module.ts b/BE/src/app.module.ts index 69fd3dd..c114dc7 100644 --- a/BE/src/app.module.ts +++ b/BE/src/app.module.ts @@ -3,11 +3,16 @@ import { ConfigModule } from '@nestjs/config'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { PrismaModule } from './prisma/prisma.module'; +import { AuthModule } from './auth/auth.module'; @Module({ - imports: [PrismaModule, ConfigModule.forRoot({ - isGlobal: true, - })], + imports: [ + PrismaModule, + ConfigModule.forRoot({ + isGlobal: true, + }), + AuthModule, + ], controllers: [AppController], providers: [AppService], }) diff --git a/BE/src/auth/auth.controller.ts b/BE/src/auth/auth.controller.ts new file mode 100644 index 0000000..09c1b78 --- /dev/null +++ b/BE/src/auth/auth.controller.ts @@ -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); + } +} diff --git a/BE/src/auth/auth.module.ts b/BE/src/auth/auth.module.ts new file mode 100644 index 0000000..55af999 --- /dev/null +++ b/BE/src/auth/auth.module.ts @@ -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 {} diff --git a/BE/src/auth/auth.service.ts b/BE/src/auth/auth.service.ts new file mode 100644 index 0000000..3ac9111 --- /dev/null +++ b/BE/src/auth/auth.service.ts @@ -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, + }; + } +} diff --git a/BE/src/auth/cohort-rules.ts b/BE/src/auth/cohort-rules.ts new file mode 100644 index 0000000..8782c5f --- /dev/null +++ b/BE/src/auth/cohort-rules.ts @@ -0,0 +1,19 @@ +export const COHORT_ORG_MAP: Record = { + 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; +} diff --git a/BE/src/auth/github-oauth.client.ts b/BE/src/auth/github-oauth.client.ts new file mode 100644 index 0000000..6be64d9 --- /dev/null +++ b/BE/src/auth/github-oauth.client.ts @@ -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 { + // redirect_uri로 전달받은 일회성 인가 코드 -> Access Token 발급받아오기 + const res = await firstValueFrom( + this.http.post( + '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 { + const res = await firstValueFrom( + this.http.get>('https://api.github.com/user/orgs', { + headers: { + Authorization: `Bearer ${accessToken}`, + Accept: 'application/vnd.github+json', + }, + }), + ); + return res.data.map((o) => o.login); + } +}