feat: 카카오 계정의 본인 복구와 인증 후 연결 교체를 추가한다
This commit is contained in:
1 parent
d1cdf8a85f
commit
c81d304073
22 files changed
+1338
-199
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { TRPCError } from '@trpc/server';
|
||||
import type { UserRecord } from './userRepository.js';
|
||||
|
||||
export type KakaoLinkAuthorization = 'EMAIL' | 'PASSWORD' | 'ADMIN';
|
||||
export interface KakaoLinkSnapshot {
|
||||
authRevision: number;
|
||||
oauthType: UserRecord['oauthType'];
|
||||
oauthId?: string;
|
||||
email?: string;
|
||||
passwordFingerprint: string;
|
||||
}
|
||||
export const snapshotKakaoLink = (user: UserRecord): KakaoLinkSnapshot => ({
|
||||
authRevision: user.authRevision ?? 0,
|
||||
oauthType: user.oauthType,
|
||||
oauthId: user.oauthId,
|
||||
email: user.email,
|
||||
passwordFingerprint: createHash('sha256').update(user.passwordHash).digest('hex'),
|
||||
});
|
||||
export const matchesKakaoLinkSnapshot = (user: UserRecord, expected: KakaoLinkSnapshot): boolean =>
|
||||
(user.authRevision ?? 0) === expected.authRevision &&
|
||||
user.oauthType === expected.oauthType &&
|
||||
user.oauthId === expected.oauthId &&
|
||||
user.email === expected.email &&
|
||||
createHash('sha256').update(user.passwordHash).digest('hex') === expected.passwordFingerprint;
|
||||
|
||||
export const authorizeKakaoLink = (
|
||||
user: UserRecord,
|
||||
email: string,
|
||||
passwordVerified: boolean,
|
||||
firstUserId: string | undefined,
|
||||
now = new Date()
|
||||
): KakaoLinkAuthorization => {
|
||||
const privileged =
|
||||
user.id === firstUserId ||
|
||||
user.roles.some((role) => role === 'superuser' || role === 'admin' || role.startsWith('admin.'));
|
||||
const approved = Boolean(
|
||||
user.kakaoReplacementApprovedByUserId &&
|
||||
user.kakaoReplacementReason &&
|
||||
user.kakaoReplacementApprovedUntil &&
|
||||
new Date(user.kakaoReplacementApprovedUntil) > now
|
||||
);
|
||||
if (privileged && user.oauthType === 'KAKAO' && user.oauthId) {
|
||||
if (approved) return 'ADMIN';
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: '관리자 계정의 카카오 교체에는 관리자 승인이 필요합니다.',
|
||||
});
|
||||
}
|
||||
if (passwordVerified) return 'PASSWORD';
|
||||
if (privileged)
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: '관리자 계정의 최초 카카오 연결은 기존 비밀번호를 확인해야 합니다.',
|
||||
});
|
||||
if (!privileged && user.email?.toLowerCase() === email.toLowerCase()) return 'EMAIL';
|
||||
if (approved) return 'ADMIN';
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: '이메일이 다른 계정은 기존 비밀번호로 확인하거나 관리자에게 복구를 요청해 주세요.',
|
||||
});
|
||||
};
|
||||
Reference in new issue
Block a user