MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / resolve_auth

Function resolve_auth

nodedb/src/control/server/http/auth.rs:92–129  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

90/// Uses `AuthContext::from_jwt()` when JWT claims are available (richer context),
91/// falls back to `build_auth_context()` for API key / password auth.
92pub 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.
132fn apply_on_deny_header(

Callers 3

from_request_partsMethod · 0.85
create_sessionFunction · 0.85
exchange_keyFunction · 0.85

Calls 7

try_validate_jwtFunction · 0.85
generate_session_idFunction · 0.85
build_auth_contextFunction · 0.85
apply_on_deny_headerFunction · 0.85
resolve_identityFunction · 0.85
decode_claimsMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected