pub mod mock_authn;
pub mod mock_clock;
pub mod mock_random;
pub mod mock_refresh_store;
pub mod mock_tracing;
#[cfg(feature = "authz")]
pub mod mock_policy;
#[cfg(feature = "local-idp")]
pub mod local_idp;
#[cfg(feature = "testing-oauth")]
pub mod oauth_wiremock;
pub use mock_authn::{MockFactorStore, MockIdentityStore, MockStoreError};
pub use mock_clock::MockClock;
pub use mock_random::MockRng;
pub use mock_refresh_store::{MemoryRefreshStoreError, MemoryRefreshTokenStore};
pub use axess_identity::testing::MockResolver;
pub fn user_record(label: &str, tenant_label: &str) -> crate::authn::types::User {
let now = chrono::Utc::now();
crate::authn::types::User {
id: axess_identity::testing::user(label),
tenant_id: axess_identity::testing::tenant(tenant_label),
identifier: label.into(),
display_name: label.into(),
status: crate::authn::types::EntityState::Active,
webauthn_id: None,
created_by: crate::authn::ids::UserId::system(),
created_at: now,
updated_by: crate::authn::ids::UserId::system(),
updated_at: now,
}
}
pub fn tenant_record(label: &str) -> crate::authn::types::Tenant {
let now = chrono::Utc::now();
crate::authn::types::Tenant {
id: axess_identity::testing::tenant(label),
identifier: label.into(),
display_name: label.into(),
status: crate::authn::types::EntityState::Active,
created_by: crate::authn::ids::UserId::system(),
created_at: now,
updated_by: crate::authn::ids::UserId::system(),
updated_at: now,
}
}
pub fn test_session() -> crate::session::extractor::AuthSession {
use crate::session::{
data::SessionData,
id::SessionId,
layer::{SessionHandle, SessionInner},
};
use std::sync::Arc;
use tokio::sync::RwLock;
let inner = SessionInner {
id: SessionId::new(&axess_rng::SystemRng),
data: SessionData::default(),
modified: false,
regenerate: false,
pre_cycle_id: None,
pending_fingerprint: None,
max_custom_bytes: 64 * 1024,
};
crate::session::extractor::AuthSession(SessionHandle(Arc::new(RwLock::new(inner))))
}
pub use session_ext::AuthSessionTestExt;
mod session_ext {
use crate::authn::factor::FactorKind;
use crate::authn::ids::{TenantId, UserId};
use crate::session::extractor::AuthSession;
use chrono::{DateTime, Utc};
use std::sync::Arc;
pub trait AuthSessionTestExt {
fn set_authenticated(
&self,
user_id: UserId,
tenant_id: TenantId,
authn_time: DateTime<Utc>,
) -> impl std::future::Future<Output = ()> + Send;
fn begin_authenticating(
&self,
user_id: UserId,
tenant_id: TenantId,
method_name: Arc<str>,
factors: Vec<FactorKind>,
) -> impl std::future::Future<Output = ()> + Send;
fn advance_factor(
&self,
kind: &FactorKind,
authn_time: DateTime<Utc>,
) -> impl std::future::Future<Output = ()> + Send;
fn record_attempt_at(
&self,
now: DateTime<Utc>,
) -> impl std::future::Future<Output = ()> + Send;
}
impl AuthSessionTestExt for AuthSession {
async fn set_authenticated(
&self,
user_id: UserId,
tenant_id: TenantId,
authn_time: DateTime<Utc>,
) {
AuthSession::set_authenticated(self, user_id, tenant_id, authn_time).await;
}
async fn begin_authenticating(
&self,
user_id: UserId,
tenant_id: TenantId,
method_name: Arc<str>,
factors: Vec<FactorKind>,
) {
let steps = factors
.into_iter()
.map(crate::authn::factor::FactorStep::Required)
.collect();
AuthSession::begin_authenticating(self, user_id, tenant_id, method_name, steps).await;
}
async fn advance_factor(&self, kind: &FactorKind, authn_time: DateTime<Utc>) {
AuthSession::advance_factor(self, kind, authn_time).await;
}
async fn record_attempt_at(&self, now: DateTime<Utc>) {
AuthSession::record_attempt_at(self, now).await;
}
}
}