Compare commits
14 Commits
6c2399b283
...
4a30758d7f
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a30758d7f | |||
| e93f6c8f41 | |||
| b73d5d55fb | |||
| 4a06fc7e83 | |||
| b9642961ac | |||
| e528f6a67e | |||
| e224455dea | |||
| c7b0655ec2 | |||
| 8c0b19d542 | |||
| b3c9a8f6ee | |||
| 5fe38e1c90 | |||
| 295b914f0f | |||
| 4c4ef662fd | |||
| b9fa6255b7 |
@@ -37,4 +37,4 @@ export const useAuth = () => {
|
||||
setFromLoginResponse,
|
||||
logout,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import CryptoJS from "crypto-js";
|
||||
|
||||
const KEY = CryptoJS.enc.Utf8.parse("kr!-Kfp@-fr0nT-CrypT0-Key-STR");
|
||||
const IV = CryptoJS.enc.Utf8.parse("kr!-Kfp@-IVST");
|
||||
const KEY = CryptoJS.enc.Utf8.parse("kr!-Kfp@-fr0nT-CrypT0-Key-STR!!!");
|
||||
const IV = CryptoJS.enc.Utf8.parse("kr!-Kfp@Dash-IV!");
|
||||
|
||||
export type WordArray = CryptoJS.lib.WordArray;
|
||||
|
||||
@@ -143,17 +143,16 @@ export class Security {
|
||||
}
|
||||
|
||||
public static encodeBase64Url(input: WordArray): string {
|
||||
const base64url = CryptoJS.enc.Base64url.stringify(input);
|
||||
return this.convertToSpecialChars(base64url);
|
||||
return CryptoJS.enc.Base64url.stringify(input);
|
||||
}
|
||||
|
||||
public static decodeBase64Url(input: string): WordArray {
|
||||
try {
|
||||
if (this.hasSpecialChars(input)) {
|
||||
const base64url = this.convertFromSpecialChars(input);
|
||||
return CryptoJS.enc.Base64url.parse(base64url);
|
||||
} catch {
|
||||
return CryptoJS.enc.Base64url.parse(input);
|
||||
}
|
||||
|
||||
return CryptoJS.enc.Base64url.parse(input);
|
||||
}
|
||||
|
||||
public static encodeBase64Utf8(input: string): string {
|
||||
@@ -165,17 +164,20 @@ export class Security {
|
||||
}
|
||||
|
||||
public static encodeBase64(input: WordArray): string {
|
||||
const base64 = CryptoJS.enc.Base64.stringify(input);
|
||||
return this.convertToSpecialChars(base64);
|
||||
return CryptoJS.enc.Base64.stringify(input);
|
||||
}
|
||||
|
||||
public static decodeBase64(input: string): WordArray {
|
||||
try {
|
||||
if (this.hasSpecialChars(input)) {
|
||||
const base64 = this.convertFromSpecialChars(input);
|
||||
return CryptoJS.enc.Base64.parse(base64);
|
||||
} catch {
|
||||
return CryptoJS.enc.Base64.parse(input);
|
||||
}
|
||||
|
||||
return CryptoJS.enc.Base64.parse(input);
|
||||
}
|
||||
|
||||
private static hasSpecialChars(input: string): boolean {
|
||||
return input.includes("\u200b") || input.includes("\u200c");
|
||||
}
|
||||
|
||||
public static encodeHex(input: WordArray): string {
|
||||
|
||||
@@ -105,4 +105,4 @@ export const createSessionStorageAtom = <T>(
|
||||
);
|
||||
|
||||
return persistentAtom;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,71 +1,50 @@
|
||||
import { Security } from "@/lib/security";
|
||||
import {clientEnv} from "@/lib/env.client";
|
||||
import { clientEnv } from "@/lib/env.client";
|
||||
|
||||
type StorageValue = any; // 요청대로 any 유지
|
||||
type StorageValue = any;
|
||||
|
||||
// --- 내부 헬퍼 함수들 ---
|
||||
|
||||
const getDecryptedValue = (value: string): StorageValue => {
|
||||
try {
|
||||
if (!value || value.trim() === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const decryptValue = clientEnv.ENV ==="prod" ? Security.decryptBase64(value) : value;
|
||||
|
||||
if (!decryptValue || decryptValue.trim() === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JSON.parse(decryptValue);
|
||||
} catch (error) {
|
||||
// 손상된 데이터인 경우 null 반환
|
||||
const parseStorageValue = (value: string): StorageValue => {
|
||||
if (!value || value.trim() === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rawValue = clientEnv.ENV === "prod" ? Security.decryptBase64(value) : value;
|
||||
if (!rawValue || rawValue.trim() === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JSON.parse(rawValue);
|
||||
};
|
||||
|
||||
const getEncryptedValue = (value: StorageValue): string => {
|
||||
const stringifyValue = JSON.stringify(value);
|
||||
return clientEnv.ENV ==="prod" ? Security.encryptBase64(stringifyValue) : stringifyValue;
|
||||
const stringifyStorageValue = (value: StorageValue): string => {
|
||||
const rawValue = JSON.stringify(value);
|
||||
return clientEnv.ENV === "prod" ? Security.encryptBase64(rawValue) : rawValue;
|
||||
};
|
||||
|
||||
|
||||
// --- LocalStorage 함수들 ---
|
||||
|
||||
export const setLocalStorage = (key: string, value: StorageValue): void => {
|
||||
try {
|
||||
const encryptValue = getEncryptedValue(value);
|
||||
localStorage.setItem(key, encryptValue);
|
||||
} catch (error) {
|
||||
// 필요하면 로깅 추가
|
||||
}
|
||||
localStorage.setItem(key, stringifyStorageValue(value));
|
||||
} catch {}
|
||||
};
|
||||
|
||||
export const getLocalStorage = (key: string): StorageValue => {
|
||||
try {
|
||||
const encryptValue: string | null = localStorage.getItem(key);
|
||||
if (encryptValue === null || encryptValue === "") {
|
||||
const value = localStorage.getItem(key);
|
||||
if (value === null || value === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
let value: StorageValue = null;
|
||||
|
||||
try {
|
||||
// 암호화된 값 복호화 시도
|
||||
value = getDecryptedValue(encryptValue);
|
||||
} catch (decryptError) {
|
||||
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
|
||||
return parseStorageValue(value);
|
||||
} catch {
|
||||
try {
|
||||
value = JSON.parse(encryptValue);
|
||||
} catch (fallbackError) {
|
||||
// 완전히 실패한 경우 저장된 데이터 제거
|
||||
return JSON.parse(value);
|
||||
} catch {
|
||||
localStorage.removeItem(key);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
} catch (error) {
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
@@ -78,43 +57,30 @@ export const clearLocalStorage = (): void => {
|
||||
localStorage.clear();
|
||||
};
|
||||
|
||||
// --- SessionStorage 함수들 ---
|
||||
|
||||
// 세션스토리지는 테스트/실제 서버 모두 암호화 없음
|
||||
export const setSessionStorage = (key: string, value: StorageValue): void => {
|
||||
try {
|
||||
const encryptValue = getEncryptedValue(value);
|
||||
sessionStorage.setItem(key, encryptValue);
|
||||
} catch (error) {
|
||||
// 필요하면 로깅 추가
|
||||
}
|
||||
sessionStorage.setItem(key, stringifyStorageValue(value));
|
||||
} catch {}
|
||||
};
|
||||
|
||||
export const getSessionStorage = (key: string): StorageValue => {
|
||||
try {
|
||||
const encryptValue: string | null = sessionStorage.getItem(key);
|
||||
if (encryptValue === null || encryptValue === "") {
|
||||
const value = sessionStorage.getItem(key);
|
||||
if (value === null || value === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
let value: StorageValue = null;
|
||||
|
||||
try {
|
||||
// 암호화된 값 복호화 시도
|
||||
value = getDecryptedValue(encryptValue);
|
||||
} catch (decryptError) {
|
||||
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
|
||||
return parseStorageValue(value);
|
||||
} catch {
|
||||
try {
|
||||
value = JSON.parse(encryptValue);
|
||||
} catch (fallbackError) {
|
||||
// 완전히 실패한 경우 저장된 데이터 제거
|
||||
localStorage.removeItem(key);
|
||||
return JSON.parse(value);
|
||||
} catch {
|
||||
sessionStorage.removeItem(key);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
} catch (error) {
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
@@ -125,4 +91,4 @@ export const removeSessionStorage = (key: string): void => {
|
||||
|
||||
export const clearSessionStorage = (): void => {
|
||||
sessionStorage.clear();
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user