Resolve both authenticated identity and auth context from HTTP headers. Uses `AuthContext::from_jwt()` when JWT claims are available (richer context), falls back to `build_auth_context()` for API key / password auth.
(
headers: &HeaderMap,
state: &AppState,
peer_addr: &str,
)
| 90 | /// Uses `AuthContext::from_jwt()` when JWT claims are available (richer context), |
| 91 | /// falls back to `build_auth_context()` for API key / password auth. |
| 92 | pub fn resolve_auth( |
| 93 | headers: &HeaderMap, |
| 94 | state: &AppState, |
| 95 | peer_addr: &str, |
| 96 | ) -> Result< |
| 97 | ( |
| 98 | AuthenticatedIdentity, |
| 99 | crate::control::security::auth_context::AuthContext, |
| 100 | ), |
| 101 | ApiError, |
| 102 | > { |
| 103 | use crate::control::security::auth_context::{AuthContext, generate_session_id}; |
| 104 | |
| 105 | // Check for JWT Bearer to get rich AuthContext. |
| 106 | if let Some(auth_header) = headers.get("authorization") |
| 107 | && let Ok(auth_str) = auth_header.to_str() |
| 108 | && let Some(token) = auth_str.strip_prefix("Bearer ") |
| 109 | { |
| 110 | let token = token.trim(); |
| 111 | if let Some(identity) = try_validate_jwt(state, token) { |
| 112 | let auth_ctx = if let Some(ref registry) = state.shared.jwks_registry |
| 113 | && let Ok(claims) = registry.decode_claims(token) |
| 114 | { |
| 115 | AuthContext::from_jwt(&claims, generate_session_id()) |
| 116 | } else { |
| 117 | tracing::trace!("JWT claims decode unavailable, using basic auth context"); |
| 118 | session_auth::build_auth_context(&identity) |
| 119 | }; |
| 120 | let auth_ctx = apply_on_deny_header(headers, auth_ctx); |
| 121 | return Ok((identity, auth_ctx)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Fallback: resolve identity normally and build basic AuthContext. |
| 126 | let identity = resolve_identity(headers, state, peer_addr)?; |
| 127 | let auth_ctx = apply_on_deny_header(headers, session_auth::build_auth_context(&identity)); |
| 128 | Ok((identity, auth_ctx)) |
| 129 | } |
| 130 | |
| 131 | /// Check `X-On-Deny` header and set the `on_deny_override` on AuthContext. |
| 132 | fn apply_on_deny_header( |
no test coverage detected