Skip to main content

Token

Struct Token 

Source
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

Source

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.

Source

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.

Source

pub fn with_created_at(self, at: u64) -> Self

Source

pub fn with_expires_at(self, at: Option<u64>) -> Self

Source

pub fn with_last_used_at(self, at: Option<u64>) -> Self

Source

pub fn id(&self) -> &str

Source

pub fn identity(&self) -> &Identity

Who the token speaks for.

Source

pub fn name(&self) -> &str

What the person called it — “iPhone”, “CI deploy key”.

Purely so a revocation screen lists something a human recognises.

Source

pub fn scopes(&self) -> &Scopes

Source

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.

Source

pub fn created_at(&self) -> u64

Source

pub fn expires_at(&self) -> Option<u64>

Source

pub fn last_used_at(&self) -> Option<u64>

Source

pub fn set_last_used_at(&mut self, at: u64)

Source

pub fn can(&self, scope: &str) -> bool

Whether the token grants scope. See Scopes::can.

Source

pub fn is_expired(&self) -> bool

A token with no expiry never expires — that is what None means.

Source

pub fn is_expired_at(&self, now: u64) -> bool

Source

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.

Trait Implementations§

Source§

impl Clone for Token

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Token

Redacted by hand rather than derived.

The digest is the one value that verifies a token. {:?} on a request’s extensions, or on a store, is exactly the sort of thing that ends up in a log file, and a log file is exactly where it must not be.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Token

§

impl RefUnwindSafe for Token

§

impl Send for Token

§

impl Sync for Token

§

impl Unpin for Token

§

impl UnsafeUnpin for Token

§

impl UnwindSafe for Token

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.