fix: enhance logging in token introspection and validation for better debugging
This commit is contained in:
+7
-5
@@ -86,6 +86,7 @@ def get_introspection_endpoint() -> Optional[str]:
|
|||||||
def introspect_token(token: str) -> Dict[str, Any]:
|
def introspect_token(token: str) -> Dict[str, Any]:
|
||||||
introspection_endpoint = get_introspection_endpoint()
|
introspection_endpoint = get_introspection_endpoint()
|
||||||
if not introspection_endpoint:
|
if not introspection_endpoint:
|
||||||
|
logger.error("Introspection endpoint not available")
|
||||||
raise HTTPException(status_code=401, detail="Unable to introspect token")
|
raise HTTPException(status_code=401, detail="Unable to introspect token")
|
||||||
|
|
||||||
data = {"token": token, "token_type_hint": "access_token"}
|
data = {"token": token, "token_type_hint": "access_token"}
|
||||||
@@ -98,6 +99,9 @@ def introspect_token(token: str) -> Dict[str, Any]:
|
|||||||
with httpx.Client(timeout=10.0) as client:
|
with httpx.Client(timeout=10.0) as client:
|
||||||
response = client.post(introspection_endpoint, data=data, headers={"Accept": "application/json"}, auth=auth)
|
response = client.post(introspection_endpoint, data=data, headers={"Accept": "application/json"}, auth=auth)
|
||||||
|
|
||||||
|
logger.debug("Introspection request to %s returned %s", introspection_endpoint, response.status_code)
|
||||||
|
logger.debug("Introspection response text: %s", response.text)
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise HTTPException(status_code=401, detail="Unable to introspect token")
|
raise HTTPException(status_code=401, detail="Unable to introspect token")
|
||||||
|
|
||||||
@@ -120,6 +124,7 @@ async def validate_token(credentials: HTTPAuthorizationCredentials = Security(se
|
|||||||
key = get_signing_key(token)
|
key = get_signing_key(token)
|
||||||
public_key = jwk.construct(key)
|
public_key = jwk.construct(key)
|
||||||
discovery = get_discovery()
|
discovery = get_discovery()
|
||||||
|
logger.debug("Validating token with JWKS; issuer=%s, audience=%s", discovery.get("issuer"), settings.oidc_audience)
|
||||||
# Only pass `audience` to the decoder if configured. Some providers
|
# Only pass `audience` to the decoder if configured. Some providers
|
||||||
# (or local development setups) may not include the aud claim in a
|
# (or local development setups) may not include the aud claim in a
|
||||||
# way that matches your API identifier; in that case leave
|
# way that matches your API identifier; in that case leave
|
||||||
@@ -132,14 +137,11 @@ async def validate_token(credentials: HTTPAuthorizationCredentials = Security(se
|
|||||||
jwt_kwargs["audience"] = settings.oidc_audience
|
jwt_kwargs["audience"] = settings.oidc_audience
|
||||||
|
|
||||||
verified = jwt.decode(token, public_key, **jwt_kwargs)
|
verified = jwt.decode(token, public_key, **jwt_kwargs)
|
||||||
|
logger.debug("JWT validation succeeded; claims=%s", verified)
|
||||||
except JWTError:
|
except JWTError:
|
||||||
logger.exception("JWT validation failed, attempting introspection")
|
logger.exception("JWT validation failed, attempting introspection")
|
||||||
verified = introspect_token(token)
|
verified = introspect_token(token)
|
||||||
|
logger.debug("Introspection succeeded; claims=%s", verified)
|
||||||
try:
|
|
||||||
return TokenClaims(**verified)
|
|
||||||
except Exception as exc:
|
|
||||||
raise HTTPException(status_code=401, detail="Unable to parse token claims") from exc
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return TokenClaims(**verified)
|
return TokenClaims(**verified)
|
||||||
|
|||||||
@@ -119,4 +119,5 @@ async def auth_callback(payload: AuthCallbackRequest):
|
|||||||
|
|
||||||
@app.post("/chat", response_class=StreamingResponse)
|
@app.post("/chat", response_class=StreamingResponse)
|
||||||
async def chat(request: ChatRequest, token=Depends(validate_token)):
|
async def chat(request: ChatRequest, token=Depends(validate_token)):
|
||||||
|
logger.debug("Chat request authenticated: %s", token.dict())
|
||||||
return StreamingResponse(event_stream(request), media_type="text/event-stream")
|
return StreamingResponse(event_stream(request), media_type="text/event-stream")
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ async function handleRedirectCallback(): Promise<string | null> {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const result = await response.json()
|
const result = await response.json()
|
||||||
const token = result.id_token || result.access_token
|
const token = result.access_token || result.id_token
|
||||||
if (response.ok && token) {
|
if (response.ok && token) {
|
||||||
setLocalToken(token)
|
setLocalToken(token)
|
||||||
return token
|
return token
|
||||||
|
|||||||
Reference in New Issue
Block a user