alibi-core 0.9.0

Shared authentication contracts and backend-independent services for Alibi
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use crate::AdapterRecord;
use crate::field_policy::FieldValues;
use crate::types::AddTeamMemberResult;
use crate::user_validation::{PreparedUserCreation, UserValidationSource};
use crate::verification::{VerificationCreation, VerificationPublication, VerificationSnapshot};
use crate::{
    AuthError, AuthResult, AuthSchema, AuthStore, CreateAccount, CreateJwk, CreateMember,
    CreatePasskey, CreateSession, CreateUser, CreateVerification, Jwk, Member, Passkey, Team,
};
use async_trait::async_trait;
use std::any::Any;
use std::pin::Pin;
pub type BoxedTransactionValue = Box<dyn Any + Send>;

pub type TransactionFuture<'a> =
    Pin<Box<dyn Future<Output = AuthResult<BoxedTransactionValue>> + Send + 'a>>;

pub type TypedTransactionFuture<'a, T> = Pin<Box<dyn Future<Output = AuthResult<T>> + Send + 'a>>;

pub type TransactionWork<S> =
    dyn for<'tx> FnOnce(&'tx dyn AuthTransaction<S>) -> TransactionFuture<'tx> + Send;

#[async_trait]
pub trait AuthTransaction<S: AuthSchema>: Send + Sync {
    async fn check_scim_user_attributes(
        &self,
        _id: &str,
        _connection: &str,
        _attributes: &crate::store::ScimUserAttributes,
    ) -> AuthResult<()> {
        Err(AuthError::not_implemented(
            "Transactional SCIM User uniqueness checks are unsupported",
        ))
    }

    async fn lock_scim_user(
        &self,
        _id: &str,
        _connection: &str,
        _domain: &str,
        _attributes: Option<&crate::store::ScimUserAttributes>,
        _now: chrono::DateTime<chrono::Utc>,
    ) -> AuthResult<crate::store::ScimUserMutationContext> {
        Err(AuthError::not_implemented(
            "Transactional SCIM User mutation is unsupported",
        ))
    }
    async fn replace_scim_user(
        &self,
        _id: &str,
        _data: crate::store::ReplaceScimUser,
        _roles: bool,
    ) -> AuthResult<(
        crate::store::ScimUserRecord,
        Vec<crate::store::ScimProjectionPreparedUser>,
    )> {
        Err(AuthError::not_implemented(
            "Transactional SCIM User replacement is unsupported",
        ))
    }

    async fn create_scim_user(
        &self,
        _data: crate::store::CreateScimUser,
        _roles: bool,
    ) -> AuthResult<(
        crate::store::ScimUserRecord,
        Vec<crate::store::ScimProjectionPreparedUser>,
    )> {
        Err(AuthError::not_implemented(
            "Transactional SCIM User creation is unsupported",
        ))
    }

    async fn delete_scim_user(
        &self,
        _id: &str,
        _connection: &str,
        _domain: &str,
        _roles: bool,
    ) -> AuthResult<(String, Vec<crate::store::ScimProjectionPreparedUser>)> {
        Err(AuthError::not_implemented(
            "Transactional SCIM User deletion is unsupported",
        ))
    }
    async fn patch_scim_group(
        &self,
        _id: &str,
        _connection: &str,
        _domain: &str,
        _patch: crate::store::ScimGroupPatch,
        _roles: bool,
    ) -> AuthResult<(
        crate::store::ScimGroupRecord,
        Vec<crate::store::ScimProjectionPreparedUser>,
    )> {
        Err(AuthError::not_implemented(
            "Transactional SCIM Group patching is unsupported",
        ))
    }
    async fn replace_scim_group(
        &self,
        _id: &str,
        _data: crate::store::ReplaceScimGroup,
        _roles: bool,
    ) -> AuthResult<(
        crate::store::ScimGroupRecord,
        Vec<crate::store::ScimProjectionPreparedUser>,
    )> {
        Err(AuthError::not_implemented(
            "Transactional SCIM Group replacement is unsupported",
        ))
    }
    async fn delete_scim_group(
        &self,
        _id: &str,
        _connection: &str,
        _domain: &str,
        _roles: bool,
    ) -> AuthResult<Vec<crate::store::ScimProjectionPreparedUser>> {
        Err(AuthError::not_implemented(
            "Transactional SCIM Group deletion is unsupported",
        ))
    }
    async fn create_scim_group(
        &self,
        _data: crate::store::CreateScimGroup,
        _roles: bool,
    ) -> AuthResult<(
        crate::store::ScimGroupRecord,
        Vec<crate::store::ScimProjectionPreparedUser>,
    )> {
        Err(AuthError::not_implemented(
            "Transactional SCIM Groups are unsupported",
        ))
    }
    async fn fence_scim_connection(&self, _id: &str, _key: &str) -> AuthResult<()> {
        Err(AuthError::not_implemented(
            "Transactional SCIM fencing is unsupported",
        ))
    }
    async fn get_scim_connection_binding(
        &self,
        _id: &str,
    ) -> AuthResult<Option<crate::store::ScimConnectionBinding>> {
        Err(AuthError::not_implemented(
            "Transactional SCIM bindings are unsupported",
        ))
    }
    async fn scim_projection_candidates(
        &self,
        _domain: &str,
        _cursor: Option<&str>,
    ) -> AuthResult<Vec<crate::store::ScimProjectionCandidate>> {
        Err(AuthError::not_implemented(
            "Transactional SCIM candidates are unsupported",
        ))
    }
    async fn renew_scim_decommission_lease(
        &self,
        _binding: &crate::store::ScimConnectionBinding,
        _lease: &str,
    ) -> AuthResult<bool> {
        Err(AuthError::not_implemented(
            "Transactional SCIM leases are unsupported",
        ))
    }
    async fn advance_scim_decommission(
        &self,
        _binding: &crate::store::ScimConnectionBinding,
        _lease: &str,
        _cursor: Option<&str>,
        _count: usize,
        _more: bool,
    ) -> AuthResult<Option<crate::store::ScimConnectionBinding>> {
        Err(AuthError::not_implemented(
            "Transactional SCIM checkpoints are unsupported",
        ))
    }
    async fn reconcile_scim_identity(
        &self,
        _user_id: &str,
    ) -> AuthResult<crate::store::ScimIdentityState> {
        Err(AuthError::not_implemented(
            "Transactional SCIM identity is unsupported",
        ))
    }
    async fn delete_user_sessions(&self, _user_id: &str) -> AuthResult<()> {
        Err(AuthError::not_implemented(
            "Transactional session retirement is unsupported",
        ))
    }

    async fn prepare_scim_projection(
        &self,
        _domain: &str,
        _ids: &[String],
        _roles: bool,
    ) -> AuthResult<Vec<crate::store::ScimProjectionPreparedUser>> {
        Err(AuthError::not_implemented(
            "Transactional SCIM projection is unsupported",
        ))
    }
    async fn create_scim_projection_grant(
        &self,
        _data: crate::store::CreateScimProjectionGrant,
    ) -> AuthResult<()> {
        Err(AuthError::not_implemented(
            "Transactional SCIM grants are unsupported",
        ))
    }
    async fn remove_scim_projection_grants(
        &self,
        _domain: &str,
        _user_id: &str,
        _ids: &[String],
    ) -> AuthResult<()> {
        Err(AuthError::not_implemented(
            "Transactional SCIM grants are unsupported",
        ))
    }

    async fn get_user_by_email(&self, _email: &str) -> AuthResult<Option<S::User>> {
        Err(AuthError::NotImplemented(
            "Transactional user lookup is unsupported".into(),
        ))
    }
    async fn get_account(
        &self,
        _provider: &str,
        _account_id: &str,
    ) -> AuthResult<Option<S::Account>> {
        Err(AuthError::NotImplemented(
            "Transactional account lookup is unsupported".into(),
        ))
    }
    async fn update_user(&self, _id: &str, _update: crate::UpdateUser) -> AuthResult<S::User> {
        Err(AuthError::NotImplemented(
            "Transactional user updates are unsupported".into(),
        ))
    }
    async fn update_account(
        &self,
        _id: &str,
        _update: crate::UpdateAccount,
    ) -> AuthResult<S::Account> {
        Err(AuthError::NotImplemented(
            "Transactional account updates are unsupported".into(),
        ))
    }
    async fn get_user_by_email_record(
        &self,
        email: &str,
    ) -> AuthResult<Option<AdapterRecord<S::User>>> {
        self.get_user_by_email(email)
            .await?
            .map(AdapterRecord::physical)
            .transpose()
    }
    async fn get_account_record(
        &self,
        provider: &str,
        account_id: &str,
    ) -> AuthResult<Option<AdapterRecord<S::Account>>> {
        self.get_account(provider, account_id)
            .await?
            .map(AdapterRecord::physical)
            .transpose()
    }
    async fn update_user_record(
        &self,
        id: &str,
        update: crate::UpdateUser,
    ) -> AuthResult<AdapterRecord<S::User>> {
        AdapterRecord::physical(self.update_user(id, update).await?)
    }
    async fn update_account_record(
        &self,
        id: &str,
        update: crate::UpdateAccount,
    ) -> AuthResult<AdapterRecord<S::Account>> {
        AdapterRecord::physical(self.update_account(id, update).await?)
    }

    /// Lock and read a persisted enterprise provider on this transaction's connection.
    async fn lock_sso_provider(
        &self,
        _id: &str,
        _provider_id: &str,
    ) -> AuthResult<Option<crate::store::SsoProvider>> {
        Err(AuthError::NotImplemented(
            "Transactional SSO provider locking is unsupported".into(),
        ))
    }
    /// Apply a prepared provider update, including linked-account boundary protection.
    async fn update_sso_provider(
        &self,
        _update: crate::store::SsoProviderUpdate,
    ) -> AuthResult<Option<crate::store::SsoProvider>> {
        Err(AuthError::NotImplemented(
            "Transactional SSO provider updates are unsupported".into(),
        ))
    }
    /// Retire a provider and its identity namespace in this transaction.
    async fn delete_sso_provider_and_accounts(
        &self,
        _id: &str,
        _provider_id: &str,
    ) -> AuthResult<bool> {
        Err(AuthError::NotImplemented(
            "Transactional SSO provider deletion is unsupported".into(),
        ))
    }

    /// Read the provider verification column using the adapter's physical scalar
    /// rules. This is retained output, not authorization input; typed models
    /// continue to supply the canonical boolean accessor.
    async fn provider_verification_output(
        &self,
        _id: &str,
    ) -> AuthResult<Option<serde_json::Value>> {
        Ok(None)
    }

    /// Stage a session using the actual transaction-local model and creation hooks.
    async fn prepare_secondary_session_creation(
        &self,
        _input: CreateSession,
        _persist: bool,
    ) -> AuthResult<S::Session> {
        Err(AuthError::NotImplemented(
            "Transactional secondary session creation is unsupported".into(),
        ))
    }

    /// Stage an update against a trusted cached model using this transaction's hooks.
    async fn prepare_secondary_session_update(
        &self,
        _session: S::Session,
        _expires_at: Option<chrono::DateTime<chrono::Utc>>,
        _fields: FieldValues,
    ) -> AuthResult<Option<(S::Session, FieldValues)>> {
        Err(AuthError::NotImplemented(
            "Transactional secondary session updates are unsupported".into(),
        ))
    }
    /// Persist a staged update on the current connection; after hooks belong to commit.
    async fn complete_secondary_session_update(
        &self,
        _session: S::Session,
        _expires_at: Option<chrono::DateTime<chrono::Utc>>,
        _fields: FieldValues,
        _persist: bool,
    ) -> AuthResult<Option<S::Session>> {
        Err(AuthError::NotImplemented(
            "Transactional secondary session updates are unsupported".into(),
        ))
    }

    /// Read the managed signing keyring on this transaction's connection.
    async fn list_jwks(&self) -> AuthResult<Vec<Jwk>> {
        Err(AuthError::config(
            "Transactional JWKS reads are unsupported by this store",
        ))
    }
    /// Find a managed signing key on this transaction's connection.
    async fn get_jwk_by_id(&self, _id: &str) -> AuthResult<Option<Jwk>> {
        Err(AuthError::config(
            "Transactional JWKS reads are unsupported by this store",
        ))
    }
    /// Persist a newly generated managed signing key in this transaction.
    async fn create_jwk(&self, _data: CreateJwk) -> AuthResult<Jwk> {
        Err(AuthError::config(
            "Transactional JWKS writes are unsupported by this store",
        ))
    }

    /// Return a retained adapter record. The default is the physical model's
    /// serialized snapshot; initialized stores apply their declared output policy.
    async fn create_user_record(
        &self,
        create_user: CreateUser,
    ) -> AuthResult<AdapterRecord<S::User>> {
        AdapterRecord::physical(self.create_user(create_user).await?)
    }

    /// Return a retained adapter record. The default is the physical model's
    /// serialized snapshot; initialized stores apply their declared output policy.
    async fn create_user_with_source_record(
        &self,
        create_user: CreateUser,
        source: UserValidationSource,
    ) -> AuthResult<AdapterRecord<S::User>> {
        AdapterRecord::physical(self.create_user_with_source(create_user, source).await?)
    }

    /// Return a retained adapter record. The default is the physical model's
    /// serialized snapshot; initialized stores apply their declared output policy.
    async fn create_user_prepared_record(
        &self,
        prepared: PreparedUserCreation,
    ) -> AuthResult<AdapterRecord<S::User>> {
        AdapterRecord::physical(self.create_user_prepared(prepared).await?)
    }

    /// Return a retained adapter record. The default is the physical model's
    /// serialized snapshot; initialized stores apply their declared output policy.
    async fn get_user_by_id_record(&self, id: &str) -> AuthResult<Option<AdapterRecord<S::User>>> {
        self.get_user_by_id(id)
            .await?
            .map(AdapterRecord::physical)
            .transpose()
    }

    /// Return a retained adapter record. The default is the physical model's
    /// serialized snapshot; initialized stores apply their declared output policy.
    async fn create_account_record(
        &self,
        create_account: CreateAccount,
    ) -> AuthResult<AdapterRecord<S::Account>> {
        AdapterRecord::physical(self.create_account(create_account).await?)
    }

    /// Return a retained adapter record. The default is the physical model's
    /// serialized snapshot; initialized stores apply their declared output policy.
    async fn create_session_record(
        &self,
        create_session: CreateSession,
    ) -> AuthResult<AdapterRecord<S::Session>> {
        AdapterRecord::physical(self.create_session(create_session).await?)
    }

    /// Return a retained adapter record. The default is the physical model's
    /// serialized snapshot; initialized stores apply their declared output policy.
    async fn update_session_active_organization_record(
        &self,
        token: &str,
        organization_id: Option<&str>,
    ) -> AuthResult<AdapterRecord<S::Session>> {
        AdapterRecord::physical(
            self.update_session_active_organization(token, organization_id)
                .await?,
        )
    }

    /// Return a retained adapter record. The default is the physical model's
    /// serialized snapshot; initialized stores apply their declared output policy.
    async fn update_session_active_team_record(
        &self,
        token: &str,
        team_id: Option<&str>,
    ) -> AuthResult<AdapterRecord<S::Session>> {
        AdapterRecord::physical(self.update_session_active_team(token, team_id).await?)
    }

    /// Create through before hooks, optional physical persistence, secondary
    /// publication, then after hooks. Unsupported adapters must fail closed.
    async fn create_verification_record(
        &self,
        _data: VerificationCreation,
        _publication: VerificationPublication,
    ) -> AuthResult<Option<VerificationSnapshot>> {
        Err(AuthError::NotImplemented(
            "Verification publication phases are not supported by this store".into(),
        ))
    }
    /// Read a team within the authorized organization through this transaction.
    async fn get_team(&self, _organization_id: &str, _team_id: &str) -> AuthResult<Option<Team>> {
        Err(AuthError::NotImplemented(
            "Team lookup in a transaction is not supported by this store".into(),
        ))
    }
    /// Add an idempotent team membership under the same transaction's capacity check.
    async fn add_team_member(
        &self,
        _team_id: &str,
        _user_id: &str,
        _maximum: Option<f64>,
    ) -> AuthResult<AddTeamMemberResult> {
        Err(AuthError::NotImplemented(
            "Team admission in a transaction is not supported by this store".into(),
        ))
    }
    async fn create_member(&self, _member: CreateMember) -> AuthResult<Member> {
        Err(AuthError::NotImplemented(
            "Member creation in a transaction is not supported by this store".into(),
        ))
    }
    /// Update the already authenticated token through the transaction connection.
    async fn update_session_active_team(
        &self,
        _token: &str,
        _team_id: Option<&str>,
    ) -> AuthResult<S::Session> {
        Err(AuthError::NotImplemented(
            "Active team updates in a transaction are not supported by this store".into(),
        ))
    }
    async fn update_session_active_organization(
        &self,
        _token: &str,
        _organization_id: Option<&str>,
    ) -> AuthResult<S::Session> {
        Err(AuthError::NotImplemented(
            "Active organization updates in a transaction are not supported by this store".into(),
        ))
    }
    /// Read an existing registration owner through this transaction's connection.
    async fn get_user_by_id(&self, _id: &str) -> AuthResult<Option<S::User>> {
        Err(AuthError::NotImplemented(
            "User lookup in a transaction is not supported by this store".to_owned(),
        ))
    }
    /// Persist a verified passkey in the same transaction as its new session.
    async fn create_passkey(&self, _passkey: CreatePasskey) -> AuthResult<Passkey> {
        Err(AuthError::NotImplemented(
            "Passkey creation in a transaction is not supported by this store".to_owned(),
        ))
    }
    async fn create_user(&self, create_user: CreateUser) -> AuthResult<S::User>;
    /// Create with endpoint-owned identity provenance. The finalized instance
    /// applies its policy before adapter hooks. Raw adapters retain ordinary creation.
    async fn create_user_with_source(
        &self,
        create_user: CreateUser,
        _source: UserValidationSource,
    ) -> AuthResult<S::User> {
        self.create_user(create_user).await
    }
    /// Persist an admitted candidate without a second email normalization.
    /// Custom adapters serving validation-enabled instances must implement this.
    async fn create_user_prepared(&self, _prepared: PreparedUserCreation) -> AuthResult<S::User> {
        Err(AuthError::NotImplemented(
            "Prepared user creation is not supported by this transaction".into(),
        ))
    }
    async fn create_account(&self, create_account: CreateAccount) -> AuthResult<S::Account>;
    async fn create_session(&self, create_session: CreateSession) -> AuthResult<S::Session>;
    async fn create_verification(
        &self,
        _verification: CreateVerification,
    ) -> AuthResult<S::Verification> {
        Err(AuthError::NotImplemented(
            "Verification creation in a transaction is not supported by this store".to_owned(),
        ))
    }
}

/// How to handle callback errors after the database has committed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AfterCommitErrorPolicy {
    Propagate,
    Report,
}
#[async_trait]
pub trait TransactionStore<S: AuthSchema>: Send + Sync {
    async fn transaction_boxed(
        &self,
        work: Box<TransactionWork<S>>,
    ) -> AuthResult<BoxedTransactionValue>;
    async fn transaction_boxed_with_policy(
        &self,
        work: Box<TransactionWork<S>>,
        policy: AfterCommitErrorPolicy,
    ) -> AuthResult<BoxedTransactionValue> {
        if policy == AfterCommitErrorPolicy::Propagate {
            self.transaction_boxed(work).await
        } else {
            Err(AuthError::NotImplemented(
                "Reporting committed transaction hook failures is unsupported".into(),
            ))
        }
    }
}

/// # Errors
///
/// Propagates transaction or callback errors; also rejects an unexpected transaction result type.
pub async fn transaction<S, T, F>(store: &dyn AuthStore<S>, work: F) -> AuthResult<T>
where
    S: AuthSchema,
    T: Send + 'static,
    F: for<'tx> FnOnce(&'tx dyn AuthTransaction<S>) -> TypedTransactionFuture<'tx, T>
        + Send
        + 'static,
{
    transaction_with_policy(store, AfterCommitErrorPolicy::Propagate, work).await
}
/// Run typed work while selecting the treatment of errors after commit.
///
/// # Errors
/// Propagates transaction errors and rejects an unexpected result type.
pub async fn transaction_with_policy<S, T, F>(
    store: &dyn AuthStore<S>,
    policy: AfterCommitErrorPolicy,
    work: F,
) -> AuthResult<T>
where
    S: AuthSchema,
    T: Send + 'static,
    F: for<'tx> FnOnce(&'tx dyn AuthTransaction<S>) -> TypedTransactionFuture<'tx, T>
        + Send
        + 'static,
{
    let value = store
        .transaction_boxed_with_policy(
            Box::new(move |tx| {
                Box::pin(async move {
                    let result: BoxedTransactionValue = Box::new(work(tx).await?);
                    Ok(result)
                })
            }),
            policy,
        )
        .await?;

    value
        .downcast::<T>()
        .map(|boxed| *boxed)
        .map_err(|_| AuthError::internal("store returned an invalid transaction payload"))
}