Skip to content
Merged
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
19 changes: 10 additions & 9 deletions src/common/utils/id-parser.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { BadRequestException } from '@nestjs/common';

export function parseId(raw: string): bigint {
const trimmed = raw.trim();
if (trimmed === '') {
throw new BadRequestException('Invalid id.');
}
let id: bigint;
try {
const trimmed = raw.trim();
if (trimmed === '') {
throw new Error('empty');
}
const id = BigInt(trimmed);
if (id < 0n) {
throw new Error('negative');
}
return id;
id = BigInt(trimmed);
} catch {
throw new BadRequestException('Invalid id.');
}
if (id < 0n) {
throw new BadRequestException('Invalid id.');
}
return id;
}
9 changes: 7 additions & 2 deletions src/features/auth/types/oidc-provider.type.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BadRequestException } from '@nestjs/common';

import { parseOidcProvider } from '@/features/auth/types/oidc-provider.type';

describe('parseOidcProvider', () => {
Expand All @@ -9,17 +11,20 @@ describe('parseOidcProvider', () => {
expect(parseOidcProvider('kakao')).toBe('kakao');
});

it('지원하지 않는 provider이면 에러를 던진다', () => {
it('지원하지 않는 provider이면 BadRequestException을 던진다', () => {
expect(() => parseOidcProvider('facebook')).toThrow(BadRequestException);
expect(() => parseOidcProvider('facebook')).toThrow(
'Unsupported OIDC provider: facebook',
);
});

it('빈 문자열이면 에러를 던진다', () => {
it('빈 문자열이면 BadRequestException을 던진다', () => {
expect(() => parseOidcProvider('')).toThrow(BadRequestException);
expect(() => parseOidcProvider('')).toThrow('Unsupported OIDC provider: ');
});

it('대소문자를 구분한다', () => {
expect(() => parseOidcProvider('Google')).toThrow(BadRequestException);
expect(() => parseOidcProvider('Google')).toThrow(
'Unsupported OIDC provider: Google',
);
Expand Down
8 changes: 7 additions & 1 deletion src/features/auth/types/oidc-provider.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BadRequestException } from '@nestjs/common';

/**
* 지원하는 OIDC Provider 타입
*/
Expand All @@ -6,10 +8,14 @@ export type OidcProvider = 'google' | 'kakao';
/**
* OIDC Provider 파라미터를 검증하고 타입으로 변환한다.
*
* 호출 컨텍스트: HTTP request path (auth.controller 콜백, oidc-login.service).
* 따라서 잘못된 값은 도메인 입력 오류로 분류해 4xx 로 반환한다.
*
* @param raw provider 문자열
* @returns provider 타입
* @throws BadRequestException 지원하지 않는 provider 인 경우
*/
export function parseOidcProvider(raw: string): OidcProvider {
if (raw === 'google' || raw === 'kakao') return raw;
throw new Error(`Unsupported OIDC provider: ${raw}`);
throw new BadRequestException(`Unsupported OIDC provider: ${raw}`);
}
25 changes: 13 additions & 12 deletions src/global/auth/parse-account-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import { BadRequestException } from '@nestjs/common';
import type { JwtUser } from '@/global/auth/types/jwt-payload.type';

export function parseAccountId(user: JwtUser): bigint {
const raw =
typeof user.accountId === 'string'
? user.accountId.trim()
: String(user.accountId ?? '');
if (raw === '') {
throw new BadRequestException('Invalid account id.');
}
let id: bigint;
try {
const raw =
typeof user.accountId === 'string'
? user.accountId.trim()
: String(user.accountId ?? '');
if (raw === '') {
throw new Error('empty');
}
const id = BigInt(raw);
if (id < 0n) {
throw new Error('negative');
}
return id;
id = BigInt(raw);
} catch {
throw new BadRequestException('Invalid account id.');
}
if (id < 0n) {
throw new BadRequestException('Invalid account id.');
}
return id;
}
Loading