Expand description
Session-backed authentication and lightweight authorization - Laravel’s
Auth facade, auth/guest middleware, and authorize() helper,
re-exported through larust_support::auth (see
crates/larust-support/src/lib.rs) so generated apps depend only on
larust-support, never on this crate directly.
Structs§
- Auth
- Extracts the currently authenticated user, or rejects with 401 if there
isn’t one - for handlers that require a logged-in user and want the
Useritself, not just a yes/no check. Pair with therequire_authmiddleware (which redirects a guest to the login page) on routes meant for browsers; useAuth<U>alone on routes meant to fail loudly instead (e.g. an API endpoint).
Traits§
- Authenticatable
- Implemented by an app’s
Usermodel (or whatever it calls its authenticatable type) solarust_support::auth’s guard functions and thecrate::Authextractor can look a user up from the id stored in the session. - Policy
- A per-resource authorization policy (Laravel’s Policy classes), hand-
implemented once per model - the same shape as
crate::AuthenticatableonUser: a small trait the app implements directly, not macro-generated or auto-discovered, so a typo in an ability name is a compile error rather than a silently-always-false runtime lookup.
Functions§
- authorize
- Converts a boolean authorization check into a 403 on failure (Laravel’s
$this->authorize(...)controller helper). Not aGate-style runtime registry - write the actual check as a plain typed method on your own model (impl Post { pub fn can_update(&self, user: &User) -> bool { self.user_id == user.id } }) and convert it at the call site:authorize(post.can_update(&user))?;. A typo in an ability name this way is a compile error, not a silently-always-false runtime lookup. - check
- Whether the current session is authenticated (Laravel’s
Auth::check()). Operates on the default ("web") guard; for a named second guard, usecheck_forinstead. - check_
for check’s named-guard counterpart (Laravel’sAuth::guard('admin')->check()).- hash_
password - Hashes a plaintext password for storage (Laravel’s
Hash::make()). - id
- The current session’s authenticated user id, if any (Laravel’s
Auth::id()). Operates on the default ("web") guard; for a named second guard, useid_forinstead. - id_for
id’s named-guard counterpart (Laravel’sAuth::guard('admin')->id()).- login
- Logs a user in (Laravel’s
Auth::login($user)): rotates the session ID before storing the authenticated user, so a session token an attacker captured pre-login (session fixation) can’t be reused to inherit the now-authenticated session - then storesAuthenticatable::auth_id, underU::GUARD’s own session slot, for later requests to look the user back up viauser/thecrate::Authextractor. Logging in as a second guard (e.g.AdminafterUser) doesn’t disturb the first guard’s own login state - each has its own slot. - logout
- Logs the current user out (Laravel’s
Auth::logout()). Flushes the entire session, not just the auth key - a clean logout should also invalidate anything else tied to that session (e.g. the CSRF token), not leave it half-authenticated. Operates on the default ("web") guard; for a named second guard, uselogout_forinstead, which clears only that guard’s own slot and leaves the rest of the session - including any other guard’s login state - untouched. - logout_
for logout’s named-guard counterpart: removes onlyU::GUARD’s own session slot (Laravel’sAuth::guard('admin')->logout()), leaving the rest of the session - including any other guard’s login state - intact.- redirect_
authenticated - The inverse of
require_auth: bounces an already-logged-in user away from guest-only routes (Laravel’sguestmiddleware - typically wrapped around/login//register) to"/", rather than running the handler. - redirect_
authenticated_ for redirect_authenticated’s named-guard counterpart - bounces a user already logged in asU::GUARDaway from that guard’s own guest-only routes (e.g./admin/login), regardless of whether some other guard is also logged in.- require_
auth - Redirects a guest to
"login"(falling back to a hardcoded/login, with a warning, if that route name isn’t registered - the same degrade patternlarust_support::redirect()->route()already uses) rather than running the handler. Pair with routes that require a logged-in user (Laravel’sauthmiddleware); use thecrate::Authextractor instead on routes that should fail with a 401 rather than redirect. - require_
auth_ for require_auth’s named-guard counterpart (Laravel’sAuth::guard('admin')protecting a route group) - redirects unlessU::GUARDspecifically is logged in, regardless of whether some other guard is. Used asrequire_auth_for::<Admin>, e.g.r.middleware(axum::middleware::from_fn(require_auth_for::<Admin>)).- user
- The current session’s authenticated user, if any (Laravel’s
Auth::user()), resolved throughU::GUARD’s own session slot - souser::<User>(&session)anduser::<Admin>(&session)read independent login state even though they share oneSession.Ok(None)covers both “not logged in” and “logged in as a user id that no longer exists” - verify_
password - Checks a plaintext password against a stored hash (Laravel’s
Hash::check()). ReturnsOk(false)for a wrong password - only a malformed/corrupt stored hash is anErr, since that’s the only case that isn’t a normal, expected verification outcome.