From 356929b399ef31a4de82906267df438cf29ea59d Mon Sep 17 00:00:00 2001 From: 0-Zz-ang Date: Thu, 10 Jul 2025 15:56:13 +0900 Subject: 인터페이스 관련 파일 수정 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/integration-log/saml-logging.ts | 232 ++++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 lib/integration-log/saml-logging.ts (limited to 'lib/integration-log/saml-logging.ts') diff --git a/lib/integration-log/saml-logging.ts b/lib/integration-log/saml-logging.ts new file mode 100644 index 00000000..ba361d14 --- /dev/null +++ b/lib/integration-log/saml-logging.ts @@ -0,0 +1,232 @@ +"use server"; + +import { logIntegrationExecution } from "./service"; + +/** + * SAML 인증 로깅 래퍼 함수 + * + * @description + * SAML 인증 과정을 자동으로 로깅하는 래퍼 함수입니다. + * 로그인, 로그아웃, SSO 등 다양한 SAML 작업의 실행 시간과 결과를 기록합니다. + * + * @param integrationId 인터페이스 ID (추후 매핑 필요) + * @param operation 작업 유형 (login, logout, sso 등) + * @param userId 사용자 ID (선택사항) + * @param processor 실제 SAML 처리 함수 + * @returns 처리 결과 + * + * @example + * // 기본 SAML 작업 로깅 + * const result = await withSamlLogging( + * 3, // SAML 인터페이스 ID + * 'validate', + * 'user123', + * async () => { + * // SAML 토큰 검증 로직 + * const isValid = await validateSamlToken(token); + * return { isValid, userId: 'user123' }; + * } + * ); + * + * @example + * // 에러 처리와 함께 + * try { + * const authResult = await withSamlLogging( + * 3, + * 'authenticate', + * 'user456', + * async () => { + * const userInfo = await authenticateUser(samlResponse); + * if (!userInfo) { + * throw new Error('Authentication failed'); + * } + * return userInfo; + * } + * ); + * } catch (error) { + * console.error('SAML 인증 실패:', error); + * } + * + * @example + * // 익명 사용자 작업 + * const metadata = await withSamlLogging( + * 3, + * 'get_metadata', + * undefined, // 익명 사용자 + * async () => { + * return await getSamlMetadata(); + * } + * ); + */ +export async function withSamlLogging( + integrationId: number, + operation: string, + userId?: string, + processor?: () => Promise +): Promise { + const start = Date.now(); + + try { + let result: T; + + if (processor) { + result = await processor(); + } else { + // 기본 처리가 없는 경우 빈 결과 반환 + result = {} as T; + } + + const duration = Date.now() - start; + + // 성공 로그 기록 + await logIntegrationExecution({ + integrationId, + status: 'success', + responseTime: duration, + requestMethod: 'SAML', + requestUrl: `saml_${operation}`, + correlationId: `saml_${operation}_${userId || 'anonymous'}_${Date.now()}`, + }); + + return result; + + } catch (error) { + const duration = Date.now() - start; + + // 실패 로그 기록 + await logIntegrationExecution({ + integrationId, + status: 'failed', + responseTime: duration, + errorMessage: error instanceof Error ? error.message : 'Unknown error', + requestMethod: 'SAML', + requestUrl: `saml_${operation}`, + correlationId: `saml_${operation}_${userId || 'anonymous'}_${Date.now()}`, + }); + + throw error; + } +} + +/** + * SAML SSO 로그인 로깅 헬퍼 함수 + * + * @description + * SAML을 통한 SSO 로그인 과정을 로깅하는 전용 헬퍼 함수입니다. + * 로그인 성공/실패 여부와 처리 시간을 자동으로 기록합니다. + * + * @param userId 사용자 ID + * @param processor 로그인 처리 함수 + * @returns 처리 결과 + * + * @example + * // 기본 SAML 로그인 로깅 + * const loginResult = await withSamlLoginLogging( + * 'user123', + * async () => { + * // SAML 응답 처리 + * const samlResponse = await parseSamlResponse(req.body); + * const userInfo = await validateAndGetUserInfo(samlResponse); + * + * // 세션 생성 + * const session = await createSession(userInfo); + * + * return { + * success: true, + * user: userInfo, + * sessionId: session.id + * }; + * } + * ); + * + * @example + * // 에러 처리와 함께 + * try { + * const result = await withSamlLoginLogging( + * 'user456', + * async () => { + * const userInfo = await authenticateWithSaml(samlToken); + * if (!userInfo.isActive) { + * throw new Error('User account is disabled'); + * } + * return userInfo; + * } + * ); + * + * // 로그인 성공 후 처리 + * redirect('/dashboard'); + * } catch (error) { + * console.error('SAML 로그인 실패:', error); + * redirect('/login?error=saml_failed'); + * } + */ +export async function withSamlLoginLogging( + userId: string, + processor: () => Promise +): Promise { + return withSamlLogging( + 3, // SAML SSO 인터페이스 ID (추후 매핑 필요) + 'login', + userId, + processor + ); +} + +/** + * SAML SSO 로그아웃 로깅 헬퍼 함수 + * + * @description + * SAML을 통한 SSO 로그아웃 과정을 로깅하는 전용 헬퍼 함수입니다. + * 로그아웃 성공/실패 여부와 처리 시간을 자동으로 기록합니다. + * + * @param userId 사용자 ID + * @param processor 로그아웃 처리 함수 + * @returns 처리 결과 + * + * @example + * // 기본 SAML 로그아웃 로깅 + * const logoutResult = await withSamlLogoutLogging( + * 'user123', + * async () => { + * // 세션 종료 + * await destroySession(sessionId); + * + * // SAML 로그아웃 요청 생성 + * const logoutRequest = await createSamlLogoutRequest(userId); + * + * return { + * success: true, + * logoutUrl: logoutRequest.url, + * sessionDestroyed: true + * }; + * } + * ); + * + * @example + * // 강제 로그아웃 (에러 무시) + * const result = await withSamlLogoutLogging( + * 'user456', + * async () => { + * try { + * await destroySession(sessionId); + * await notifyIdpLogout(userId); + * } catch (error) { + * // 로그아웃 과정에서 에러가 발생해도 계속 진행 + * console.warn('일부 로그아웃 과정에서 에러:', error); + * } + * + * return { success: true, forced: true }; + * } + * ); + */ +export async function withSamlLogoutLogging( + userId: string, + processor: () => Promise +): Promise { + return withSamlLogging( + 3, // SAML SSO 인터페이스 ID (추후 매핑 필요) + 'logout', + userId, + processor + ); +} \ No newline at end of file -- cgit v1.2.3