pub struct Auth { /* private fields */ }Expand description
Explicit authentication and session service backed by slabs_sessions, slabs_magic_links, and slabs_access_tokens.
Supports Argon2id password hashing, single-use 6-character magic links, HttpOnly browser
session cookies, SHA-256 hashed Authorization: Bearer tokens, and HMAC-SHA256 signing.
Implementations§
Source§impl Auth
impl Auth
Sourcepub fn new(db: Db, secret_key: impl Into<String>, cookie_secure: bool) -> Self
pub fn new(db: Db, secret_key: impl Into<String>, cookie_secure: bool) -> Self
Create an Auth service backed by db, using secret_key for HMAC signing and cookie_secure for the cookie Secure flag.
Sourcepub fn for_signing(secret_key: impl Into<String>, cookie_secure: bool) -> Self
pub fn for_signing(secret_key: impl Into<String>, cookie_secure: bool) -> Self
Create a stateless Auth handle for HMAC signing, token verification, and signed cookies without a database connection.
Return whether cookies emitted by this Auth instance should set the Secure attribute.
Sourcepub fn hash_password(&self, password: &str) -> SlabResult<String>
pub fn hash_password(&self, password: &str) -> SlabResult<String>
Hash a plaintext password with Argon2id and a random OS salt, returning a PHC string.
Sourcepub fn verify_password(&self, password: &str, password_hash: &str) -> bool
pub fn verify_password(&self, password: &str, password_hash: &str) -> bool
Verify a plaintext password against a stored Argon2id PHC hash string.
Sourcepub fn hash_token(&self, raw_token: &str) -> String
pub fn hash_token(&self, raw_token: &str) -> String
Compute the hex-encoded SHA-256 digest of a raw token or verification code.
Sourcepub fn generate_token(&self) -> String
pub fn generate_token(&self) -> String
Generate a cryptographically random 32-byte hex-encoded token.
Sourcepub fn generate_short_code(&self, len: usize) -> String
pub fn generate_short_code(&self, len: usize) -> String
Generate a random alphanumeric code of length len, omitting ambiguous 0, O, 1, and I characters.
Sourcepub fn sign(&self, payload: &str) -> String
pub fn sign(&self, payload: &str) -> String
Compute a hex-encoded HMAC-SHA256 signature for payload using the configured secret key.
Sourcepub fn verify_signature(&self, payload: &str, signature_hex: &str) -> bool
pub fn verify_signature(&self, payload: &str, signature_hex: &str) -> bool
Verify a hex-encoded HMAC-SHA256 signature for payload in constant time.
Sourcepub fn sign_token<T: Serialize>(
&self,
purpose: &str,
payload: &T,
ttl: Duration,
) -> SlabResult<String>
pub fn sign_token<T: Serialize>( &self, purpose: &str, payload: &T, ttl: Duration, ) -> SlabResult<String>
Mint a purpose-bound, expiring HMAC-signed token carrying payload.
Encodes {"p": purpose, "exp": unix_ts, "d": payload} as URL-safe base64 without padding
and appends .<hex_hmac_sig>.
Sourcepub fn verify_token<T: DeserializeOwned>(
&self,
purpose: &str,
token: &str,
) -> SlabResult<T>
pub fn verify_token<T: DeserializeOwned>( &self, purpose: &str, token: &str, ) -> SlabResult<T>
Verify a signed token produced by Auth::sign_token, enforcing purpose and expiration.
Returns HTTP 401 Unauthorized when the format, HMAC signature, encoding, payload shape,
purpose, or expiration check fails.
Sourcepub async fn create_session_in_tx(
&self,
tx: &mut Transaction<'_, Sqlite>,
user_id: &str,
role: &str,
ttl_days: i64,
) -> SlabResult<(Session, Cookie<'static>)>
pub async fn create_session_in_tx( &self, tx: &mut Transaction<'_, Sqlite>, user_id: &str, role: &str, ttl_days: i64, ) -> SlabResult<(Session, Cookie<'static>)>
Insert a new browser session inside an existing SQLite transaction and return its cookie.
Sourcepub async fn create_session(
&self,
user_id: &str,
role: &str,
ttl_days: i64,
) -> SlabResult<(Session, Cookie<'static>)>
pub async fn create_session( &self, user_id: &str, role: &str, ttl_days: i64, ) -> SlabResult<(Session, Cookie<'static>)>
Create and persist a new browser session in its own transaction and return its cookie.
Sourcepub async fn optional_session(
&self,
jar: &CookieJar,
) -> SlabResult<Option<Session>>
pub async fn optional_session( &self, jar: &CookieJar, ) -> SlabResult<Option<Session>>
Look up the active, unexpired Session for the request’s cookie jar, if present.
Sourcepub async fn require_session(&self, jar: &CookieJar) -> SlabResult<Session>
pub async fn require_session(&self, jar: &CookieJar) -> SlabResult<Session>
Require a valid browser session from jar, failing with HTTP 401 if unauthenticated.
Sourcepub async fn require_admin(&self, jar: &CookieJar) -> SlabResult<Session>
pub async fn require_admin(&self, jar: &CookieJar) -> SlabResult<Session>
Require a valid browser session with the "admin" role, failing with HTTP 401 or 403 otherwise.
Sourcepub async fn destroy_session(
&self,
jar: &CookieJar,
) -> SlabResult<Cookie<'static>>
pub async fn destroy_session( &self, jar: &CookieJar, ) -> SlabResult<Cookie<'static>>
Delete the current session from the database (if present) and return a removal cookie.
Build an HttpOnly, SameSite=Lax session cookie carrying raw_token.
Build an expired removal cookie that clears the browser session cookie.
Sourcepub async fn issue_magic_link(
&self,
email: &str,
purpose: &str,
ttl_minutes: i64,
) -> SlabResult<MagicLinkIssued>
pub async fn issue_magic_link( &self, email: &str, purpose: &str, ttl_minutes: i64, ) -> SlabResult<MagicLinkIssued>
Issue an expiring 6-character magic link code and store its SHA-256 hash in slabs_magic_links.
Sourcepub async fn consume_magic_link(
&self,
raw_code: &str,
) -> SlabResult<MagicLinkConsumed>
pub async fn consume_magic_link( &self, raw_code: &str, ) -> SlabResult<MagicLinkConsumed>
Atomically consume a valid, unexpired magic link code via DELETE ... RETURNING so each code works once.
Sourcepub async fn cleanup_expired_magic_links(&self) -> SlabResult<u64>
pub async fn cleanup_expired_magic_links(&self) -> SlabResult<u64>
Delete all expired magic link rows and return the number of rows removed.
Sourcepub async fn issue_access_token(
&self,
user_id: &str,
role: &str,
description: &str,
permission: &str,
) -> SlabResult<AccessTokenIssued>
pub async fn issue_access_token( &self, user_id: &str, role: &str, description: &str, permission: &str, ) -> SlabResult<AccessTokenIssued>
Issue a personal access token prefixed with slb_ and store its SHA-256 hash in slabs_access_tokens.
Sourcepub async fn optional_auth(
&self,
jar: &CookieJar,
headers: &HeaderMap,
) -> SlabResult<Option<Principal>>
pub async fn optional_auth( &self, jar: &CookieJar, headers: &HeaderMap, ) -> SlabResult<Option<Principal>>
Authenticate via Authorization: Bearer <token> or the slabs_session cookie.
When a Bearer header is present, this method validates only that token and returns Ok(None) if it is invalid.
Sourcepub async fn require_auth(
&self,
jar: &CookieJar,
headers: &HeaderMap,
) -> SlabResult<Principal>
pub async fn require_auth( &self, jar: &CookieJar, headers: &HeaderMap, ) -> SlabResult<Principal>
Require authentication via either session cookie or Authorization: Bearer token.
Sourcepub async fn require_write_auth(
&self,
jar: &CookieJar,
headers: &HeaderMap,
) -> SlabResult<Principal>
pub async fn require_write_auth( &self, jar: &CookieJar, headers: &HeaderMap, ) -> SlabResult<Principal>
Require an authenticated Principal with "write" permission.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Auth
impl !UnwindSafe for Auth
impl Forget for Auth
impl Freeze for Auth
impl Move for Auth
impl Send for Auth
impl Sync for Auth
impl Unpin for Auth
impl UnsafeUnpin for Authwhere
*const ArcInner<PoolInner<Sqlite>> is !null: UnsafeUnpin,
PhantomData<ArcInner<PoolInner<Sqlite>>>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more