Skip to main content

Crate larust_sanctum

Crate larust_sanctum 

Source
Expand description

Laravel’s laravel/sanctum - narrowed to its bearer-token half, the part with no existing Larust equivalent at all. larust_auth::Auth<U> already covers Sanctum’s other job (a first-party frontend authenticating via session cookie) natively, so this crate doesn’t touch that path - it only adds what’s missing: a plain API client (no cookie jar, no browser) authenticating via Authorization: Bearer {token}.

A separate crate from larust-auth, deliberately. larust-auth currently has zero direct dependency on larust-orm/sqlx - every bit of persistence is delegated to the app’s own Authenticatable::find_for_auth impl, keeping the auth logic storage-agnostic. A personal_access_tokens table is new storage this functionality has to own, so it lives here instead, the same way larust-permissions/larust-notifications/larust-cache each own their own table rather than being folded into a crate that otherwise has none. This crate depends on larust-auth (for Authenticatable/AppError conventions), never the other way round.

Re-exported through larust_support::sanctum (see crates/ larust-support/src/lib.rs) so generated apps depend only on larust-support, never on this crate directly.

§Deliberately out of scope for this version

  • No token abilities/scopes (Sanctum’s can()/ability-string gating). A real separate feature layering on top of the existing larust_auth::Policy trait - not attempted here.
  • No SPA/stateful cookie mode. See this doc comment’s own opening paragraph - Auth<U> already covers it.
  • No auth:sanctum middleware-string recognition in xr convert. crates/larust-convert/src/routes.rs already blanket-defers every Route::middleware(...)->group(...) call, deliberately - this crate doesn’t special-case Sanctum’s own alias within that existing boundary, the same choice larust-permissions’s own doc comment already made for role:/permission: middleware strings.

Structs§

ApiAuth
Extracts the user identified by a valid Authorization: Bearer {token} header, or rejects with 401 - the bearer-token sibling to larust_auth::Auth<U>, never a replacement for it. A route picks whichever extractor matches how it’s actually authenticated, the same way a Laravel route explicitly chooses auth:sanctum vs auth:web middleware; Auth<U> gains no branch point for “no session, try a header instead.”

Functions§

create_token
Issues a new token for user, returning the plaintext value ("{id}|{plaintext}") - Laravel’s $user->createToken('name'). This exact string is the only time the plaintext is ever available; only its hash is stored, so it can’t be recovered from the database later (a stolen DB dump hands out no usable tokens). ttl mirrors larust_cache::put’s own optional-expiry convention - None for a token that never expires, matching Sanctum’s own default.
revoke_all_tokens_for
Revokes every token user currently holds - Laravel’s $user->tokens()->delete(), commonly used for a “log out everywhere” action.
revoke_token
Revokes one token by its row id (the id portion of the token string returned by create_token) - Laravel’s $user->tokens()->where('id', $id)->delete(). Not an error to revoke an id that doesn’t exist or was already revoked, same “no meaningful double-removal error” reasoning larust-notifications’s mark_as_read already applies.