hirnd 0.1.0

hirn standalone daemon — gRPC, HTTP, and MCP server for cognitive memory
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use std::collections::HashMap;
use std::sync::Arc;

use axum::extract::Request;
use axum::http::StatusCode;
use axum::middleware::Next;
use axum::response::Response;
use hirn::prelude::{AgentId, Namespace};
use jsonwebtoken::{DecodingKey, EncodingKey, Header, Validation, decode, encode};
use metrics::counter;
use serde::{Deserialize, Serialize};
use subtle::ConstantTimeEq;

use crate::config::{AuthConfig, TokenConfig};

const INTERNAL_REQUEST_HEADERS: &[&str] = &[
    "x-hirnd-expected-owner-id",
    "x-client-cert-cn",
    "x-token-namespaces",
    "x-token-operations",
];

/// Hash an API key to a fixed 32-byte digest for constant-time comparison.
///
/// Using blake3 normalizes all key lengths so that `ct_eq` on the digests
/// does not leak the expected key length via response timing (N-H05).
fn hash_api_key(key: &str) -> [u8; 32] {
    *blake3::hash(key.as_bytes()).as_bytes()
}

/// Resolved identity from an API key: realm + agent_id.
#[derive(Debug, Clone)]
pub struct KeyIdentity {
    pub realm: String,
    pub agent_id: String,
}

/// Operations a token is allowed to perform.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Operation {
    Read,
    Write,
    Admin,
}

/// JWT claims carried in a token-scoped session token.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenClaims {
    /// Realm this token is scoped to.
    pub realm: String,
    /// Agent identity.
    pub agent_id: String,
    /// Namespace allowlist. Empty = private + shared only.
    #[serde(default)]
    pub namespaces: Vec<String>,
    /// Allowed operations. Empty = all.
    #[serde(default)]
    pub operations: Vec<Operation>,
    /// Issued-at (seconds since epoch).
    pub iat: u64,
    /// Expiry (seconds since epoch).
    pub exp: u64,
}

/// Resolved identity from either an API key or JWT token.
#[derive(Debug, Clone)]
pub struct ResolvedIdentity {
    pub realm: String,
    pub agent_id: String,
    /// Namespace restrictions from token (empty = unrestricted / default).
    pub namespaces: Vec<String>,
    /// Operation restrictions from token (empty = unrestricted).
    pub operations: Vec<Operation>,
}

pub(crate) fn token_allows_operation(
    allowed_operations: &[Operation],
    required: &Operation,
) -> bool {
    allowed_operations.is_empty() || allowed_operations.contains(required)
}

pub(crate) fn token_allows_namespace(
    agent_id: &AgentId,
    allowed_namespaces: &[String],
    namespace: Option<&str>,
) -> bool {
    let private_namespace = Namespace::private_for(agent_id);
    let shared_namespace = Namespace::shared();
    let default_namespace = Namespace::default_ns();
    let private_name = private_namespace.as_str();
    let shared_name = shared_namespace.as_str();
    let default_name = default_namespace.as_str();

    let effective_namespace = match namespace {
        Some(ns) if ns != default_name => ns,
        _ => private_name,
    };

    if allowed_namespaces.is_empty() {
        return effective_namespace == private_name || effective_namespace == shared_name;
    }

    allowed_namespaces
        .iter()
        .any(|allowed| match allowed.as_str() {
            "private" | "default" => effective_namespace == private_name,
            "shared" => effective_namespace == shared_name,
            other => effective_namespace == other,
        })
}

/// Shared authentication state.
pub struct AuthState {
    /// Maps blake3(API key) digest → (realm, agent_id).
    /// Pre-hashing at construction time means `validate()` performs O(n)
    /// fixed-length digest comparisons with no length side-channel (N-H05).
    keys: Option<HashMap<[u8; 32], KeyIdentity>>,
    /// Maps client certificate CN → (realm, agent_id) for mTLS authentication.
    client_certs: HashMap<String, KeyIdentity>,
    /// Token signing/verification config.
    token_config: Option<TokenConfig>,
    /// Whether explicit insecure development mode permits unauthenticated requests.
    allow_unauthenticated: bool,
}

impl AuthState {
    pub fn new(auth_config: Option<&AuthConfig>, token_config: Option<&TokenConfig>) -> Self {
        Self::with_posture(auth_config, token_config, false)
    }

    pub fn insecure_dev_mode(
        auth_config: Option<&AuthConfig>,
        token_config: Option<&TokenConfig>,
    ) -> Self {
        Self::with_posture(auth_config, token_config, true)
    }

    fn with_posture(
        auth_config: Option<&AuthConfig>,
        token_config: Option<&TokenConfig>,
        allow_unauthenticated: bool,
    ) -> Self {
        let client_certs = auth_config
            .map(|c| {
                c.client_certs
                    .iter()
                    .map(|(cn, kc)| {
                        (
                            cn.clone(),
                            KeyIdentity {
                                realm: kc.realm.clone(),
                                agent_id: kc.agent_id.clone(),
                            },
                        )
                    })
                    .collect()
            })
            .unwrap_or_default();

        Self {
            keys: auth_config.map(|c| {
                c.api_keys
                    .iter()
                    .map(|(key, kc)| {
                        (
                            hash_api_key(key),
                            KeyIdentity {
                                realm: kc.realm.clone(),
                                agent_id: kc.agent_id.clone(),
                            },
                        )
                    })
                    .collect()
            }),
            client_certs,
            token_config: token_config.cloned(),
            allow_unauthenticated,
        }
    }

    /// Validate an API key using constant-time comparison to prevent timing
    /// side-channel attacks.
    ///
    /// **N-H05 fix:** both the candidate and stored keys are hashed to a
    /// fixed 32-byte blake3 digest before comparison, so `ct_eq` always
    /// compares equal-length values and the response time does not reveal
    /// expected key length. All stored-key digests are iterated regardless
    /// of match to avoid early-exit timing differences.
    pub fn validate(&self, key: &str) -> Option<&KeyIdentity> {
        self.keys.as_ref().and_then(|keys| {
            let candidate_hash = hash_api_key(key);
            let mut found: Option<&KeyIdentity> = None;
            for (stored_hash, identity) in keys {
                // ct_eq on [u8; 32]: always 32 bytes, constant time.
                if stored_hash.ct_eq(&candidate_hash).into() {
                    found = Some(identity);
                }
            }
            found
        })
    }

    /// Validate a client certificate CN. Returns the associated realm + agent_id if mapped.
    pub fn validate_client_cert(&self, cn: &str) -> Option<&KeyIdentity> {
        self.client_certs.get(cn)
    }

    /// Whether auth is enabled.
    pub fn is_enabled(&self) -> bool {
        self.keys.is_some()
    }

    /// Whether explicit insecure development posture allows unauthenticated requests.
    pub fn allows_unauthenticated(&self) -> bool {
        self.allow_unauthenticated
    }

    /// Whether token issuance is enabled.
    pub fn tokens_enabled(&self) -> bool {
        self.token_config.is_some()
    }

    /// Issue a JWT token for the given identity with optional namespace/operation scoping.
    pub fn issue_token(
        &self,
        identity: &KeyIdentity,
        namespaces: Vec<String>,
        operations: Vec<Operation>,
        ttl_override: Option<u64>,
    ) -> Result<String, String> {
        let tc = self
            .token_config
            .as_ref()
            .ok_or("token issuance not configured")?;

        let now = jsonwebtoken::get_current_timestamp();
        let ttl = ttl_override.unwrap_or(tc.ttl_secs);

        let claims = TokenClaims {
            realm: identity.realm.clone(),
            agent_id: identity.agent_id.clone(),
            namespaces,
            operations,
            iat: now,
            exp: now + ttl,
        };

        encode(
            &Header::default(),
            &claims,
            &EncodingKey::from_secret(tc.secret.as_bytes()),
        )
        .map_err(|e| format!("failed to encode token: {e}"))
    }

    /// Validate a JWT token. Returns the decoded claims if valid.
    pub fn validate_token(&self, token: &str) -> Result<TokenClaims, TokenError> {
        let tc = self
            .token_config
            .as_ref()
            .ok_or(TokenError::NotConfigured)?;

        let mut validation = Validation::default();
        validation.set_required_spec_claims(&["exp", "iat"]);
        // N-M01 fix: leeway covers only clock skew between client and server.
        // rotation_grace_secs is NOT applied as universal leeway because that
        // would silently accept tokens expired by up to rotation_grace_secs
        // from ANY key, widening the acceptance window dangerously.
        // API key rotation is managed at the key-store level (add new key,
        // issue new tokens, remove old key after drain); JWT leeway is for
        // clock skew only.
        validation.leeway = tc.clock_skew_leeway_secs;

        let data = decode::<TokenClaims>(
            token,
            &DecodingKey::from_secret(tc.secret.as_bytes()),
            &validation,
        )
        .map_err(|e| match e.kind() {
            jsonwebtoken::errors::ErrorKind::ExpiredSignature => TokenError::Expired,
            _ => TokenError::Invalid(e.to_string()),
        })?;

        Ok(data.claims)
    }
}

#[derive(Debug)]
pub enum TokenError {
    Expired,
    Invalid(String),
    NotConfigured,
}

/// Axum middleware layer for API key and JWT authentication.
pub async fn auth_middleware(
    state: axum::extract::State<Arc<AuthState>>,
    mut request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    let client_cn = request
        .headers()
        .get("x-client-cert-cn")
        .and_then(|v| v.to_str().ok())
        .map(|s| s.to_owned());
    for header in INTERNAL_REQUEST_HEADERS {
        if request.headers_mut().remove(*header).is_some() {
            counter!(
                "hirnd_internal_metadata_strips_total",
                "interface" => "http",
                "header" => *header,
            )
            .increment(1);
        }
    }

    if !state.is_enabled() {
        if !state.allows_unauthenticated() {
            tracing::warn!(
                "HTTP auth rejected: auth is not configured and insecure_dev_mode is disabled"
            );
            return Err(StatusCode::UNAUTHORIZED);
        }
        return Ok(next.run(request).await);
    }

    // ── mTLS: check client certificate CN (injected by serve_http_tls) ──
    // F-17: Internal forwarding headers were already stripped above.

    if let Some(cn) = client_cn.as_deref() {
        if let Some(ki) = state.validate_client_cert(cn) {
            let identity = ResolvedIdentity {
                realm: ki.realm.clone(),
                agent_id: ki.agent_id.clone(),
                namespaces: vec![],
                operations: vec![],
            };

            // Inject realm and agent_id as headers for downstream handlers
            request.headers_mut().insert(
                "x-realm-id",
                identity
                    .realm
                    .parse()
                    .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
            );
            request.headers_mut().insert(
                "x-agent-id",
                identity
                    .agent_id
                    .parse()
                    .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
            );

            return Ok(next.run(request).await);
        }
        // CN not in mapping — fall through to Bearer token auth
    }

    // ── Bearer token auth (JWT or API key) ──

    // Extract Bearer token from Authorization header
    let auth_header = request
        .headers()
        .get("authorization")
        .and_then(|v| v.to_str().ok());

    let bearer = match auth_header {
        Some(h) if h.starts_with("Bearer ") => &h[7..],
        _ => {
            tracing::warn!("HTTP auth failed: missing or invalid authorization header");
            return Err(StatusCode::UNAUTHORIZED);
        }
    };

    // Try JWT token first (if tokens are configured), then fall back to API key
    let identity = if state.tokens_enabled() {
        match state.validate_token(bearer) {
            Ok(claims) => {
                // Store token restrictions as daemon-authored headers for downstream enforcement.
                let ns_json = serde_json::to_string(&claims.namespaces)
                    .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
                request.headers_mut().insert(
                    "x-token-namespaces",
                    ns_json
                        .parse()
                        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
                );
                let ops_json = serde_json::to_string(&claims.operations)
                    .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
                request.headers_mut().insert(
                    "x-token-operations",
                    ops_json
                        .parse()
                        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
                );
                ResolvedIdentity {
                    realm: claims.realm,
                    agent_id: claims.agent_id,
                    namespaces: claims.namespaces,
                    operations: claims.operations,
                }
            }
            Err(TokenError::Expired) => {
                tracing::warn!("HTTP auth failed: token expired");
                return Err(StatusCode::UNAUTHORIZED);
            }
            Err(TokenError::NotConfigured) => {
                // Shouldn't happen since we checked tokens_enabled
                return Err(StatusCode::INTERNAL_SERVER_ERROR);
            }
            Err(TokenError::Invalid(_)) => {
                // Not a valid JWT — try as API key
                match state.validate(bearer) {
                    Some(ki) => ResolvedIdentity {
                        realm: ki.realm.clone(),
                        agent_id: ki.agent_id.clone(),
                        namespaces: vec![],
                        operations: vec![],
                    },
                    None => {
                        tracing::warn!("HTTP auth failed: invalid JWT and invalid API key");
                        return Err(StatusCode::UNAUTHORIZED);
                    }
                }
            }
        }
    } else {
        // No token config — API key only
        match state.validate(bearer) {
            Some(ki) => ResolvedIdentity {
                realm: ki.realm.clone(),
                agent_id: ki.agent_id.clone(),
                namespaces: vec![],
                operations: vec![],
            },
            None => {
                tracing::warn!("HTTP auth failed: invalid API key");
                return Err(StatusCode::UNAUTHORIZED);
            }
        }
    };

    // Inject realm and agent_id as headers for downstream handlers
    request.headers_mut().insert(
        "x-realm-id",
        identity
            .realm
            .parse()
            .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
    );
    request.headers_mut().insert(
        "x-agent-id",
        identity
            .agent_id
            .parse()
            .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
    );

    Ok(next.run(request).await)
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::Router;
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use axum::middleware;
    use axum::routing::get;
    use tower::ServiceExt;

    async fn ok() -> &'static str {
        "ok"
    }

    #[tokio::test]
    async fn auth_disabled_without_insecure_dev_mode_returns_unauthorized() {
        let router = Router::new()
            .route("/ok", get(ok))
            .layer(middleware::from_fn_with_state(
                Arc::new(AuthState::new(None, None)),
                auth_middleware,
            ));

        let response = router
            .oneshot(Request::builder().uri("/ok").body(Body::empty()).unwrap())
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    }
}