|
@@ -1,3 +1,4 @@
|
|
|
|
|
+import logging
|
|
|
import uuid
|
|
import uuid
|
|
|
from datetime import datetime, timedelta, timezone
|
|
from datetime import datetime, timedelta, timezone
|
|
|
from urllib.parse import urlencode
|
|
from urllib.parse import urlencode
|
|
@@ -15,6 +16,7 @@ from app.core.sso_client import exchange_code_for_token, fetch_sso_userinfo
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
router = APIRouter()
|
|
|
settings = get_settings()
|
|
settings = get_settings()
|
|
|
|
|
+logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
class CodeExchangeRequest(BaseModel):
|
|
class CodeExchangeRequest(BaseModel):
|
|
@@ -67,13 +69,21 @@ async def exchange_code(req: CodeExchangeRequest):
|
|
|
if not req.code:
|
|
if not req.code:
|
|
|
return {"code": "100001", "message": "缺少授权码", "data": None}
|
|
return {"code": "100001", "message": "缺少授权码", "data": None}
|
|
|
|
|
|
|
|
|
|
+ logger.info("[SSO] exchange_code start, code=%s", req.code[:10])
|
|
|
|
|
+ logger.info("[SSO] sso_base_url=%s", settings.sso_base_url)
|
|
|
|
|
+ logger.info("[SSO] client_id=%s", settings.sso_client_id)
|
|
|
|
|
+ logger.info("[SSO] redirect_uri=%s", settings.sso_redirect_uri)
|
|
|
|
|
+
|
|
|
try:
|
|
try:
|
|
|
token_resp = await exchange_code_for_token(req.code)
|
|
token_resp = await exchange_code_for_token(req.code)
|
|
|
|
|
+ logger.info("[SSO] token response: %s", token_resp)
|
|
|
sso_access_token = token_resp.get("access_token")
|
|
sso_access_token = token_resp.get("access_token")
|
|
|
if not sso_access_token:
|
|
if not sso_access_token:
|
|
|
|
|
+ logger.error("[SSO] no access_token in response: %s", token_resp)
|
|
|
raise HTTPException(status_code=500, detail="登录失败: 获取令牌失败")
|
|
raise HTTPException(status_code=500, detail="登录失败: 获取令牌失败")
|
|
|
|
|
|
|
|
sso_userinfo = await fetch_sso_userinfo(sso_access_token)
|
|
sso_userinfo = await fetch_sso_userinfo(sso_access_token)
|
|
|
|
|
+ logger.info("[SSO] userinfo: %s", sso_userinfo)
|
|
|
if not sso_userinfo.get("username") and not sso_userinfo.get("sub"):
|
|
if not sso_userinfo.get("username") and not sso_userinfo.get("sub"):
|
|
|
raise HTTPException(status_code=500, detail="登录失败: 用户信息格式异常")
|
|
raise HTTPException(status_code=500, detail="登录失败: 用户信息格式异常")
|
|
|
|
|
|
|
@@ -115,6 +125,8 @@ async def exchange_code(req: CodeExchangeRequest):
|
|
|
except HTTPException:
|
|
except HTTPException:
|
|
|
raise
|
|
raise
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
|
|
+ import traceback
|
|
|
|
|
+ logger.error("[SSO] exchange_code failed: %s", traceback.format_exc())
|
|
|
raise HTTPException(status_code=500, detail=f"登录失败: {str(e)}")
|
|
raise HTTPException(status_code=500, detail=f"登录失败: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|