-   Notifications  You must be signed in to change notification settings 
- Fork 0
[25.04.21 / TASK-148] Feature - qrcode app & QRCode API #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
   Merged  
     Merged  
 Changes from 11 commits
 Commits 
  Show all changes 
  33 commits   Select commit Hold shift + click to select a range 
 6d3b0ec  feat: qrcode app ์ปจํธ๋กค๋ฌ, ์๋น์ค, ๋ ํฌ์งํ ๋ฆฌ, ํ์
 ์ถ๊ฐ, ๊ด๋ จ API ๊ตฌํ 
  Jihyun3478 3b6adaa  hotfix: ์ฝ๋๋๋น ๋ฆฌ๋ทฐ ๋ฐ์ 
  Jihyun3478 b70a582  modify: ์ฌ์ฉํ์ง ์๋ ํด๋์ค ์ญ์  
  Jihyun3478 56c2283  refactor: ๋ถํ์ํ ์์กด์ฑ ์ญ์  
  Jihyun3478 ac39619  refactor: ๊ณต๋ฐฑ ์ ๊ฑฐ 
  Jihyun3478 d118b16  refactor: ํ์ฉ๋ ๋ฎ์ ํ๋ ์์ ์ ๋ฆฌ 
  Jihyun3478 f6f3936  modify: token ๊ณ ์  ๊ธธ์ด ๊ฐ ๋ช
์ & exception ์ฌ์ฌ์ฉ 
  Jihyun3478 065879c  refactor: repository ํ
์คํธ ์ฝ๋ ๋ฉ์๋ ๋ถ๋ฆฌ 
  Jihyun3478 3faf173  hotfix: uuid ์ญ์  
  Jihyun3478 a007870  hotfix: ๋น์ฆ๋์ค ๋ก์ง ํ๋ฆ์ ๋ง๊ฒ ์์  
  Jihyun3478 a95635e  refactor: ์ฝ๋๋๋น ๋ฆฌ๋ทฐ ๋ฐ์ 
  Jihyun3478 245a721  hotfix: token 36์๊ฐ ์๋ 10์๋ก ์์  
  Jihyun3478 4fd26ac  hotfix: ์ฟผ๋ฆฌ ์์ฑ ์ qr_login_tokens๊ฐ ์๋ users_qrlogintoken์ผ๋ก ์์  
  Jihyun3478 c7e9019  test: ๋ ํฌ์งํ ๋ฆฌ ํตํฉํ
์คํธ์ฝ๋ ๊ตฌํ 
  Jihyun3478 51a0162  Merge branch 'main' into feature/qrcode-app 
  Jihyun3478 5932dac  hotfix: ์ค๋ณต ํ ํฐ ํ
์คํธ ํ DB ์ฐ๊ฒฐ ์ข
๋ฃ 
  Jihyun3478 85b502c  hotfix: ์ค๋ณต ํ ํฐ ์ฝ์
 ํ
์คํธ ์ญ์  
  Jihyun3478 37b9633  modify: QRLoginToken ๋ผ์ฐํฐ, ์๋น์ค, ๋ ํฌ User์ชฝ์ผ๋ก ํฉ์น๊ธฐ 
  Jihyun3478 3440fa2  modify: ์ฝ๋๋๋น ๋ฆฌ๋ทฐ 1์ฐจ ๋ฐ์ 
  Jihyun3478 d2d127a  hotfix: process.env ๋์  ์์์ ๋์๋ฅผ ์ฌ์ฉํ๋๋ก ์์  
  Jihyun3478 fe68599  refactor: lint ์ ์ฉ 
  Jihyun3478 f8801e0  refactor: ๋ค์ฌ์ฐ๊ธฐ ์ ๋ฆฌ 
  Jihyun3478 5613dbb  hotfix: ์ฌ๋๋ mockingํ๋๋ก ์์  
  Jihyun3478 31b1a43  hotfix: ํน์  ํ
์คํธ ๋ฐ์ดํฐ ํ
์คํธ๊ฐ ์๋ฃ๋ ํ ์ง์ฐ๋๋ก ์์  
  Jihyun3478 6d6f918  docs: Swagger ์ฃผ์ ์์  ๋ฐ ์ถ๊ฐ 
  Jihyun3478 3ce8994  hotfix: ํ ํฐ ์์ฑ ๋ก์ง ์์  
  Jihyun3478 5c8f0a9  hotfix: ์ค์  ํด๋ผ์ด์ธํธ์ IP์ ์ ๊ทผํ๋๋ก ์์  & logger ๊ตฌ์ฒดํ 
  Jihyun3478 0769497  refactor: ์ฝ๋๋๋น ๋ฆฌ๋ทฐ ๋ฐ์ 
  Jihyun3478 624be89  docs: swagger ๋ฌธ์ ์์  
  Jihyun3478 c20e48e  modify: service์ getByToken์ด ์๋ repo์ findQRLoginToken์ ์ฌ์ฉํ๋๋ก ์์  
  Jihyun3478 b08d834  refactor: findByVelogUUID & getDecryptedTokens ๋ฉ์๋ ๋ณํฉ 
  Jihyun3478 21764ee  refactor: ๋ ํฌ ๊ณ์ธต๊ณผ์ ์ค๋ณต ํ
์คํธ์ฝ๋ ์ ๊ฑฐ 
  Jihyun3478 c2dd34a  refactor: ์ฌ์ฉํ์ง ์๋ import & ์ฝ๋ ์ ๊ฑฐ 
  Jihyun3478 File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
There are no files selected for viewing
   This file contains hidden or 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,95 @@ | ||
| import { NextFunction, Request, RequestHandler, Response } from 'express'; | ||
| import logger from '@/configs/logger.config'; | ||
| import { QRLoginTokenService } from "@/services/qr.service"; | ||
| import { QRLoginTokenResponseDto } from "@/types/dto/responses/qrResponse.type"; | ||
| import { InvalidTokenError, TokenExpiredError } from '@/exception/token.exception'; | ||
| import { UserService } from '@/services/user.service'; | ||
| import { NotFoundError } from '@/exception'; | ||
| import { CookieOptions } from 'express'; | ||
|  | ||
| type Token32 = string & { __lengthBrand: 32 }; | ||
|  | ||
| export class QRLoginController { | ||
| constructor( | ||
| private qrService: QRLoginTokenService, | ||
| private userService: UserService | ||
| ) {} | ||
|  | ||
| private cookieOption(): CookieOptions { | ||
| const isProd = process.env.NODE_ENV === 'production'; | ||
|  | ||
| const baseOptions: CookieOptions = { | ||
| httpOnly: isProd, | ||
| secure: isProd, | ||
| }; | ||
|  | ||
| if (isProd) { | ||
| baseOptions.sameSite = 'lax'; | ||
| baseOptions.domain = "velog-dashboard.kro.kr"; | ||
| } else { | ||
| baseOptions.domain = 'localhost'; | ||
| } | ||
|  | ||
| return baseOptions; | ||
| } | ||
|  | ||
| createToken: RequestHandler = async ( | ||
| req: Request, | ||
| res: Response<QRLoginTokenResponseDto>, | ||
| next: NextFunction, | ||
| ) => { | ||
| try { | ||
| const user = req.user; | ||
| const ip = req.ip ?? ''; | ||
| const userAgent = req.headers['user-agent'] || ''; | ||
|  | ||
| const token = await this.qrService.create(user.id, ip, userAgent); | ||
| const typedToken = token as Token32; | ||
|  | ||
| const response = new QRLoginTokenResponseDto( | ||
| true, | ||
| 'QR ํ ํฐ ์์ฑ ์๋ฃ', | ||
| { token: typedToken }, | ||
| null | ||
| ); | ||
| res.status(200).json(response); | ||
| } catch (error) { | ||
| logger.error('QR ํ ํฐ ์์ฑ ์คํจ:', error); | ||
| next(error); | ||
| } | ||
| }; | ||
|  | ||
| getToken: RequestHandler = async (req: Request, res: Response, next: NextFunction) => { | ||
| try { | ||
| const token = req.query.token as string; | ||
| if (!token) { | ||
| throw new InvalidTokenError('ํ ํฐ์ด ํ์ํฉ๋๋ค.'); | ||
| } | ||
|  | ||
| const found = await this.qrService.useToken(token); | ||
| if (!found) { | ||
| throw new TokenExpiredError(); | ||
| } | ||
|  | ||
| const user = await this.userService.findByVelogUUID(found.user.toString()); | ||
| if (!user) throw new NotFoundError('์ ์ ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.'); | ||
|  | ||
| const { decryptedAccessToken, decryptedRefreshToken } = this.userService.getDecryptedTokens( | ||
| user.group_id, | ||
| user.access_token, | ||
| user.refresh_token | ||
| ); | ||
|  | ||
| res.clearCookie('access_token', this.cookieOption()); | ||
| res.clearCookie('refresh_token', this.cookieOption()); | ||
|  | ||
| res.cookie('access_token', decryptedAccessToken, this.cookieOption()); | ||
| res.cookie('refresh_token', decryptedRefreshToken, this.cookieOption()); | ||
|  | ||
| res.redirect('/main'); | ||
| } catch (error) { | ||
| logger.error('QR ํ ํฐ ๋ก๊ทธ์ธ ์ฒ๋ฆฌ ์คํจ', error); | ||
| next(error); | ||
| } | ||
| }; | ||
| } | ||
|   Jihyun3478 marked this conversation as resolved. Show resolved Hide resolved | 
   This file contains hidden or 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,80 @@ | ||
| import { QRLoginTokenRepository } from '@/repositories/qr.repository'; | ||
| import { DBError } from '@/exception'; | ||
| import { Pool } from 'pg'; | ||
|  | ||
| const mockPool: Partial<Pool> = { | ||
| query: jest.fn(), | ||
| }; | ||
|  | ||
| describe('QRLoginTokenRepository', () => { | ||
| let repo: QRLoginTokenRepository; | ||
|  | ||
| beforeEach(() => { | ||
| repo = new QRLoginTokenRepository(mockPool as Pool); | ||
| }); | ||
|  | ||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|  | ||
| describe('createQRLoginToken', () => { | ||
| it('QR ํ ํฐ์ ์ฑ๊ณต์ ์ผ๋ก ์ฝ์ ํด์ผ ํ๋ค', async () => { | ||
| (mockPool.query as jest.Mock).mockResolvedValueOnce(undefined); | ||
|  | ||
| await expect( | ||
| repo.createQRLoginToken('token', 1, 'ip', 'agent') | ||
| ).resolves.not.toThrow(); | ||
|  | ||
| expect(mockPool.query).toHaveBeenCalled(); | ||
| }); | ||
|  | ||
| it('์ฝ์ ์ค ์ค๋ฅ ๋ฐ์ ์ DBError๋ฅผ ๋์ ธ์ผ ํ๋ค', async () => { | ||
| (mockPool.query as jest.Mock).mockRejectedValueOnce(new Error('fail')); | ||
|  | ||
| await expect( | ||
| repo.createQRLoginToken('token', 1, 'ip', 'agent') | ||
| ).rejects.toThrow(DBError); | ||
| }); | ||
| }); | ||
|  | ||
| describe('findQRLoginToken', () => { | ||
| it('ํ ํฐ์ด ์กด์ฌํ ๊ฒฝ์ฐ ๋ฐํํด์ผ ํ๋ค', async () => { | ||
| const mockTokenData = { token: 'token', user: 1 }; | ||
| (mockPool.query as jest.Mock).mockResolvedValueOnce({ rows: [mockTokenData] }); | ||
|  | ||
| const result = await repo.findQRLoginToken('token'); | ||
| expect(result).toEqual(mockTokenData); | ||
| }); | ||
|  | ||
| it('ํ ํฐ์ด ์กด์ฌํ์ง ์์ผ๋ฉด null์ ๋ฐํํด์ผ ํ๋ค', async () => { | ||
| (mockPool.query as jest.Mock).mockResolvedValueOnce({ rows: [] }); | ||
|  | ||
| const result = await repo.findQRLoginToken('token'); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|  | ||
| it('์กฐํ ์ค ์ค๋ฅ ๋ฐ์ ์ DBError๋ฅผ ๋์ ธ์ผ ํ๋ค', async () => { | ||
| (mockPool.query as jest.Mock).mockRejectedValueOnce(new Error('fail')); | ||
|  | ||
| await expect(repo.findQRLoginToken('token')).rejects.toThrow(DBError); | ||
| }); | ||
| }); | ||
|  | ||
| describe('markTokenUsed', () => { | ||
| it('ํ ํฐ์ ์ฌ์ฉ ์ฒ๋ฆฌํด์ผ ํ๋ค', async () => { | ||
| (mockPool.query as jest.Mock).mockResolvedValueOnce(undefined); | ||
|  | ||
| await expect(repo.markTokenUsed('token')).resolves.not.toThrow(); | ||
| expect(mockPool.query).toHaveBeenCalledWith( | ||
| expect.stringContaining('UPDATE qr_login_tokens SET is_used = true'), | ||
| ['token'] | ||
| ); | ||
| }); | ||
|  | ||
| it('ํ ํฐ ์ฌ์ฉ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์ ์ DBError๋ฅผ ๋์ ธ์ผ ํ๋ค', async () => { | ||
| (mockPool.query as jest.Mock).mockRejectedValueOnce(new Error('fail')); | ||
|  | ||
| await expect(repo.markTokenUsed('token')).rejects.toThrow(DBError); | ||
| }); | ||
| }); | ||
| }); | 
   This file contains hidden or 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,48 @@ | ||
| import { Pool } from 'pg'; | ||
| import logger from '@/configs/logger.config'; | ||
| import { DBError } from '@/exception'; | ||
| import { QRLoginToken } from '@/types/models/QRLoginToken.type'; | ||
|  | ||
| export class QRLoginTokenRepository { | ||
| constructor(private pool: Pool) { } | ||
|  | ||
| async createQRLoginToken(token: string, userId: number, ip: string, userAgent: string): Promise<void> { | ||
| try { | ||
| const query = ` | ||
| INSERT INTO qr_login_tokens (token, user_id, created_at, expires_at, is_used, ip_address, user_agent) | ||
| VALUES ($1, $2, NOW(), NOW() + INTERVAL '5 minutes', false, $3, $4); | ||
| `; | ||
| await this.pool.query(query, [token, userId, ip, userAgent]); | ||
| } catch (error) { | ||
| logger.error('QRLoginToken Repo Create Error : ', error); | ||
| throw new DBError('QR ์ฝ๋ ํ ํฐ ์์ฑ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.'); | ||
| } | ||
| } | ||
|  | ||
| async findQRLoginToken(token: string): Promise<QRLoginToken | null> { | ||
| try { | ||
| const query = ` | ||
| SELECT * | ||
| FROM qr_login_tokens | ||
| WHERE token = $1 AND is_used = false AND expires_at > NOW(); | ||
| `; | ||
| const result = await this.pool.query(query, [token]); | ||
| return result.rows[0] ?? null; | ||
| } catch (error) { | ||
| logger.error('QRLoginToken Repo find QR Code Error : ', error); | ||
| throw new DBError('QR ์ฝ๋ ํ ํฐ ์กฐํ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.'); | ||
| } | ||
| } | ||
|  | ||
| async markTokenUsed(token: string): Promise<void> { | ||
| try { | ||
| const query = ` | ||
| UPDATE qr_login_tokens SET is_used = true WHERE token = $1; | ||
| `; | ||
| await this.pool.query(query, [token]); | ||
| } catch (error) { | ||
| logger.error('QRLoginToken Repo mark as used Error : ', error); | ||
| throw new DBError('QR ์ฝ๋ ์ฌ์ฉ ์ฒ๋ฆฌ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.'); | ||
| } | ||
| } | ||
| } | 
   This file contains hidden or 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 hidden or 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,56 @@ | ||
| import express, { Router } from 'express'; | ||
| import pool from '@/configs/db.config'; | ||
|  | ||
| import { authMiddleware } from '@/middlewares/auth.middleware'; | ||
| import { QRLoginTokenRepository } from '@/repositories/qr.repository'; | ||
| import { QRLoginTokenService } from '@/services/qr.service'; | ||
| import { QRLoginController } from '@/controllers/qr.controller'; | ||
| import { UserRepository } from '@/repositories/user.repository'; | ||
| import { UserService } from '@/services/user.service'; | ||
|  | ||
| const router: Router = express.Router(); | ||
|  | ||
| const qrRepository = new QRLoginTokenRepository(pool); | ||
| const userRepository = new UserRepository(pool); | ||
| const userService = new UserService(userRepository); | ||
| const qrService = new QRLoginTokenService(qrRepository); | ||
| const qrController = new QRLoginController(qrService, userService); | ||
|  | ||
| /** | ||
| * @swagger | ||
| * /api/qr-login: | ||
| * post: | ||
| * summary: QR ๋ก๊ทธ์ธ ํ ํฐ ์์ฑ | ||
| * tags: [QRLogin] | ||
| * security: | ||
| * - bearerAuth: [] | ||
| * responses: | ||
| * 200: | ||
| * description: QR ๋ก๊ทธ์ธ ํ ํฐ ์์ฑ ์ฑ๊ณต | ||
| */ | ||
| router.post('/qr-login', authMiddleware.login, qrController.createToken); | ||
|  | ||
| /** | ||
| * @swagger | ||
| * /api/qr-login: | ||
| * get: | ||
| * summary: QR ๋ก๊ทธ์ธ ํ ํฐ ์กฐํ ๋ฐ ์๋ ๋ก๊ทธ์ธ ์ฒ๋ฆฌ | ||
| * tags: [QRLogin] | ||
| * parameters: | ||
| * - in: query | ||
| * name: token | ||
| * required: true | ||
| * schema: | ||
| * type: string | ||
| * description: ์กฐํํ QR ํ ํฐ | ||
| * responses: | ||
| * 302: | ||
| * description: ์๋ ๋ก๊ทธ์ธ ์๋ฃ ํ ๋ฉ์ธ ํ์ด์ง๋ก ๋ฆฌ๋๋ ์  | ||
| * 400: | ||
| * description: ์๋ชป๋ ํ ํฐ | ||
| * 404: | ||
| * description: ๋ง๋ฃ ๋๋ ์กด์ฌํ์ง ์๋ ํ ํฐ | ||
| */ | ||
| router.get('/qr-login', qrController.getToken); | ||
|  | ||
| export default router; | 
  Oops, something went wrong.  
  Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
Uh oh!
There was an error while loading. Please reload this page.