Files
sori.studio/server/utils/admin-auth.js
zenn de767095ba v1.10.88 품질·보안 개선과 미디어 카탈로그 database 전환을 반영한다.
세션·로그인 제한, ZIP/업로드 스트리밍, CSP 최소 강제와 Report-Only, Playwright 격리 검증을 추가하고 미디어 목록을 reconcile 후 database 모드로 고정한다.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-22 11:05:40 +09:00

158 lines
4.3 KiB
JavaScript

import { createHmac, timingSafeEqual } from 'node:crypto'
import { createError, deleteCookie, getCookie, setCookie } from 'h3'
import { getRuntimeEnvValue } from './runtime-env'
import { shouldUseSecureSessionCookie } from './session-cookie'
const adminSessionCookieName = 'sori_admin_session'
const sessionMaxAge = 60 * 60 * 12
/**
* 세션 서명 비밀값 조회
* @returns {string} 세션 서명 비밀값
*/
const getSessionSecret = () => {
const adminPassword = getRuntimeEnvValue('ADMIN_PASSWORD', 'adminPassword')
if (!adminPassword) {
throw createError({
statusCode: 500,
message: '관리자 비밀번호 환경 변수가 없습니다.'
})
}
return adminPassword
}
/**
* 문자열 안전 비교
* @param {string} left - 비교 문자열
* @param {string} right - 비교 대상 문자열
* @returns {boolean} 일치 여부
*/
export const safeCompare = (left, right) => {
const leftBuffer = Buffer.from(left)
const rightBuffer = Buffer.from(right)
if (leftBuffer.length !== rightBuffer.length) {
return false
}
return timingSafeEqual(leftBuffer, rightBuffer)
}
/**
* 세션 페이로드 서명
* @param {string} payload - 인코딩된 세션 페이로드
* @returns {string} 세션 서명
*/
const signPayload = (payload) => createHmac('sha256', getSessionSecret())
.update(payload)
.digest('base64url')
/**
* 관리자 세션 토큰 생성
* @param {{ userId: string, email: string, sessionVersion?: number }} adminUser - 관리자 사용자 정보
* @returns {string} 세션 토큰
*/
export const createAdminSessionToken = (adminUser) => {
const payload = Buffer.from(JSON.stringify({
userId: adminUser.userId,
email: adminUser.email,
role: 'admin',
sessionVersion: Number.isInteger(adminUser.sessionVersion) ? adminUser.sessionVersion : 1,
expiresAt: Date.now() + sessionMaxAge * 1000
})).toString('base64url')
return `${payload}.${signPayload(payload)}`
}
/**
* 관리자 세션 토큰 검증
* @param {string | undefined} token - 세션 토큰
* @returns {{ userId: string, email: string, role: 'admin', sessionVersion: number } | null} 세션 정보
*/
export const verifyAdminSessionToken = (token) => {
if (!token) {
return null
}
const [payload, signature] = token.split('.')
if (!payload || !signature || !safeCompare(signature, signPayload(payload))) {
return null
}
let session = null
try {
session = JSON.parse(Buffer.from(payload, 'base64url').toString('utf8'))
} catch {
return null
}
if (!session.userId || !session.email || session.role !== 'admin' || !session.expiresAt || session.expiresAt < Date.now()) {
return null
}
return {
userId: session.userId,
email: session.email,
role: 'admin',
sessionVersion: Number.isInteger(session.sessionVersion) && session.sessionVersion > 0
? session.sessionVersion
: 1
}
}
/**
* 관리자 세션 쿠키 설정
* @param {import('h3').H3Event} event - 요청 이벤트
* @param {{ userId: string, email: string, sessionVersion?: number }} adminUser - 관리자 사용자 정보
* @returns {void}
*/
export const setAdminSession = (event, adminUser) => {
setCookie(event, adminSessionCookieName, createAdminSessionToken(adminUser), {
httpOnly: true,
sameSite: 'lax',
secure: shouldUseSecureSessionCookie(event),
path: '/',
maxAge: sessionMaxAge
})
}
/**
* 관리자 세션 쿠키 삭제
* @param {import('h3').H3Event} event - 요청 이벤트
* @returns {void}
*/
export const clearAdminSession = (event) => {
deleteCookie(event, adminSessionCookieName, {
path: '/'
})
}
/**
* 관리자 세션 조회
* @param {import('h3').H3Event} event - 요청 이벤트
* @returns {{ userId: string, email: string, role: 'admin', sessionVersion: number } | null} 세션 정보
*/
export const getAdminSession = (event) => verifyAdminSessionToken(getCookie(event, adminSessionCookieName))
/**
* 관리자 세션 필수 확인
* @param {import('h3').H3Event} event - 요청 이벤트
* @returns {{ userId: string, email: string, role: 'admin', sessionVersion: number }} 세션 정보
*/
export const requireAdminSession = (event) => {
const session = getAdminSession(event)
if (!session) {
throw createError({
statusCode: 401,
message: '관리자 로그인이 필요합니다.'
})
}
return session
}