pub struct Token(/* private fields */);Expand description
A session token: 32 bytes of cryptographically secure randomness, held by the client as its proof of a session.
The raw token only ever travels between the client and the TokenStore.
Applications persist its hash instead, so a leaked session
database never contains a credential a client could present.
Implementations§
Source§impl Token
impl Token
Sourcepub fn new(bytes: [u8; 32]) -> Self
pub fn new(bytes: [u8; 32]) -> Self
Creates a token from raw bytes, typically inside a TokenStore that
has deserialized a client-presented token.
Sourcepub fn random() -> Self
pub fn random() -> Self
Generates a fresh random token.
§Panics
Panics when the platform’s source of cryptographically secure randomness fails.
Sourcepub fn decode(s: &str) -> Result<Self, DecodeError>
pub fn decode(s: &str) -> Result<Self, DecodeError>
Parses a token from its URL-safe base64 encoded form.
§Errors
Returns a DecodeError when s is not valid base64 or does not
decode to exactly 32 bytes.
Sourcepub fn encode(&self) -> String
pub fn encode(&self) -> String
Encodes the token as URL-safe base64, for a TokenStore to send to
the client.
Sourcepub fn hash(&self) -> TokenHash
pub fn hash(&self) -> TokenHash
Returns the TokenHash identifying this token’s session, safe for
the application to persist.
Sourcepub fn dangerous_as_array(&self) -> &[u8; 32]
pub fn dangerous_as_array(&self) -> &[u8; 32]
Exposes the raw token bytes.
Only a TokenStore serializing the token for the client should need
this; never persist the raw bytes server-side.