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
|
'use server';
import { eq, and } from 'drizzle-orm';
import db from '@/db/db';
import { users, mfaTokens } from '@/db/schema';
import { isEmailWhitelisted } from '@/lib/email-whitelist/service';
import { SessionRepository } from '@/lib/users/session/repository';
import { sendEmail } from '@/lib/mail/sendEmail';
/**
* 이메일 화이트리스트 확인 및 인증 시작
* 화이트리스트면 바로 Email MFA 시작, 아니면 패스워드 필요
*/
export async function checkEmailAndStartAuth(email: string): Promise<{
success: boolean;
isWhitelisted: boolean;
requiresPassword: boolean;
userId?: number;
userName?: string;
tempAuthKey?: string;
error?: string;
errorCode?: string;
}> {
try {
if (!email) {
return {
success: false,
isWhitelisted: false,
requiresPassword: false,
error: '이메일을 입력해주세요.',
errorCode: 'INVALID_INPUT'
};
}
const normalizedEmail = email.toLowerCase().trim();
// 1. 사용자 존재 확인
const [user] = await db
.select({
id: users.id,
name: users.name,
email: users.email,
isActive: users.isActive,
isLocked: users.isLocked,
lockoutUntil: users.lockoutUntil,
})
.from(users)
.where(eq(users.email, normalizedEmail))
.limit(1);
if (!user) {
return {
success: false,
isWhitelisted: false,
requiresPassword: false,
error: '등록되지 않은 이메일입니다.',
errorCode: 'USER_NOT_FOUND'
};
}
// 2. 계정 상태 확인
if (!user.isActive) {
return {
success: false,
isWhitelisted: false,
requiresPassword: false,
error: '비활성화된 계정입니다.',
errorCode: 'ACCOUNT_DEACTIVATED'
};
}
if (user.isLocked) {
const now = new Date();
if (user.lockoutUntil && user.lockoutUntil > now) {
const remainingMinutes = Math.ceil((user.lockoutUntil.getTime() - now.getTime()) / 60000);
return {
success: false,
isWhitelisted: false,
requiresPassword: false,
error: `계정이 잠겼습니다. ${remainingMinutes}분 후 다시 시도해주세요.`,
errorCode: 'ACCOUNT_LOCKED'
};
}
}
// 3. 이메일 화이트리스트 확인
const whitelisted = await isEmailWhitelisted(normalizedEmail);
if (whitelisted) {
// 화이트리스트 이메일: 바로 Email MFA 시작
const expiresAt = new Date();
expiresAt.setMinutes(expiresAt.getMinutes() + 10); // 10분 유효
// 임시 인증 세션 생성 (tempAuthSessions 테이블 사용)
const tempAuthKey = await SessionRepository.createTempAuthSession({
userId: user.id,
email: user.email,
authMethod: 'email',
expiresAt,
});
// Email OTP 생성 및 전송
const otpCode = Math.floor(100000 + Math.random() * 900000).toString();
const otpExpiresAt = new Date();
otpExpiresAt.setMinutes(otpExpiresAt.getMinutes() + 5); // 5분 유효
// 기존 OTP 비활성화
await db
.update(mfaTokens)
.set({ isActive: false })
.where(
and(
eq(mfaTokens.userId, user.id),
eq(mfaTokens.type, 'email_otp'),
eq(mfaTokens.isActive, true)
)
);
// 새 OTP 생성
await db.insert(mfaTokens).values({
userId: user.id,
token: otpCode,
type: 'email_otp',
expiresAt: otpExpiresAt,
isActive: true,
});
const userLanguage = user.language || 'en';
// OTP 이메일 전송
await sendEmail({
to: user.email,
subject: '로그인 인증번호',
template: 'otp',
context: {
language: userLanguage,
name: user.name,
otp: otpCode,
verificationUrl: '',
location: '',
},
});
return {
success: true,
isWhitelisted: true,
requiresPassword: false,
userId: user.id,
userName: user.name,
tempAuthKey,
};
} else {
// 일반 이메일: 패스워드 입력 필요
return {
success: true,
isWhitelisted: false,
requiresPassword: true,
userId: user.id,
userName: user.name,
};
}
} catch (error) {
console.error('Email auth check error:', error);
return {
success: false,
isWhitelisted: false,
requiresPassword: false,
error: '인증 처리 중 오류가 발생했습니다.',
errorCode: 'SYSTEM_ERROR'
};
}
}
/**
* Email OTP 재전송
*/
export async function resendEmailOtp(userId: number, email: string, userName: string): Promise<{
success: boolean;
error?: string;
}> {
try {
// OTP 생성
const otpCode = Math.floor(100000 + Math.random() * 900000).toString();
const otpExpiresAt = new Date();
otpExpiresAt.setMinutes(otpExpiresAt.getMinutes() + 5);
// 기존 OTP 비활성화
await db
.update(mfaTokens)
.set({ isActive: false })
.where(
and(
eq(mfaTokens.userId, userId),
eq(mfaTokens.type, 'email_otp'),
eq(mfaTokens.isActive, true)
)
);
// 새 OTP 생성
await db.insert(mfaTokens).values({
userId,
token: otpCode,
type: 'email_otp',
expiresAt: otpExpiresAt,
isActive: true,
});
// OTP 이메일 전송
await sendEmail({
to: email,
subject: '로그인 인증번호',
template: 'otp',
context: {
language: 'ko',
name: userName,
otp: otpCode,
verificationUrl: '',
location: '',
},
});
return { success: true };
} catch (error) {
console.error('Email OTP resend error:', error);
return {
success: false,
error: '이메일 전송에 실패했습니다.',
};
}
}
|