summaryrefslogtreecommitdiff
path: root/lib/users/auth/passwordUtil.ts
blob: 046e7d9074dc152a6f7796301d57ab5a66f214e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// lib/auth/passwordUtils.ts

import bcrypt from 'bcryptjs';
import crypto from 'crypto';
import { eq, and, desc, count, sql, gte, inArray } from 'drizzle-orm';
import db from '@/db/db';
import { 
  users, 
  passwords, 
  passwordHistory, 
  securitySettings,
  mfaTokens 
} from '@/db/schema';

// libphonenumber-js import 추가
import {
  parsePhoneNumber,
  parsePhoneNumberFromString,
  isValidPhoneNumber
} from 'libphonenumber-js';

export interface PasswordStrength {
  score: number; // 1-5
  hasUppercase: boolean;
  hasLowercase: boolean;
  hasNumbers: boolean;
  hasSymbols: boolean;
  length: number;
  feedback: string[];
}

export interface PasswordPolicy {
  minLength: number;
  requireUppercase: boolean;
  requireLowercase: boolean;
  requireNumbers: boolean;
  requireSymbols: boolean;
  historyCount: number;
}

// 패스워드 강도 분석
export function analyzePasswordStrength(password: string): PasswordStrength {
  const hasUppercase = /[A-Z]/.test(password);
  const hasLowercase = /[a-z]/.test(password);
  const hasNumbers = /\d/.test(password);
  const hasSymbols = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password);
  const length = password.length;
  
  const feedback: string[] = [];
  let score = 1;
  
  // 길이 체크
  if (length >= 12) {
    score += 1;
  } else if (length < 8) {
    feedback.push('최소 8자 이상 사용하세요');
  }
  
  // 문자 종류 체크
  const typeCount = [hasUppercase, hasLowercase, hasNumbers, hasSymbols]
    .filter(Boolean).length;
  
  if (typeCount >= 4) {
    score += 2;
  } else if (typeCount >= 3) {
    score += 1;
  } else {
    if (!hasUppercase) feedback.push('대문자를 포함하세요');
    if (!hasLowercase) feedback.push('소문자를 포함하세요');
    if (!hasNumbers) feedback.push('숫자를 포함하세요');
    if (!hasSymbols) feedback.push('특수문자를 포함하세요');
  }
  
  // 일반적인 패턴 체크
  if (/(.)\1{2,}/.test(password)) {
    feedback.push('같은 문자가 3번 이상 반복되지 않도록 하세요');
    score = Math.max(1, score - 1);
  }
  
  if (/123|abc|qwe|password|admin/i.test(password)) {
    feedback.push('일반적인 패턴이나 단어는 피하세요');
    score = Math.max(1, score - 1);
  }
  
  return {
    score: Math.min(5, score),
    hasUppercase,
    hasLowercase,
    hasNumbers,
    hasSymbols,
    length,
    feedback,
  };
}

// 패스워드 정책 가져오기
export async function getPasswordPolicy(): Promise<PasswordPolicy> {
  const settings = await db.select().from(securitySettings).limit(1);
  const setting = settings[0];
  
  return {
    minLength: setting?.minPasswordLength || 8,
    requireUppercase: setting?.requireUppercase || true,
    requireLowercase: setting?.requireLowercase || true,
    requireNumbers: setting?.requireNumbers || true,
    requireSymbols: setting?.requireSymbols || true,
    historyCount: setting?.passwordHistoryCount || 5,
  };
}

// 패스워드 정책 검증
export async function validatePasswordPolicy(
  password: string,
  policy?: PasswordPolicy
): Promise<{ valid: boolean; errors: string[] }> {
  const passwordPolicy = policy || await getPasswordPolicy();
  const strength = analyzePasswordStrength(password);
  const errors: string[] = [];
  
  if (strength.length < passwordPolicy.minLength) {
    errors.push(`최소 ${passwordPolicy.minLength}자 이상이어야 합니다`);
  }
  
  if (passwordPolicy.requireUppercase && !strength.hasUppercase) {
    errors.push('대문자를 포함해야 합니다');
  }
  
  if (passwordPolicy.requireLowercase && !strength.hasLowercase) {
    errors.push('소문자를 포함해야 합니다');
  }
  
  if (passwordPolicy.requireNumbers && !strength.hasNumbers) {
    errors.push('숫자를 포함해야 합니다');
  }
  
  if (passwordPolicy.requireSymbols && !strength.hasSymbols) {
    errors.push('특수문자를 포함해야 합니다');
  }
  
  return {
    valid: errors.length === 0,
    errors,
  };
}

// 이전 패스워드와 중복 체크
export async function checkPasswordHistory(
  userId: number,
  newPassword: string,
  historyCount: number = 5
): Promise<boolean> {
  const histories = await db
    .select({
      passwordHash: passwordHistory.passwordHash,
      salt: passwordHistory.salt,
    })
    .from(passwordHistory)
    .where(eq(passwordHistory.userId, userId))
    .orderBy(desc(passwordHistory.createdAt))
    .limit(historyCount);
  
  // 현재 활성 패스워드도 체크
  const currentPassword = await db
    .select({
      passwordHash: passwords.passwordHash,
      salt: passwords.salt,
    })
    .from(passwords)
    .where(
      and(
        eq(passwords.userId, userId),
        eq(passwords.isActive, true)
      )
    )
    .limit(1);
  
  const allPasswords = [...histories];
  if (currentPassword[0]) {
    allPasswords.unshift(currentPassword[0]);
  }
  
  // 각 이전 패스워드와 비교
  for (const pwd of allPasswords) {
    const isMatch = await bcrypt.compare(
      newPassword + pwd.salt,
      pwd.passwordHash
    );
    if (isMatch) {
      return false; // 중복됨
    }
  }
  
  return true; // 중복 없음
}

// Salt 생성
function generateSalt(): string {
  return crypto.randomBytes(16).toString('hex');
}

// 패스워드 해싱
async function hashPassword(password: string, salt: string): Promise<string> {
  const saltRounds = 12;
  return bcrypt.hash(password + salt, saltRounds);
}

// 새 패스워드 설정
export async function setPassword(
  userId: number,
  newPassword: string
): Promise<{ success: boolean; errors: string[] }> {
  try {
    // 1. 정책 검증
    const policyResult = await validatePasswordPolicy(newPassword);
    if (!policyResult.valid) {
      return { success: false, errors: policyResult.errors };
    }
    
    // 2. 히스토리 검증
    const policy = await getPasswordPolicy();
    const isUnique = await checkPasswordHistory(userId, newPassword, policy.historyCount);
    if (!isUnique) {
      return { 
        success: false, 
        errors: [`최근 ${policy.historyCount}개 패스워드와 달라야 합니다`] 
      };
    }
    
    // 3. 현재 패스워드를 히스토리로 이동
    const currentPassword = await db
      .select()
      .from(passwords)
      .where(
        and(
          eq(passwords.userId, userId),
          eq(passwords.isActive, true)
        )
      )
      .limit(1);
    
    if (currentPassword[0]) {
      await db.insert(passwordHistory).values({
        userId,
        passwordHash: currentPassword[0].passwordHash,
        salt: currentPassword[0].salt,
        createdAt: currentPassword[0].createdAt,
        replacedAt: new Date(),
      });
      
      // 현재 패스워드 비활성화
      await db
        .update(passwords)
        .set({ isActive: false })
        .where(eq(passwords.id, currentPassword[0].id));
    }
    
    // 4. 새 패스워드 생성
    const salt = generateSalt();
    const hashedPassword = await hashPassword(newPassword, salt);
    const strength = analyzePasswordStrength(newPassword);
    
    // 패스워드 만료일 계산
    let expiresAt: Date | null = null;
    const settings = await db.select().from(securitySettings).limit(1);
    const expiryDays = settings[0]?.passwordExpiryDays;
    if (expiryDays) {
      expiresAt = new Date();
      expiresAt.setDate(expiresAt.getDate() + expiryDays);
    }
    
    await db.insert(passwords).values({
      userId,
      passwordHash: hashedPassword,
      salt,
      strength: strength.score,
      hasUppercase: strength.hasUppercase,
      hasLowercase: strength.hasLowercase,
      hasNumbers: strength.hasNumbers,
      hasSymbols: strength.hasSymbols,
      length: strength.length,
      expiresAt,
      isActive: true,
    });
    
    // 5. 패스워드 변경 필수 플래그 해제
    await db
      .update(users)
      .set({ passwordChangeRequired: false })
      .where(eq(users.id, userId));
    
    // 6. 오래된 히스토리 정리
    await cleanupPasswordHistory(userId, policy.historyCount);
    
    return { success: true, errors: [] };
    
  } catch (error) {
    console.error('Password setting error:', error);
    return { success: false, errors: ['패스워드 설정 중 오류가 발생했습니다'] };
  }
}

// 패스워드 히스토리 정리
async function cleanupPasswordHistory(userId: number, keepCount: number) {
  const histories = await db
    .select({ id: passwordHistory.id })
    .from(passwordHistory)
    .where(eq(passwordHistory.userId, userId))
    .orderBy(desc(passwordHistory.createdAt))
    .offset(keepCount);
  
  if (histories.length > 0) {
    const idsToDelete = histories.map(h => h.id);
    await db
      .delete(passwordHistory)
      .where(and(eq(passwordHistory.userId, userId), inArray(passwordHistory.id, idsToDelete)))
  }
}

// ========== SMS 관련 함수들 ==========

// 전화번호에서 국가 정보 추출 (libphonenumber-js 사용)
function extractCountryInfo(phoneNumber: string): { 
  countryCode: string; 
  nationalNumber: string; 
  country?: string;
} | null {
  try {
    // 앞뒤 공백 및 중간 공백 제거
    const cleanedPhone = phoneNumber.trim().replace(/\s+/g, '');
    let parsed;
    
    // E.164 형식인지 확인
    if (cleanedPhone.startsWith('+')) {
      parsed = parsePhoneNumber(cleanedPhone);
    } else {
      // 국가 코드가 없으면 한국으로 가정 (기본값)
      parsed = parsePhoneNumberFromString(cleanedPhone, 'KR');
    }
    
    if (!parsed || !isValidPhoneNumber(parsed.number)) {
      return null;
    }
    
    const countryCallingCode = parsed.countryCallingCode;
    let nationalNumber = parsed.nationalNumber;
    
    // 국가별 특별 처리
    switch (countryCallingCode) {
      case '82': // 한국
        // 한국 번호는 0으로 시작해야 함
        if (!nationalNumber.startsWith('0')) {
          nationalNumber = '0' + nationalNumber;
        }
        break;
      case '1': // 미국/캐나다
        // 이미 올바른 형식
        break;
      case '81': // 일본
        // 이미 올바른 형식
        break;
      case '86': // 중국
        // 이미 올바른 형식
        break;
    }
    
    return {
      countryCode: countryCallingCode,
      nationalNumber: nationalNumber.replace(/[-\s]/g, ''), // 하이픈과 공백 제거
      country: parsed.country
    };
  } catch (error) {
    console.error('Country info extraction error:', error);
    return null;
  }
}

// Bizppurio API 토큰 발급
async function getBizppurioToken(): Promise<string> {
  const account = process.env.BIZPPURIO_ACCOUNT;
  const password = process.env.BIZPPURIO_PASSWORD;
  
  if (!account || !password) {
    throw new Error('Bizppurio credentials not configured');
  }

  const credentials = Buffer.from(`${account}:${password}`).toString('base64');
  
  const response = await fetch('https://api.bizppurio.com/v1/token', {
    method: 'POST',
    headers: {
      'Authorization': `Basic ${credentials}`,
      'Content-Type': 'application/json; charset=utf-8'
    }
  });

  if (!response.ok) {
    throw new Error(`Token request failed: ${response.status}`);
  }

  const data = await response.json();
  return data.accesstoken;
}

// SMS 메시지 전송 (libphonenumber-js 사용)
async function sendSmsMessage(phoneNumber: string, message: string): Promise<boolean> {
  try {
    const accessToken = await getBizppurioToken();
    const account = process.env.BIZPPURIO_ACCOUNT;
    const fromNumber = process.env.BIZPPURIO_FROM_NUMBER;
    
    if (!account || !fromNumber) {
      throw new Error('Bizppurio configuration missing');
    }

    // libphonenumber-js를 사용하여 전화번호 파싱
    const countryInfo = extractCountryInfo(phoneNumber);
    
    if (!countryInfo) {
      throw new Error(`Invalid phone number format: ${phoneNumber}`);
    }

    console.log(`Sending SMS to ${phoneNumber}:`);
    console.log(`  Country Code: ${countryInfo.countryCode}`);
    console.log(`  National Number: ${countryInfo.nationalNumber}`);
    if (countryInfo.country) {
      console.log(`  Country: ${countryInfo.country}`);
    }

    const requestBody = {
      account: account,
      type: 'sms',
      from: fromNumber,
      to: countryInfo.nationalNumber,
      country: countryInfo.countryCode,
      content: {
        sms: {
          message: message
        }
      },
      refkey: `sms_${Date.now()}_${Math.random().toString(36).substring(7)}`
    };

    const response = await fetch('https://api.bizppurio.com/v3/message', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${accessToken}`,
        'Content-Type': 'application/json; charset=utf-8'
      },
      body: JSON.stringify(requestBody)
    });

    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(`SMS send failed: ${response.status} - ${errorText}`);
    }

    const result = await response.json();
    
    if (result.code === 1000) {
      console.log(`SMS sent successfully to ${phoneNumber}. MessageKey: ${result.messagekey}`);
      return true;
    } else {
      throw new Error(`SMS send failed: ${result.description} (Code: ${result.code})`);
    }
  } catch (error) {
    console.error('SMS send error:', error);
    return false;
  }
}

// SMS 템플릿 (기존 유지)
const SMS_TEMPLATES = {
  '82': '[인증번호] {token}',           // 한국
  '1': '[Verification Code] {token}',   // 미국
  '81': '[認証コード] {token}',         // 일본
  '86': '[验证码] {token}',             // 중국
  'default': '[Verification Code] {token}' // 기본값 (영어)
} as const;

// SMS 메시지 생성 (libphonenumber-js 사용)
function getSmsMessage(phoneNumber: string, token: string): string {
  try {
    const countryInfo = extractCountryInfo(phoneNumber);
    if (!countryInfo) {
      return SMS_TEMPLATES.default.replace('{token}', token);
    }
    
    const template = SMS_TEMPLATES[countryInfo.countryCode as keyof typeof SMS_TEMPLATES] || SMS_TEMPLATES.default;
    return template.replace('{token}', token);
  } catch (error) {
    return SMS_TEMPLATES.default.replace('{token}', token); // 에러 시 기본값
  }
}

// 전화번호 정규화 (저장용)
export function normalizePhoneNumber(phoneNumber: string, countryCode?: string): string | null {
  try {
    // 앞뒤 공백 및 중간 공백 제거
    const cleanedPhone = phoneNumber.trim().replace(/\s+/g, '');
    let parsed;
    
    if (countryCode) {
      // 국가 코드가 제공된 경우
      parsed = parsePhoneNumberFromString(cleanedPhone, countryCode);
    } else {
      // 국가 코드가 없는 경우 국제 형식으로 파싱 시도
      parsed = parsePhoneNumber(cleanedPhone);
    }
    
    if (!parsed || !isValidPhoneNumber(parsed.number)) {
      return null;
    }
    
    // 항상 국제 형식으로 반환 (예: +821012345678)
    return parsed.format('E.164');
  } catch (error) {
    console.error('Phone number normalization error:', error);
    return null;
  }
}

// SMS 토큰 생성 및 전송 (업데이트됨)
export async function generateAndSendSmsToken(
  userId: number,
  phoneNumber: string
): Promise<{ success: boolean; error?: string }> {
  try {
    // 앞뒤 공백 및 중간 공백 제거
    const cleanedPhone = phoneNumber.trim().replace(/\s+/g, '');
    
    // 전화번호 유효성 검사
    if (!isValidPhoneNumber(cleanedPhone)) {
      return { success: false, error: '유효하지 않은 전화번호입니다' };
    }
    
    // 1. 일일 SMS 한도 체크
    const settings = await db.select().from(securitySettings).limit(1);
    const maxSmsPerDay = settings[0]?.maxSmsAttemptsPerDay || 10;
    
    const today = new Date();
    today.setHours(0, 0, 0, 0);
    
    const todayCount = await db
      .select({ count: count() })
      .from(mfaTokens)
      .where(
        and(
          eq(mfaTokens.userId, userId),
          eq(mfaTokens.type, 'sms'),
          gte(mfaTokens.createdAt, today)
        )
      );
    
    if (todayCount[0]?.count >= maxSmsPerDay) {
      return { success: false, error: '일일 SMS 한도를 초과했습니다' };
    }
    
    // 2. 이전 SMS 토큰 비활성화
    await db
      .update(mfaTokens)
      .set({ isActive: false })
      .where(
        and(
          eq(mfaTokens.userId, userId),
          eq(mfaTokens.type, 'sms'),
          eq(mfaTokens.isActive, true)
        )
      );
    
    // 3. 새 토큰 생성
    const token = Math.random().toString().slice(2, 8).padStart(6, '0');
    const expiryMinutes = settings[0]?.smsTokenExpiryMinutes || 5;
    const expiresAt = new Date();
    expiresAt.setMinutes(expiresAt.getMinutes() + expiryMinutes);
    
    // 전화번호 정규화 (저장용)
    const normalizedPhone = normalizePhoneNumber(cleanedPhone);
    if (!normalizedPhone) {
      return { success: false, error: '전화번호 형식이 올바르지 않습니다' };
    }
    
    await db.insert(mfaTokens).values({
      userId,
      token,
      type: 'sms',
      phoneNumber: normalizedPhone, // 정규화된 번호로 저장
      expiresAt,
      isActive: true,
    });
    
    // 4. SMS 전송
    const message = getSmsMessage(normalizedPhone, token);
    const smsResult = await sendSmsMessage(normalizedPhone, message);
    
    if (!smsResult) {
      // SMS 전송 실패 시 토큰 비활성화
      await db
        .update(mfaTokens)
        .set({ isActive: false })
        .where(
          and(
            eq(mfaTokens.userId, userId),
            eq(mfaTokens.token, token)
          )
        );
      
      return { success: false, error: 'SMS 전송에 실패했습니다' };
    }
    
    console.log(`SMS 토큰을 ${normalizedPhone}로 전송했습니다`);
    return { success: true };
    
  } catch (error) {
    console.error('SMS token generation error:', error);
    return { success: false, error: 'SMS 전송 중 오류가 발생했습니다' };
  }
}

// SMS 토큰 검증
export async function verifySmsToken(
  userId: number,
  token: string
): Promise<{ success: boolean; error?: string }> {
  try {
    // 개발용: 000000 입력 시 무조건 통과
    if (token === '000000') {
      console.log(`${new Date().toISOString()} 개발용 우회 코드 '000000' 사용됨 - 사용자 ${userId} 인증 성공`);
      return { success: true };
    }

    const mfaToken = await db
      .select()
      .from(mfaTokens)
      .where(
        and(
          eq(mfaTokens.userId, userId),
          eq(mfaTokens.token, token),
          eq(mfaTokens.type, 'sms'),
          eq(mfaTokens.isActive, true)
        )
      )
      .limit(1);
    
    if (!mfaToken[0]) {
      return { success: false, error: '잘못된 인증번호입니다' };
    }
    
    // 만료 체크
    if (mfaToken[0].expiresAt < new Date()) {
      await db
        .update(mfaTokens)
        .set({ isActive: false })
        .where(eq(mfaTokens.id, mfaToken[0].id));
      
      return { success: false, error: '인증번호가 만료되었습니다' };
    }
    
    // 시도 횟수 증가
    const newAttempts = mfaToken[0].attempts + 1;
    if (newAttempts > 3) {
      await db
        .update(mfaTokens)
        .set({ isActive: false })
        .where(eq(mfaTokens.id, mfaToken[0].id));
      
      return { success: false, error: '시도 횟수를 초과했습니다' };
    }
    
    // 토큰 사용 처리
    await db
      .update(mfaTokens)
      .set({
        usedAt: new Date(),
        isActive: false,
        attempts: newAttempts,
      })
      .where(eq(mfaTokens.id, mfaToken[0].id));
    
    return { success: true };
    
  } catch (error) {
    console.error('SMS token verification error:', error);
    return { success: false, error: '인증 중 오류가 발생했습니다' };
  }
}

// Email OTP 생성 및 전송
export async function generateAndSendEmailToken(
  userId: number,
  email: string,
  userName: string
): Promise<{ success: boolean; error?: string }> {
  try {
    // 1. 보안 설정 가져오기
    const settings = await db.select().from(securitySettings).limit(1);
    const expiryMinutes = settings[0]?.smsTokenExpiryMinutes || 10; // Email OTP도 동일한 만료 시간 사용
    
    // 2. 일일 Email OTP 한도 체크 (30회)
    const maxEmailPerDay = 30;
    
    const today = new Date();
    today.setHours(0, 0, 0, 0);
    
    const todayCount = await db
      .select({ count: count() })
      .from(mfaTokens)
      .where(
        and(
          eq(mfaTokens.userId, userId),
          eq(mfaTokens.type, 'email'),
          gte(mfaTokens.createdAt, today)
        )
      );
    
    if (todayCount[0]?.count >= maxEmailPerDay) {
      return { success: false, error: '일일 이메일 인증 한도를 초과했습니다' };
    }
    
    // 3. 이전 Email OTP 토큰 비활성화
    await db
      .update(mfaTokens)
      .set({ isActive: false })
      .where(
        and(
          eq(mfaTokens.userId, userId),
          eq(mfaTokens.type, 'email'),
          eq(mfaTokens.isActive, true)
        )
      );
    
    // 4. 새 토큰 생성 (6자리 숫자)
    const token = Math.random().toString().slice(2, 8).padStart(6, '0');
    const expiresAt = new Date();
    expiresAt.setMinutes(expiresAt.getMinutes() + expiryMinutes);
    
    // 5. DB에 토큰 저장
    await db.insert(mfaTokens).values({
      userId,
      token,
      type: 'email',
      expiresAt,
      isActive: true,
      attempts: 0,
    });
    
    // 6. 이메일 전송
    const { sendEmail } = await import('@/lib/mail/sendEmail');
    
    await sendEmail({
      to: email,
      template: 'otp',
      context: {
        name: userName,
        otp: token,
        verificationUrl: '', // Email OTP는 URL 없이 코드만 입력
        location: '',
        language: 'ko',
      },
    });
    
    console.log(`Email OTP sent to user ${userId} (${email})`);
    return { success: true };
    
  } catch (error) {
    console.error('Email OTP generation/sending error:', error);
    return { success: false, error: '이메일 인증번호 전송 중 오류가 발생했습니다' };
  }
}

// Email OTP 검증
export async function verifyEmailToken(
  userId: number,
  token: string
): Promise<{ success: boolean; error?: string }> {
  try {

    const mfaToken = await db
      .select()
      .from(mfaTokens)
      .where(
        and(
          eq(mfaTokens.userId, userId),
          eq(mfaTokens.token, token),
          eq(mfaTokens.type, 'email'),
          eq(mfaTokens.isActive, true)
        )
      )
      .limit(1);
    
    if (!mfaToken[0]) {
      return { success: false, error: '잘못된 인증번호입니다' };
    }
    
    // 만료 체크
    if (mfaToken[0].expiresAt < new Date()) {
      await db
        .update(mfaTokens)
        .set({ isActive: false })
        .where(eq(mfaTokens.id, mfaToken[0].id));
      
      return { success: false, error: '인증번호가 만료되었습니다' };
    }
    
    // 시도 횟수 증가
    const newAttempts = mfaToken[0].attempts + 1;
    if (newAttempts > 3) {
      await db
        .update(mfaTokens)
        .set({ isActive: false })
        .where(eq(mfaTokens.id, mfaToken[0].id));
      
      return { success: false, error: '시도 횟수를 초과했습니다' };
    }
    
    // 토큰 사용 처리
    await db
      .update(mfaTokens)
      .set({
        usedAt: new Date(),
        isActive: false,
        attempts: newAttempts,
      })
      .where(eq(mfaTokens.id, mfaToken[0].id));
    
    return { success: true };
    
  } catch (error) {
    console.error('Email token verification error:', error);
    return { success: false, error: '인증 중 오류가 발생했습니다' };
  }
}

// 패스워드 강제 변경 필요 체크
export async function checkPasswordChangeRequired(userId: number): Promise<boolean> {
  const user = await db
    .select({ passwordChangeRequired: users.passwordChangeRequired })
    .from(users)
    .where(eq(users.id, userId))
    .limit(1);
  
  return user[0]?.passwordChangeRequired || false;
}