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 existinglarust_auth::Policytrait - not attempted here. - No SPA/stateful cookie mode. See this doc comment’s own opening
paragraph -
Auth<U>already covers it. - No
auth:sanctummiddleware-string recognition inxr convert.crates/larust-convert/src/routes.rsalready blanket-defers everyRoute::middleware(...)->group(...)call, deliberately - this crate doesn’t special-case Sanctum’s own alias within that existing boundary, the same choicelarust-permissions’s own doc comment already made forrole:/permission:middleware strings.
Structs§
- ApiAuth
- Extracts the user identified by a valid
Authorization: Bearer {token}header, or rejects with401- the bearer-token sibling tolarust_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 choosesauth:sanctumvsauth:webmiddleware;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).ttlmirrorslarust_cache::put’s own optional-expiry convention -Nonefor a token that never expires, matching Sanctum’s own default. - revoke_
all_ tokens_ for - Revokes every token
usercurrently holds - Laravel’s$user->tokens()->delete(), commonly used for a “log out everywhere” action. - revoke_
token - Revokes one token by its row id (the
idportion of the token string returned bycreate_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” reasoninglarust-notifications’smark_as_readalready applies.