Skip to content

Commit 3ce8994

Browse files
committed
hotfix: 토큰 생성 로직 수정
1 parent 6d6f918 commit 3ce8994

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/services/__test__/qr.service.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jest.mock('@/modules/slack/slack.notifier', () => ({
1717

1818
jest.mock('@/repositories/user.repository');
1919

20-
describe('UserService', () => {
20+
describe('UserService 의 QRService', () => {
2121
let service: UserService;
2222
let repo: jest.Mocked<UserRepository>;
2323

@@ -42,7 +42,7 @@ describe('UserService', () => {
4242

4343
expect(typeof token).toBe('string');
4444
expect(token.length).toBe(10);
45-
expect(/^[A-Za-z0-9]{10}$/.test(token)).toBe(true);
45+
expect(/^[A-Za-z0-9\-_.~!]{10}$/.test(token)).toBe(true);
4646
expect(repo.createQRLoginToken).toHaveBeenCalledWith(token, userId, ip, userAgent);
4747
});
4848

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import crypto from 'crypto';
22

3+
const CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~!';
4+
const CHARSET_LENGTH = CHARSET.length;
5+
36
export function generateRandomToken(length: number = 10): string {
4-
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
5-
let result = '';
6-
const randomBytes = crypto.randomBytes(length);
7-
for (let i = 0; i < length; i++) {
8-
result += chars.charAt(randomBytes[i] % chars.length);
9-
}
10-
return result;
11-
}
7+
const randomBytes = crypto.randomBytes(length);
8+
const result = new Array(length);
9+
10+
for (let i = 0; i < length; i++) {
11+
result[i] = CHARSET[randomBytes[i] % CHARSET_LENGTH];
12+
}
13+
14+
return result.join('');
15+
}

0 commit comments

Comments
 (0)