pub struct Session { /* private fields */ }Expand description
One visitor’s session.
Implementations§
Source§impl Session
impl Session
Sourcepub const TOKEN_KEY: &'static str = "_token"
pub const TOKEN_KEY: &'static str = "_token"
Where the CSRF token lives, and the name of the form field that carries
it back. Laravel’s _token, so a template ported across looks familiar.
pub fn id(&self) -> &str
Sourcepub fn updated_at(&self) -> u64
pub fn updated_at(&self) -> u64
When this session was last written, in unix seconds.
Sourcepub fn is_dirty(&self) -> bool
pub fn is_dirty(&self) -> bool
Whether anything changed since the session was loaded.
The middleware uses this to avoid writing a session file for every anonymous request that never touched the session.
pub fn get(&self, key: &str) -> Option<&Json>
Sourcepub fn get_string(&self, key: &str) -> Option<String>
pub fn get_string(&self, key: &str) -> Option<String>
A value as a string, which is what most session data actually is.
pub fn has(&self, key: &str) -> bool
pub fn put(&mut self, key: impl Into<String>, value: impl Into<Json>)
Sourcepub fn forget(&mut self, key: &str) -> Option<Json>
pub fn forget(&mut self, key: &str) -> Option<Json>
Remove a key, handing back whatever was there.
Sourcepub fn flush(&mut self)
pub fn flush(&mut self)
Remove everything, keeping the id.
On logout prefer Session::invalidate, which also rotates the id.
pub fn all(&self) -> &BTreeMap<String, Json>
Sourcepub fn flash(&mut self, key: impl Into<String>, value: impl Into<Json>)
pub fn flash(&mut self, key: impl Into<String>, value: impl Into<Json>)
Store a value that survives exactly one further request.
The redirect-with-a-message pattern: a POST handler flashes status,
redirects, and the GET that follows renders it. By the request after
that, it is gone — so a refresh does not show the message twice.
Sourcepub fn age_flash(&mut self)
pub fn age_flash(&mut self)
Advance the flash lifecycle by one request.
Called by the session middleware after the handler has run: what was flashed last request has now been read and is dropped, and what was flashed this request takes its place.
Sourcepub fn regenerate(&mut self) -> &str
pub fn regenerate(&mut self) -> &str
Give this session a new id, keeping its contents.
This is the defence against session fixation. An attacker who can plant a cookie — through a subdomain, an open redirect, or a shared machine — hands the victim a session id he already knows, waits for the victim to log in, and then presents the same id to arrive inside the authenticated session. Rotating the id at the moment privileges change makes the id he planted worthless: it is no longer the id the session answers to.
crate::Guard::login calls this for exactly that reason.
Sourcepub fn invalidate(&mut self) -> &str
pub fn invalidate(&mut self) -> &str
Throw the contents away and rotate the id — what logout wants.
Sourcepub fn token(&mut self) -> String
pub fn token(&mut self) -> String
The CSRF token for this session, generated on first use.
Living in the session is what makes it available to templates: a view
renders session.get("_token") into a hidden field, and the csrf
middleware compares what comes back against this same value.
Sourcepub fn to_json(&self) -> Json
pub fn to_json(&self) -> Json
Serialize for a store. The id is the key, so it is not in the payload.