1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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<T>(
integrationId: number,
operation: string,
userId?: string,
processor?: () => Promise<T>
): Promise<T> {
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<T>(
userId: string,
processor: () => Promise<T>
): Promise<T> {
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<T>(
userId: string,
processor: () => Promise<T>
): Promise<T> {
return withSamlLogging(
3, // SAML SSO 인터페이스 ID (추후 매핑 필요)
'logout',
userId,
processor
);
}
|