Files
sori.studio/server/utils/admin-auth.js

148 lines
3.5 KiB
JavaScript

import { createHmac, timingSafeEqual } from 'node:crypto'
import { createError, deleteCookie, getCookie, setCookie } from 'h3'
const adminSessionCookieName = 'sori_admin_session'
const sessionMaxAge = 60 * 60 * 12
/**
* 세션 서명 비밀값 조회
* @returns {string} 세션 서명 비밀값
*/
const getSessionSecret = () => {
const config = useRuntimeConfig()
if (!config.adminPassword) {
throw createError({
statusCode: 500,
message: '관리자 비밀번호 환경 변수가 없습니다.'
})
}
return config.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 {string} email - 관리자 이메일
* @returns {string} 세션 토큰
*/
export const createAdminSessionToken = (email) => {
const payload = Buffer.from(JSON.stringify({
email,
expiresAt: Date.now() + sessionMaxAge * 1000
})).toString('base64url')
return `${payload}.${signPayload(payload)}`
}
/**
* 관리자 세션 토큰 검증
* @param {string | undefined} token - 세션 토큰
* @returns {{ email: string } | 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.email || !session.expiresAt || session.expiresAt < Date.now()) {
return null
}
return {
email: session.email
}
}
/**
* 관리자 세션 쿠키 설정
* @param {import('h3').H3Event} event - 요청 이벤트
* @param {string} email - 관리자 이메일
* @returns {void}
*/
export const setAdminSession = (event, email) => {
setCookie(event, adminSessionCookieName, createAdminSessionToken(email), {
httpOnly: true,
sameSite: 'lax',
secure: process.env.NODE_ENV === 'production',
path: '/admin',
maxAge: sessionMaxAge
})
}
/**
* 관리자 세션 쿠키 삭제
* @param {import('h3').H3Event} event - 요청 이벤트
* @returns {void}
*/
export const clearAdminSession = (event) => {
deleteCookie(event, adminSessionCookieName, {
path: '/admin'
})
}
/**
* 관리자 세션 조회
* @param {import('h3').H3Event} event - 요청 이벤트
* @returns {{ email: string } | null} 세션 정보
*/
export const getAdminSession = (event) => verifyAdminSessionToken(getCookie(event, adminSessionCookieName))
/**
* 관리자 세션 필수 확인
* @param {import('h3').H3Event} event - 요청 이벤트
* @returns {{ email: string }} 세션 정보
*/
export const requireAdminSession = (event) => {
const session = getAdminSession(event)
if (!session) {
throw createError({
statusCode: 401,
message: '관리자 로그인이 필요합니다.'
})
}
return session
}