summaryrefslogtreecommitdiff
path: root/app/api/auth/saml/authn-request/route.ts
diff options
context:
space:
mode:
authorjoonhoekim <26rote@gmail.com>2025-06-23 06:44:34 +0000
committerjoonhoekim <26rote@gmail.com>2025-06-23 06:44:34 +0000
commitebe273ef4564d55f9bf193adc51a9e58211e72e9 (patch)
tree30e8c48be41d14751eceb4c24d88c18d03e9102b /app/api/auth/saml/authn-request/route.ts
parentabd9f950bbd95b9ad713a26d3fd8a7e0282b7c51 (diff)
(김준회 SAML 2.0 SSO 리팩터링, 디버깅 유틸리티 추가, MOCK 처리 추가
Diffstat (limited to 'app/api/auth/saml/authn-request/route.ts')
-rw-r--r--app/api/auth/saml/authn-request/route.ts76
1 files changed, 57 insertions, 19 deletions
diff --git a/app/api/auth/saml/authn-request/route.ts b/app/api/auth/saml/authn-request/route.ts
index e3cb8a47..f079aea0 100644
--- a/app/api/auth/saml/authn-request/route.ts
+++ b/app/api/auth/saml/authn-request/route.ts
@@ -1,45 +1,83 @@
-import { NextRequest, NextResponse } from 'next/server'
+/**
+ * SAML 2.0 SSO AuthnRequest 생성 API
+ *
+ * 역할:
+ * - 프론트엔드에서 SAML 로그인 URL을 요청할 때 사용
+ * - SAML AuthnRequest를 생성하고 IdP 로그인 URL 반환
+ * - Mock 모드 지원으로 개발/테스트 환경에서 시뮬레이션 가능
+ *
+ * 플로우:
+ * 1. 사용자가 "Knox SSO로 로그인" 버튼 클릭
+ * 2. 프론트엔드에서 이 API 호출
+ * 3. SAML AuthnRequest URL 생성 후 반환
+ * 4. 프론트엔드에서 해당 URL로 리다이렉트
+ * 5. IdP에서 인증 후 /api/saml/callback으로 SAML Response 전송
+ */
+
+import { NextResponse } from 'next/server'
import { createAuthnRequest } from '../../[...nextauth]/saml/utils'
+import { debugLog, debugError, debugSuccess, debugProcess } from '@/lib/debug-utils'
-const samlEnvironment = {
- NODE_ENV: process.env.NODE_ENV,
- SAML_USE_MOCKUP: process.env.SAML_USE_MOCKUP,
- NEXTAUTH_URL: process.env.NEXTAUTH_URL,
-}
+// SAML 환경변수 상태 체크
+function validateSAMLEnvironment() {
+ const samlEnvironment = {
+ NODE_ENV: process.env.NODE_ENV,
+ SAML_MOCKING_IDP: process.env.SAML_MOCKING_IDP,
+ NEXTAUTH_URL: process.env.NEXTAUTH_URL,
+ SAML_SP_PRIVATE_KEY: process.env.SAML_SP_PRIVATE_KEY ? '✅ Set' : '❌ Missing',
+ SAML_SP_CERT: process.env.SAML_SP_CERT ? '✅ Set' : '❌ Missing',
+ }
+
+ debugLog('📊 SAML Environment check:', JSON.stringify(samlEnvironment, null, 2))
+
+ // 필수 환경변수 검증
+ const missingVars = []
+ if (!process.env.NEXTAUTH_URL) missingVars.push('NEXTAUTH_URL')
-// 환경변수 체크
-function checkEnvironment() {
- console.log('📊 Environment check:', JSON.stringify(samlEnvironment, null, 2))
+ // 키 없어도 구현 가능해서 주석 처리함.
+ // if (!process.env.SAML_SP_PRIVATE_KEY) missingVars.push('SAML_SP_PRIVATE_KEY')
+ // if (!process.env.SAML_SP_CERT) missingVars.push('SAML_SP_CERT')
+ if (missingVars.length > 0) {
+ throw new Error(`Missing required SAML environment variables: ${missingVars.join(', ')}`)
+ }
+
+ return samlEnvironment
}
-// 요청 받으면 따로 파싱할 것 없이 동일하게 행동하므로 아규먼트 없음
+/**
+ * SAML AuthnRequest URL 생성 엔드포인트
+ *
+ * @returns {JSON} { loginUrl: string, success: boolean, isThisMocking?: boolean }
+ */
export async function GET() {
- console.log('🚀 SAML AuthnRequest API started')
- checkEnvironment()
-
+ debugProcess('🚀 SAML AuthnRequest API started')
+
try {
- console.log('SSO STEP 1: Create AuthnRequest')
+ // 환경변수 검증
+ const environment = validateSAMLEnvironment()
+
+ debugProcess('SSO STEP 1: Create AuthnRequest')
const startTime = Date.now()
const loginUrl = await createAuthnRequest()
const endTime = Date.now()
- console.log('SAML AuthnRequest created successfully:', {
- url: loginUrl,
+ debugSuccess('SAML AuthnRequest created successfully:', {
+ url: loginUrl.substring(0, 100) + '...',
urlLength: loginUrl.length,
processingTime: `${endTime - startTime}ms`,
+ mockMode: environment.SAML_MOCKING_IDP === 'true',
timestamp: new Date().toISOString()
})
return NextResponse.json({
loginUrl,
success: true,
- mode: 'real',
- message: 'Using real SAML IdP'
+ isThisMocking: environment.SAML_MOCKING_IDP === 'true'
})
} catch (error) {
- console.error('Failed to create SAML AuthnRequest:', {
+ debugError('Failed to create SAML AuthnRequest:', {
error: error instanceof Error ? error.message : 'Unknown error',
stack: error instanceof Error ? error.stack : undefined,
timestamp: new Date().toISOString()