use std::fmt;
#[cfg(not(target_arch = "wasm32"))]
use std::future::Future;
#[cfg(not(target_arch = "wasm32"))]
use std::pin::Pin;
use pocopine_core::ServerError;
#[cfg(not(target_arch = "wasm32"))]
use crate::context::RequestContext;
#[cfg(not(target_arch = "wasm32"))]
use crate::principal::Session;
#[cfg(not(target_arch = "wasm32"))]
use crate::user::AuthUser;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AuthError {
message: String,
}
impl AuthError {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl fmt::Display for AuthError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for AuthError {}
impl From<AuthError> for ServerError {
fn from(err: AuthError) -> Self {
ServerError::unauthorized(err.to_string())
}
}
pub type AuthResult<T> = Result<T, AuthError>;
#[cfg(not(target_arch = "wasm32"))]
pub type AuthFuture<'a, T> = Pin<Box<dyn Future<Output = AuthResult<T>> + Send + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
pub trait AuthProvider: Send + Sync {
fn authenticate<'a>(&'a self, ctx: &'a RequestContext) -> AuthFuture<'a, Option<AuthUser>>;
}
#[cfg(not(target_arch = "wasm32"))]
pub trait SessionStore: Send + Sync {
fn load<'a>(&'a self, session_id: &'a str) -> AuthFuture<'a, Option<Session>>;
fn save<'a>(&'a self, session: Session) -> AuthFuture<'a, ()>;
fn delete<'a>(&'a self, session_id: &'a str) -> AuthFuture<'a, ()>;
}