오류수정, 암호화 추가

This commit is contained in:
2025-06-09 22:45:55 +09:00
parent 912e6e1033
commit 7e1bbcede0
6 changed files with 248 additions and 36 deletions
+9 -4
View File
@@ -133,7 +133,7 @@ exports.uploadEventFile = (req, res) => {
// 업로드된 파일 파싱 및 미리보기 데이터 제공
exports.parseEventFile = async (req, res) => {
try {
const { filename, clubId } = req.body;
const { filename, clubId, sheetName } = req.body;
if (!filename) {
return res.status(400).json({ message: '파일명이 필요합니다.' });
@@ -196,7 +196,7 @@ exports.parseEventFile = async (req, res) => {
// 파일 형식에 따른 처리
if (fileExt === '.xlsx' || fileExt === '.xls') {
// Excel 파일 처리
parsedData = await parseExcelFile(filePath, memberMap);
parsedData = await parseExcelFile(filePath, memberMap, sheetName);
} else if (fileExt === '.csv') {
// CSV 파일 처리
parsedData = await parseCsvFile(filePath, memberMap);
@@ -227,9 +227,14 @@ exports.parseEventFile = async (req, res) => {
};
// Excel 파일 파싱 함수
async function parseExcelFile(filePath, memberMap) {
async function parseExcelFile(filePath, memberMap, requestedSheetName = null) {
const workbook = xlsx.readFile(filePath, { type: 'binary', cellDates: true, codepage: 949 });
const sheetName = workbook.SheetNames[0];
// 요청된 시트명이 있고 해당 시트가 존재하면 그것을 사용, 아니면 첫 번째 시트 사용
const sheetName = (requestedSheetName && workbook.SheetNames.includes(requestedSheetName))
? requestedSheetName
: workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
const jsonData = xlsx.utils.sheet_to_json(worksheet, { header: 1, defval: '' });
+17 -2
View File
@@ -2,6 +2,7 @@ const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
const User = require('./User');
const Club = require('./Club');
const { encrypt, decrypt } = require('../utils/encryption');
const Member = sequelize.define('Member', {
id: {
@@ -53,12 +54,26 @@ const Member = sequelize.define('Member', {
phone: {
type: DataTypes.STRING,
allowNull: true,
comment: '전화번호'
comment: '전화번호 (암호화됨)',
get() {
const rawValue = this.getDataValue('phone');
return rawValue ? decrypt(rawValue) : null;
},
set(value) {
this.setDataValue('phone', value ? encrypt(value) : null);
}
},
email: {
type: DataTypes.STRING,
allowNull: true,
comment: '이메일'
comment: '이메일 (암호화됨)',
get() {
const rawValue = this.getDataValue('email');
return rawValue ? decrypt(rawValue) : null;
},
set(value) {
this.setDataValue('email', value ? encrypt(value) : null);
}
},
joinDate: {
type: DataTypes.DATE,
+61
View File
@@ -0,0 +1,61 @@
const crypto = require('crypto');
require('dotenv').config();
// 환경 변수에서 암호화 키 가져오기
const RAW_ENCRYPTION_KEY = process.env.ENCRYPTION_KEY || '안전한_암호화_키_설정';
// 키를 정확히 32바이트(256비트)로 변환
let ENCRYPTION_KEY;
try {
// 동기적으로 키 생성 (초기화 시 한 번만 실행됨)
ENCRYPTION_KEY = crypto.createHash('sha256').update(RAW_ENCRYPTION_KEY).digest();
} catch (error) {
console.error('암호화 키 생성 오류:', error);
throw new Error('암호화 키 생성 실패');
}
const IV_LENGTH = 16; // AES 블록 크기
/**
* 문자열을 암호화하는 함수
* @param {string} text - 암호화할 텍스트
* @returns {string} - 암호화된 텍스트 (base64 인코딩)
*/
function encrypt(text) {
if (!text) return null;
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
// IV와 암호화된 데이터를 함께 저장 (복호화 시 필요)
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
/**
* 암호화된 문자열을 복호화하는 함수
* @param {string} text - 복호화할 암호화된 텍스트
* @returns {string} - 복호화된 원본 텍스트
*/
function decrypt(text) {
if (!text) return null;
const textParts = text.split(':');
if (textParts.length !== 2) return null;
const iv = Buffer.from(textParts[0], 'hex');
const encryptedText = Buffer.from(textParts[1], 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
module.exports = {
encrypt,
decrypt
};