meerkat-auth-core 0.8.16

Shared auth primitives for Meerkat: TokenStore backends, RefreshCoordinator impls, OAuth2 helpers, generic cloud-IAM authorizers (AWS SigV4, Google ADC, Azure AD).
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
496
497
498
499
500
501
502
//! Canonical coordinated browser-OAuth credential commit.
//!
//! Browser hosts own loopback HTTP and browser launch. This module owns the
//! security-sensitive terminal transaction: verify one-time state, serialize
//! against refresh/logout, consume state, acquire AuthMachine lifecycle truth,
//! stamp the durable marker, persist, and compensate on failure.

use std::sync::Arc;

use meerkat_core::auth::token_store::{
    CredentialMutationError, CredentialMutationOutcome, PersistedTokens, ProviderAuthPersistence,
    TokenKey, TokenStore,
};
use meerkat_core::handles::{
    AuthLeasePhase, AuthLeaseRestoreSnapshot, AuthLeaseSnapshot, AuthLeaseTransition,
    GeneratedAuthLeaseHandle, LeaseKey,
};
use meerkat_core::{AuthBindingRef, OAuthProviderIdentity};

use crate::oauth_flow::{OAuthFlowAuthority, OAuthFlowError};

/// One verified browser-flow terminal consume request.
#[derive(Clone)]
pub struct BrowserOAuthFlowCommit {
    pub authority: Arc<dyn OAuthFlowAuthority>,
    pub state: String,
    pub provider: OAuthProviderIdentity,
    pub redirect_uri: String,
}

struct PreparedCommit {
    key: TokenKey,
    lease_key: LeaseKey,
    previous: Option<PersistedTokens>,
    previous_lifecycle: AuthLeaseSnapshot,
    previous_lifecycle_restore: AuthLeaseRestoreSnapshot,
    lifecycle_transition: AuthLeaseTransition,
}

/// Atomically consume a one-time browser OAuth flow and publish its credential.
///
/// The persistence capability supplies both the token vault and its
/// cross-process mutation coordinator. The flow authority must be backed by
/// the same AuthMachine lifecycle authority as `auth_lease`.
///
/// Terminal state remains consumed if lifecycle publication or durable save
/// fails. The authorization code has already been exchanged at this point;
/// restoring browser state would permit replay of a terminal OAuth response.
/// Credential/lifecycle state is compensated back to its predecessor instead.
pub async fn save_oauth_tokens_and_consume_browser_flow(
    persistence: ProviderAuthPersistence,
    auth_lease: GeneratedAuthLeaseHandle,
    auth_binding: AuthBindingRef,
    tokens: PersistedTokens,
    flow: BrowserOAuthFlowCommit,
) -> Result<PersistedTokens, CredentialMutationError> {
    if !flow.authority.terminal_flow_state_is_authmachine_owned() {
        return Err(CredentialMutationError::AuthLifecycle(
            "browser OAuth terminal state is not AuthMachine-owned".to_string(),
        ));
    }
    flow.authority
        .verify(
            &flow.state,
            &auth_binding,
            flow.provider,
            &flow.redirect_uri,
        )
        .map_err(flow_error)?;

    let store = persistence.token_store();
    let coordinator = persistence.refresh_coordinator();
    let key = TokenKey::from_auth_binding(&auth_binding);
    let load_key = key.clone();
    let outcome = coordinator
        .with_exclusive_mutation(
            key,
            Box::new(move || {
                Box::pin(async move {
                    let lease_key = LeaseKey::from_auth_binding(&auth_binding);
                    let _guard =
                        meerkat_core::acquire_auth_login_lifecycle_guard(&lease_key).await;
                    let previous = meerkat_core::rehydrate_durable_predecessor_for_mutation(
                        store.as_ref(),
                        &auth_lease,
                        &auth_binding,
                        chrono::Utc::now(),
                    )
                    .await
                    .map_err(|error| {
                        CredentialMutationError::AuthLifecycle(format!(
                            "durable credential predecessor rehydrate failed: {error}"
                        ))
                    })?;
                    flow.authority
                        .consume(
                            &flow.state,
                            &auth_binding,
                            flow.provider,
                            &flow.redirect_uri,
                        )
                        .map_err(flow_error)?;

                    let previous_lifecycle_restore =
                        auth_lease.capture_auth_lifecycle_restore_snapshot(&lease_key);
                    let previous_lifecycle = previous_lifecycle_restore.snapshot().clone();
                    let lifecycle_transition =
                        match meerkat_core::publish_token_lifecycle_acquired(
                            &auth_lease,
                            &auth_binding,
                            &tokens,
                        ) {
                            Ok(transition) => transition,
                            Err(error) => {
                                let cleanup = if previous.is_none() {
                                    auth_lease
                                        .release_credential_lifecycle(&lease_key)
                                        .err()
                                        .map(|cleanup| {
                                            format!(
                                                "; uncredentialed terminal lifecycle cleanup failed: {cleanup}"
                                            )
                                        })
                                        .unwrap_or_default()
                                } else {
                                    String::new()
                                };
                                return Err(CredentialMutationError::AuthLifecycle(format!(
                                    "AuthMachine lifecycle acquire failed after OAuth consume: {error}{cleanup}"
                                )));
                            }
                        };
                    let commit = PreparedCommit {
                        key: load_key.clone(),
                        lease_key,
                        previous,
                        previous_lifecycle,
                        previous_lifecycle_restore,
                        lifecycle_transition,
                    };
                    let marked =
                        match meerkat_core::mark_tokens_lifecycle_published_for_transition(
                            &commit.key,
                            &tokens,
                            &commit.lifecycle_transition,
                        ) {
                            Ok(marked) => marked,
                            Err(error) => {
                                return Err(compensated_error(
                                    store.as_ref(),
                                    &auth_lease,
                                    &commit,
                                    format!(
                                        "AuthMachine lifecycle marker handoff failed after OAuth consume: {error}"
                                    ),
                                )
                                .await);
                            }
                        };
                    if let Err(error) = store.save(&commit.key, &marked).await {
                        return Err(compensated_error(
                            store.as_ref(),
                            &auth_lease,
                            &commit,
                            format!("TokenStore save failed after OAuth consume: {error}"),
                        )
                        .await);
                    }
                    Ok(CredentialMutationOutcome::Persisted(marked))
                })
            }),
        )
        .await?;
    match outcome {
        CredentialMutationOutcome::Persisted(tokens) => Ok(tokens),
        CredentialMutationOutcome::Cleared => Err(CredentialMutationError::Operation(
            "browser-login transaction returned cleared outcome".to_string(),
        )),
    }
}

fn flow_error(error: OAuthFlowError) -> CredentialMutationError {
    CredentialMutationError::AuthLifecycle(error.to_string())
}

async fn compensated_error(
    store: &dyn TokenStore,
    auth_lease: &GeneratedAuthLeaseHandle,
    commit: &PreparedCommit,
    message: String,
) -> CredentialMutationError {
    match rollback_commit(store, auth_lease, commit).await {
        Ok(()) => {
            CredentialMutationError::Operation(format!("{message}; acquired lease rolled back"))
        }
        Err(rollback) => CredentialMutationError::AuthLifecycle(format!(
            "{message}; acquired lease rollback failed: {rollback}"
        )),
    }
}

async fn rollback_commit(
    store: &dyn TokenStore,
    auth_lease: &GeneratedAuthLeaseHandle,
    commit: &PreparedCommit,
) -> Result<(), String> {
    auth_lease
        .release_credential_lifecycle(&commit.lease_key)
        .map_err(|error| format!("AuthMachine lifecycle rollback release failed: {error}"))?;
    match &commit.previous {
        Some(previous) => {
            store
                .save(&commit.key, previous)
                .await
                .map_err(|error| format!("TokenStore rollback save failed: {error}"))?;
            if matches!(
                commit.previous_lifecycle.phase,
                Some(phase) if phase != AuthLeasePhase::Released
            ) && let Some(restored_transition) = meerkat_core::restore_token_lifecycle_snapshot(
                auth_lease,
                &commit.previous_lifecycle_restore,
            )
            .map_err(|error| format!("AuthMachine lifecycle rollback failed: {error}"))?
            {
                let restored = meerkat_core::mark_tokens_lifecycle_published_for_transition(
                    &commit.key,
                    previous,
                    &restored_transition,
                )
                .map_err(|error| format!("AuthMachine rollback marker handoff failed: {error}"))?;
                store
                    .save(&commit.key, &restored)
                    .await
                    .map_err(|error| format!("TokenStore rollback marker save failed: {error}"))?;
            }
        }
        None => {
            store
                .clear(&commit.key)
                .await
                .map_err(|error| format!("TokenStore rollback clear failed: {error}"))?;
        }
    }
    Ok(())
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use chrono::Duration;
    use meerkat_core::auth::token_store::{PersistedAuthMode, TokenStoreError};
    use meerkat_core::{BindingId, BindingOrigin, RealmId};
    use std::time::Duration as StdDuration;

    struct AuthMachineOwnedTestFlowAuthority {
        inner: crate::oauth_flow::OAuthFlowRegistry,
    }

    impl AuthMachineOwnedTestFlowAuthority {
        fn new() -> Self {
            Self {
                inner: crate::oauth_flow::OAuthFlowRegistry::new(StdDuration::from_secs(60)),
            }
        }
    }

    impl OAuthFlowAuthority for AuthMachineOwnedTestFlowAuthority {
        fn terminal_flow_state_is_authmachine_owned(&self) -> bool {
            true
        }

        fn start(
            &self,
            target: AuthBindingRef,
            provider: OAuthProviderIdentity,
            redirect_uri: String,
            pkce_verifier: String,
        ) -> Result<String, OAuthFlowError> {
            self.inner
                .start(target, provider, redirect_uri, pkce_verifier)
        }

        fn verify(
            &self,
            state: &str,
            target: &AuthBindingRef,
            provider: OAuthProviderIdentity,
            redirect_uri: &str,
        ) -> Result<crate::oauth_flow::OAuthFlowRecord, OAuthFlowError> {
            self.inner.verify(state, target, provider, redirect_uri)
        }

        fn consume(
            &self,
            state: &str,
            target: &AuthBindingRef,
            provider: OAuthProviderIdentity,
            redirect_uri: &str,
        ) -> Result<crate::oauth_flow::OAuthFlowRecord, OAuthFlowError> {
            self.inner.consume(state, target, provider, redirect_uri)
        }

        fn admit_device_code(
            &self,
            target: AuthBindingRef,
            provider: OAuthProviderIdentity,
            device_code: String,
            expires_in: StdDuration,
        ) -> Result<(), OAuthFlowError> {
            self.inner
                .admit_device_code(target, provider, device_code, expires_in)
        }

        fn verify_device_code(
            &self,
            device_code: &str,
            target: &AuthBindingRef,
            provider: OAuthProviderIdentity,
        ) -> Result<crate::oauth_flow::OAuthDeviceFlowRecord, OAuthFlowError> {
            self.inner.verify_device_code(device_code, target, provider)
        }

        fn begin_device_code_poll(
            &self,
            device_code: &str,
            target: &AuthBindingRef,
            provider: OAuthProviderIdentity,
        ) -> Result<crate::oauth_flow::OAuthDevicePollLease, OAuthFlowError> {
            self.inner
                .begin_device_code_poll(device_code, target, provider)
        }
    }

    fn binding() -> AuthBindingRef {
        AuthBindingRef {
            realm: RealmId::parse("global").expect("valid realm"),
            binding: BindingId::parse("openai").expect("valid binding"),
            profile: None,
            origin: BindingOrigin::Configured,
        }
    }

    fn tokens() -> PersistedTokens {
        let now = chrono::Utc::now();
        PersistedTokens {
            auth_mode: PersistedAuthMode::ChatgptOauth,
            primary_secret: Some("access-token".to_string()),
            refresh_token: Some("refresh-token".to_string()),
            id_token: None,
            expires_at: Some(now + Duration::hours(1)),
            last_refresh: Some(now),
            scopes: vec!["openid".to_string()],
            account_id: None,
            metadata: serde_json::Value::Null,
        }
    }

    #[tokio::test]
    async fn browser_commit_consumes_state_and_publishes_marked_credential() {
        let runtime = meerkat_runtime::MeerkatMachine::ephemeral();
        let authority: Arc<dyn OAuthFlowAuthority> =
            Arc::new(AuthMachineOwnedTestFlowAuthority::new());
        let auth_lease = runtime.generated_auth_lease_handle();
        let store = Arc::new(crate::auth_store::EphemeralTokenStore::new());
        let persistence = ProviderAuthPersistence::new(
            store.clone(),
            Arc::new(crate::auth_store::InMemoryCoordinator::new()),
        );
        let binding = binding();
        let state = authority
            .start(
                binding.clone(),
                OAuthProviderIdentity::OpenAiChatGpt,
                "http://127.0.0.1/callback".to_string(),
                "pkce-verifier".to_string(),
            )
            .expect("start browser flow");

        let committed = save_oauth_tokens_and_consume_browser_flow(
            persistence,
            auth_lease.clone(),
            binding.clone(),
            tokens(),
            BrowserOAuthFlowCommit {
                authority: Arc::clone(&authority),
                state: state.clone(),
                provider: OAuthProviderIdentity::OpenAiChatGpt,
                redirect_uri: "http://127.0.0.1/callback".to_string(),
            },
        )
        .await
        .expect("commit browser flow");

        assert_eq!(committed.primary_secret.as_deref(), Some("access-token"));
        let stored = store
            .load(&TokenKey::from_auth_binding(&binding))
            .await
            .expect("load committed tokens")
            .expect("committed tokens exist");
        assert_eq!(stored, committed);
        assert!(meerkat_core::auth::tokens_lifecycle_published(&stored));
        assert!(matches!(
            authority.verify(
                &state,
                &binding,
                OAuthProviderIdentity::OpenAiChatGpt,
                "http://127.0.0.1/callback"
            ),
            Err(OAuthFlowError::Missing | OAuthFlowError::RegistryProjectionMissing { .. })
        ));
        assert_eq!(
            auth_lease
                .snapshot(&LeaseKey::from_auth_binding(&binding))
                .phase,
            Some(AuthLeasePhase::Valid)
        );
    }

    struct FailingSaveStore;

    #[async_trait]
    impl TokenStore for FailingSaveStore {
        async fn load(&self, _key: &TokenKey) -> Result<Option<PersistedTokens>, TokenStoreError> {
            Ok(None)
        }

        async fn save(
            &self,
            _key: &TokenKey,
            _tokens: &PersistedTokens,
        ) -> Result<(), TokenStoreError> {
            Err(TokenStoreError::Io("injected save failure".to_string()))
        }

        async fn clear(&self, _key: &TokenKey) -> Result<(), TokenStoreError> {
            Ok(())
        }

        async fn list(&self) -> Result<Vec<TokenKey>, TokenStoreError> {
            Ok(Vec::new())
        }

        fn backend_name(&self) -> &'static str {
            "failing"
        }
    }

    #[tokio::test]
    async fn browser_commit_save_failure_rolls_back_acquired_lifecycle() {
        let runtime = meerkat_runtime::MeerkatMachine::ephemeral();
        let authority: Arc<dyn OAuthFlowAuthority> =
            Arc::new(AuthMachineOwnedTestFlowAuthority::new());
        let auth_lease = runtime.generated_auth_lease_handle();
        let persistence = ProviderAuthPersistence::new(
            Arc::new(FailingSaveStore),
            Arc::new(crate::auth_store::InMemoryCoordinator::new()),
        );
        let binding = binding();
        let state = authority
            .start(
                binding.clone(),
                OAuthProviderIdentity::OpenAiChatGpt,
                "http://127.0.0.1/callback".to_string(),
                "pkce-verifier".to_string(),
            )
            .expect("start browser flow");

        let error = save_oauth_tokens_and_consume_browser_flow(
            persistence,
            auth_lease.clone(),
            binding.clone(),
            tokens(),
            BrowserOAuthFlowCommit {
                authority: Arc::clone(&authority),
                state: state.clone(),
                provider: OAuthProviderIdentity::OpenAiChatGpt,
                redirect_uri: "http://127.0.0.1/callback".to_string(),
            },
        )
        .await
        .expect_err("injected token-store save must fail");

        assert!(error.to_string().contains("acquired lease rolled back"));
        assert!(matches!(
            authority.verify(
                &state,
                &binding,
                OAuthProviderIdentity::OpenAiChatGpt,
                "http://127.0.0.1/callback"
            ),
            Err(OAuthFlowError::Missing | OAuthFlowError::RegistryProjectionMissing { .. })
        ));
        let snapshot = auth_lease.snapshot(&LeaseKey::from_auth_binding(&binding));
        assert!(!snapshot.credential_present);
        assert!(matches!(
            snapshot.phase,
            None | Some(AuthLeasePhase::Released)
        ));
    }
}