pub struct Token { /* private fields */ }Expand description
One issued token, as the store keeps it: metadata plus a digest.
The secret is not in here and never was. A store hands these back, the
middleware attaches one to the request, and a handler reads it with
req.api_token().
Implementations§
Source§impl Token
impl Token
Sourcepub fn new(
id: impl Into<String>,
identity: Identity,
name: impl Into<String>,
digest: [u8; 32],
scopes: Scopes,
) -> Self
pub fn new( id: impl Into<String>, identity: Identity, name: impl Into<String>, digest: [u8; 32], scopes: Scopes, ) -> Self
Rebuild a token from a stored row.
This is the constructor an application’s own TokenStore uses when it
reads a record back out of its table. Issuing a new token goes through
NewToken, which is the only place a secret is generated.
Sourcepub fn with_id(self, id: impl Into<String>) -> Self
pub fn with_id(self, id: impl Into<String>) -> Self
Give the token a different id.
For a store whose table allocates the key itself: create returns the
token it actually stored, and the plain-text credential is assembled
from that id.
pub fn with_created_at(self, at: u64) -> Self
pub fn with_expires_at(self, at: Option<u64>) -> Self
pub fn with_last_used_at(self, at: Option<u64>) -> Self
pub fn id(&self) -> &str
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
What the person called it — “iPhone”, “CI deploy key”.
Purely so a revocation screen lists something a human recognises.
pub fn scopes(&self) -> &Scopes
Sourcepub fn digest(&self) -> &[u8; 32]
pub fn digest(&self) -> &[u8; 32]
The stored SHA-256 of the secret, for a store to persist.
Safe to write to a database column; not safe to write to a log. It proves nothing on its own, but it is the only value that verifies a token, so anywhere it leaks is somewhere it can be replayed against.
pub fn created_at(&self) -> u64
pub fn expires_at(&self) -> Option<u64>
pub fn last_used_at(&self) -> Option<u64>
pub fn set_last_used_at(&mut self, at: u64)
Sourcepub fn can(&self, scope: &str) -> bool
pub fn can(&self, scope: &str) -> bool
Whether the token grants scope. See Scopes::can.
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
A token with no expiry never expires — that is what None means.
pub fn is_expired_at(&self, now: u64) -> bool
Sourcepub fn matches_secret(&self, secret: &str) -> bool
pub fn matches_secret(&self, secret: &str) -> bool
Whether secret is the one this token was issued with.
Constant time: the digest is compared with constant_time_eq, so how
long the check took says nothing about how many bytes were right.