Sessions and CSRF
A session cookie signs an operator in. Alongside the identity it carries a CSRF token, flash messages and a data blob.
Send the CSRF token
Every POST, PUT, PATCH and DELETE on the admin mount carries the
session’s token, in the _csrf field or the X-CSRF-Token header. Descriptor
forms and htmx requests carry it already. A hand-written form includes it:
<form method="post" action="{{ action }}">
{% include "_csrf.html" %}
...
</form>
A scripted request sends the header; a template exposes the token as
shell.csrf_token:
<button type="button" data-acme-publish data-csrf="{{ shell.csrf_token }}">Publish</button>
document.addEventListener('click', function (e) {
var button = e.target.closest('[data-acme-publish]');
if (!button) return;
fetch(button.dataset.action, {
method: 'POST',
credentials: 'same-origin',
headers: { 'X-CSRF-Token': button.dataset.csrf }
});
});
A request without the token renders a 403 “Request blocked” page. Login and
first-run setup have no session and need no token.
Read an upload
use laterite_admin::VerifiedUpload;
async fn import(mut upload: VerifiedUpload) -> Result<Response, AdminError> {
while let Some(field) = upload.next_field().await? {
// ... stream the field ...
}
Ok(Redirect::to("/admin/import").into_response())
}
An upload’s form puts the token in its first field; VerifiedUpload checks
it while reading and offers the remaining fields as Multipart does. Take an
upload with it, never with a bare multipart parser.
Load scripts as assets
Every admin response carries these; a handler that sets one of its own keeps
its value. Inline scripts and handlers are refused: a screen binds its
behaviour from a script file and a data- attribute.
| Header | Value |
|---|---|
Content-Security-Policy | Scripts and fonts from the admin’s own assets; inline styles allowed; images and media also from https: |
X-Content-Type-Options | nosniff |
X-Frame-Options | SAMEORIGIN |
Referrer-Policy | same-origin |
Permissions-Policy | camera=(), microphone=(), geolocation=(), payment=() |
Cross-Origin-Opener-Policy | same-origin |
Strict-Transport-Security | max-age=31536000; includeSubDomains, with secure_cookie = true |
<button type="button" data-acme-refresh>Refresh</button>
document.addEventListener('click', function (e) {
if (e.target.closest('[data-acme-refresh]')) location.reload();
});
Flash a message
use laterite_admin::{FlashLevel, SessionHandle};
use laterite_core::t;
async fn save(Extension(session): Extension<SessionHandle>, /* ... */) -> Response {
// ... persist ...
session.push_flash(FlashLevel::Success, t!("Settings saved."));
Redirect::to("/admin/settings").into_response()
}
| Call | Description |
|---|---|
push_flash(level, text) | Shown on the next full page, across a redirect, then cleared. |
push_flash_sticky(level, text) | Shown until dismissed. |
FlashLevel | Success, Error, Info. |
Change a password
use laterite_core::Actor;
auth.change_password(
user.id,
&new_password,
Some(&session_token),
&Actor::user(user.id, &user.username),
)
.await?;
| Argument | Value |
|---|---|
user_id | The account whose password changes. |
new_password | Refused under password_policy.min_length (default 8). |
keep_token | The session to keep signed in, or None to end all of them. |
actor | Who made the change, recorded on the audit log. Actor::system("lat admin reset-password") for a process. |
AuthService::password_changed_at(user_id) returns when it last changed, or
None if never recorded.
An operator’s own change, from Preferences → Password, goes through:
auth.change_own_password(user.id, ¤t, &new_password, &session_token, &ctx)
.await?;
| Error | Returned when |
|---|---|
AuthError::InvalidCredentials | current does not match. Counts as a failed sign-in. |
AuthError::TooManyAttempts | The account is locked out. |
AuthError::Refused | The new password is under password_policy.min_length. |
A change through this call is also on the access log as password_changed.
A password made for an operator is temporary:
auth.require_password_change(user.id).await?;
Every admin request then redirects to Preferences → Password until the
operator sets their own. lat admin create --generate and
lat admin reset-password --generate mark the account the same way.
Purge expired sessions
Expired sessions and stay-signed-in credentials are removed for you, and on demand:
lat admin purge
let purged = auth.purge_expired().await?;
println!(
"{} sessions, {} credentials, {} access log rows",
purged.sessions, purged.remember_tokens, purged.access_log
);
Access log rows older than access_log_retention_days go in the same sweep.
Sign out
auth.logout_with(&session_token, &ctx).await?;
The sign-out is on the access log as logout, from the address and client in
ctx. auth.logout(&session_token) records it with neither.
Handle an expired session
Every guarded response carries when the session ends, and a page asks for it on demand:
X-Lat-Session-Ends: 1761307200
GET /admin/session
{"ends":1761307200}
POST /admin/session, with the request token, renews the session to its full
idle window and answers the same way.
A request past the deadline is answered by the kind of request it was:
| Request | Answer |
|---|---|
| Page | A redirect to the login page with ?ended=expired. |
| htmx | 401 with HX-Trigger: lat:session-expired; the admin page signs in again in place and retries the request. |
Scripted (X-Requested-With: fetch) | 401. |
A scripted sign-in posts the login form with X-Requested-With: fetch and
gets JSON back:
POST /admin/login
X-Requested-With: fetch
username=ada&password=...
{"ok": true, "csrf": "<the new request token>", "ends": 1761314400}
{"ok": false, "error": "Invalid username or password."}
A script of your own that posts after a re-login reads the token from the
_csrf field of any rendered form, or from the hx-headers attribute on
body; both are updated.
The page’s dialog and warning announce lat:session:expired and
lat:session:renewed on #lat-session.
Read the sign-out notice
A redirect to the login screen carries ?ended=<code> when the session was
ended for a reason the login page can show; an htmx request gets
HX-Redirect with no body, except for expired (see above).
| Code | Shown when |
|---|---|
expired | Inactivity, or the session’s ceiling. |
password_changed | AuthService::change_password ended every other session. |
deactivated | The account was deactivated. |
signed_out_elsewhere | One device, or every other device, was signed out. |