vtc-service 0.7.0

Service for Verifiable Trust Communities
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
//! `POST /v1/auth/recognise` — cross-community session mint.
//!
//! Phase 3 M3.10. Spec §8.4.
//!
//! The route accepts a foreign community's (`VEC`, `VMC`) pair,
//! runs the M3.9 verifier, evaluates `cross_community_roles.rego`
//! to map the foreign role onto a local role, and mints a session
//! JWT with TTL clamped to
//! `min(jwt_default, vec.validUntil - now, vmc.validUntil - now)`.
//!
//! ## No refresh path
//!
//! Spec §8.4 + plan D5: cross-community sessions **never**
//! refresh. The standard `POST /v1/auth/refresh` route doesn't
//! carry the foreign credentials needed to re-run the
//! recognition check, so re-issuing a token without
//! re-verifying would defeat the "peer community removed
//! mid-session loses access" invariant. The session simply
//! expires when its clamped TTL elapses; the caller mints a
//! fresh one with the latest credentials.

use std::sync::Arc;

use axum::Json;
use axum::extract::State;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use tracing::{info, warn};
use uuid::Uuid;
use vti_common::audit::{AuditEvent, CrossCommunitySessionMintedData};

use crate::auth::session::{Session, SessionState, now_epoch, store_session};
use crate::error::AppError;
use crate::policy::{
    PolicyPurpose, compile as compile_policy, evaluate as evaluate_policy, get_active_policy_id,
    get_policy,
};
use crate::recognition::{
    DidResolverKeyResolver, HttpStatusListFetcher, RecognitionError, VerifiedForeignCredential,
    verify_foreign_vec,
};
use crate::server::AppState;
use affinidi_vc::VerifiableCredential;

/// Request body for `POST /v1/auth/recognise`. The caller
/// supplies the foreign VEC + VMC verbatim — the route
/// resolves the issuer's key + status list itself.
#[derive(Debug, Deserialize)]
pub struct RecogniseRequest {
    pub vec: VerifiableCredential,
    pub vmc: VerifiableCredential,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RecogniseResponse {
    pub session_id: String,
    pub data: RecogniseData,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RecogniseData {
    /// Minted JWT. Carries the *mapped local* role, not the
    /// foreign one.
    pub access_token: String,
    pub access_expires_at: u64,
    /// Foreign issuer DID, surfaced so the caller can correlate
    /// the response with their request (the route doesn't echo
    /// the credentials).
    pub foreign_issuer_did: String,
    /// Local role the foreign role mapped to.
    pub mapped_role: String,
}

pub async fn recognise(
    State(state): State<AppState>,
    Json(req): Json<RecogniseRequest>,
) -> Result<Json<RecogniseResponse>, AppError> {
    // Pre-flight: the route depends on three pieces of optional
    // state. Refuse cleanly when any is missing rather than
    // 500ing inside the handler.
    let registry = state.registry_client.as_ref().cloned().ok_or_else(|| {
        AppError::Validation("trust-registry client not configured on this VTC".into())
    })?;
    let resolver = state
        .did_resolver
        .as_ref()
        .cloned()
        .ok_or_else(|| AppError::Internal("DID resolver not configured".into()))?;
    // JWT-keys availability is re-checked inside
    // `mint_recognised_session` — but bail early when the
    // verifier hasn't been wired either, so the response is
    // shaped by config issues rather than running a real
    // verification first.
    state
        .jwt_keys
        .as_ref()
        .ok_or_else(|| AppError::Authentication("JWT keys not configured".into()))?;

    let key_resolver = DidResolverKeyResolver::new(resolver);
    let status_fetcher = HttpStatusListFetcher::new(reqwest::Client::new());

    let actor_did_for_audit = req.vec.credential_subject_id_for_audit();

    // Run the M3.9 verifier. Failures are mapped to `denied`
    // audit envelopes + a 403 response.
    let verified = match verify_foreign_vec(
        &req.vec,
        &req.vmc,
        &key_resolver,
        &status_fetcher,
        Arc::clone(&registry),
        Utc::now(),
    )
    .await
    {
        Ok(v) => v,
        Err(e) => {
            emit_denied_audit(
                &state,
                &actor_did_for_audit,
                None,
                e.reason_code(),
                None,
                &e,
            )
            .await;
            return Err(map_recognition_error(e));
        }
    };

    mint_recognised_session(&state, verified).await
}

/// Post-verification half of the recognise flow — exposed at
/// `pub(crate)` so integration tests can exercise it without
/// faking a DID resolver. Production goes through
/// [`recognise`] which runs the verifier first; tests
/// hand-build a [`VerifiedForeignCredential`] (typestate
/// proof of "this credential passed the four checks") and
/// drive only the route-level concerns: role-mapping policy,
/// TTL clamp, session mint, audit emission.
pub async fn mint_recognised_session(
    state: &AppState,
    verified: VerifiedForeignCredential,
) -> Result<Json<RecogniseResponse>, AppError> {
    let jwt_keys = state
        .jwt_keys
        .as_ref()
        .cloned()
        .ok_or_else(|| AppError::Authentication("JWT keys not configured".into()))?;

    // Run cross_community_roles.rego to map the foreign role
    // onto a local role. Deny is encoded as either:
    //   - `data.vtc.cross_community_roles.allow = false`, or
    //   - missing/null `mapped_role`.
    let mapped_role = match map_foreign_role(state, &verified).await? {
        Some(r) => r,
        None => {
            emit_denied_audit(
                state,
                &verified.subject_did,
                Some(verified.foreign_issuer_did.as_str()),
                "role-mapping-denied",
                Some(verified.foreign_role.as_str()),
                &RecognitionError::Malformed("policy denied role mapping".into()),
            )
            .await;
            return Err(AppError::Forbidden(format!(
                "cross_community_roles.rego denied mapping for foreign role '{}'",
                verified.foreign_role
            )));
        }
    };

    // Clamp the TTL to `min(jwt_default, vec.validUntil - now,
    // vmc.validUntil - now)`. The verifier already exposed the
    // *earliest* validUntil; we just compare to the configured
    // access-token TTL.
    let config = state.config.read().await;
    let jwt_default = config.auth.access_token_expiry;
    drop(config);

    let now = Utc::now();
    let creds_window_secs = (verified.earliest_valid_until - now).num_seconds().max(0) as u64;
    let access_expiry = jwt_default.min(creds_window_secs);
    if access_expiry == 0 {
        // Credentials expire before the next clock tick. Treat
        // as denied — minting a JWT that expires the same
        // instant it's issued is operator-hostile.
        emit_denied_audit(
            state,
            &verified.subject_did,
            Some(verified.foreign_issuer_did.as_str()),
            "validity-window",
            Some(verified.foreign_role.as_str()),
            &RecognitionError::ValidityWindow("credentials expire immediately".into()),
        )
        .await;
        return Err(AppError::Forbidden(
            "foreign credentials expire too soon to mint a session".into(),
        ));
    }

    // Mint the session. Mirror the existing
    // `authenticate` path: store a Session row + emit a JWT.
    // Skip the refresh token (cross-community sessions don't
    // refresh — see module docs). AAL is `did/aal1`: the foreign
    // VEC verification is a single-factor proof of the subject
    // DID; passkey or VTA step-up is not part of the recognise
    // flow.
    let session_id = format!("xc-{}", Uuid::new_v4());
    let session = Session {
        session_id: session_id.clone(),
        did: verified.subject_did.clone(),
        challenge: String::new(),
        state: SessionState::Authenticated,
        created_at: now_epoch(),
        refresh_token: None,
        refresh_expires_at: None,
        tee_attested: false,
        amr: vec!["did".to_string()],
        acr: "aal1".to_string(),
        token_id: None,
        session_pubkey_b58btc: None,
    };
    store_session(&state.sessions_ks, &session).await?;

    let claims = jwt_keys
        .new_claims(
            verified.subject_did.clone(),
            session_id.clone(),
            mapped_role.clone(),
            Vec::new(),
            access_expiry,
            false,
        )
        .with_aal(vec!["did".to_string()], "aal1");
    let access_expires_at = claims.exp;
    let access_token = jwt_keys.encode(&claims)?;

    // Audit: minted.
    if let Some(writer) = state.audit_writer.as_ref() {
        let payload = CrossCommunitySessionMintedData {
            outcome: "minted".into(),
            foreign_issuer_did: verified.foreign_issuer_did.clone(),
            foreign_role: Some(verified.foreign_role.clone()),
            mapped_role: Some(mapped_role.clone()),
            ttl_seconds: Some(access_expiry),
            reason: None,
        };
        if let Err(e) = writer
            .write(
                &verified.subject_did,
                Some(&verified.subject_did),
                AuditEvent::CrossCommunitySessionMinted(payload),
            )
            .await
        {
            warn!(error = %e, "failed to emit CrossCommunitySessionMinted (minted) envelope");
        }
    }

    info!(
        subject = %verified.subject_did,
        issuer = %verified.foreign_issuer_did,
        mapped_role = %mapped_role,
        ttl = access_expiry,
        "cross-community session minted"
    );

    Ok(Json(RecogniseResponse {
        session_id,
        data: RecogniseData {
            access_token,
            access_expires_at,
            foreign_issuer_did: verified.foreign_issuer_did,
            mapped_role,
        },
    }))
}

/// Run `cross_community_roles.rego` against the verified
/// credential pair. Returns `Ok(Some(mapped_role))` on policy
/// allow, `Ok(None)` on deny. Errors propagate as
/// `AppError::Internal` because they indicate a workspace bug
/// (no policy, compile failure, etc.) — the operator has the
/// fallback "default deny" stub from M2.5.
async fn map_foreign_role(
    state: &AppState,
    verified: &VerifiedForeignCredential,
) -> Result<Option<String>, AppError> {
    let active_id = get_active_policy_id(
        &state.active_policies_ks,
        PolicyPurpose::CrossCommunityRoles,
    )
    .await?
    .ok_or_else(|| AppError::Internal("no active cross_community_roles policy".into()))?;
    let policy = get_policy(&state.policies_ks, active_id)
        .await?
        .ok_or_else(|| {
            AppError::Internal(format!(
                "active cross_community_roles policy {active_id} not found"
            ))
        })?;
    let compiled = compile_policy(&policy.rego_source, policy.id)?;

    let input = serde_json::json!({
        "foreign_vec": {
            "issuer": verified.foreign_issuer_did,
            "role": verified.foreign_role,
            "subject_did": verified.subject_did,
        },
        "action": "mint_session",
    });

    let allow = evaluate_policy(
        &compiled,
        "data.vtc.cross_community_roles.allow",
        input.clone(),
    )?;
    let allow = allow
        .pointer("/result/0/expressions/0/value")
        .and_then(JsonValue::as_bool)
        .unwrap_or(false);
    if !allow {
        return Ok(None);
    }

    // Policy passed the allow gate; now read mapped_role.
    let mapped = evaluate_policy(
        &compiled,
        "data.vtc.cross_community_roles.mapped_role",
        input,
    )?;
    let mapped = mapped
        .pointer("/result/0/expressions/0/value")
        .and_then(JsonValue::as_str)
        .map(str::to_string);
    Ok(mapped)
}

fn map_recognition_error(e: RecognitionError) -> AppError {
    match e {
        RecognitionError::RegistryUnreachable(msg) => {
            AppError::Internal(format!("trust registry unreachable: {msg}"))
        }
        // All other variants are caller-driven rejection
        // signals → 403 Forbidden.
        other => AppError::Forbidden(other.to_string()),
    }
}

async fn emit_denied_audit(
    state: &AppState,
    actor_did: &str,
    foreign_issuer_did: Option<&str>,
    reason: &str,
    foreign_role: Option<&str>,
    _err: &RecognitionError,
) {
    let Some(writer) = state.audit_writer.as_ref() else {
        return;
    };
    let payload = CrossCommunitySessionMintedData {
        outcome: "denied".into(),
        foreign_issuer_did: foreign_issuer_did.unwrap_or("<unknown>").to_string(),
        foreign_role: foreign_role.map(str::to_string),
        mapped_role: None,
        ttl_seconds: None,
        reason: Some(reason.to_string()),
    };
    if let Err(e) = writer
        .write(
            actor_did,
            None,
            AuditEvent::CrossCommunitySessionMinted(payload),
        )
        .await
    {
        warn!(error = %e, "failed to emit CrossCommunitySessionMinted (denied) envelope");
    }
}

// Helper trait — extracts the subject DID from the VEC even
// when verification hasn't run yet, so audit envelopes for the
// `denied` arm can still name an actor when possible.
trait CredentialSubjectIdAccessor {
    fn credential_subject_id_for_audit(&self) -> String;
}

impl CredentialSubjectIdAccessor for VerifiableCredential {
    fn credential_subject_id_for_audit(&self) -> String {
        use affinidi_vc::SubjectValue;
        let subj_map = match &self.credential_subject {
            SubjectValue::Single(m) => Some(m.clone()),
            SubjectValue::Multiple(v) => v.first().cloned(),
        };
        subj_map
            .and_then(|m| m.get("id").and_then(|v| v.as_str()).map(str::to_string))
            .unwrap_or_else(|| "<unknown-subject>".into())
    }
}