Skip to main content

Token

Struct Token 

Source
pub struct Token(/* private fields */);
Expand description

Validated bearer token.

Debug output is redacted. Use Self::verify for inbound checks. Use Self::expose only when the raw value is required.

§Example

use solti_model::Token;

let token = Token::new("secret").unwrap();

assert!(token.verify("secret"));
assert!(!token.verify("other"));
assert_eq!(format!("{token:?}"), "Token(***redacted***)");

Implementations§

Source§

impl Token

Source

pub fn new(token: impl Into<String>) -> ModelResult<Self>

Wraps a raw token.

Surrounding whitespace is trimmed.

§Errors

Returns ModelError::Invalid when the trimmed value is empty.

§Example
use solti_model::Token;

let token = Token::new("  secret\n").unwrap();
assert_eq!(token.expose(), "secret");
Source

pub fn generate() -> ModelResult<Self>

Generates a random token.

The token uses 256 bits from the operating system entropy source. It uses unpadded base64url and starts with solti_agt_. The token is not persisted.

§Errors

Returns ModelError::Invalid when the entropy source is unavailable.

§Example
use solti_model::Token;

let token = Token::generate()?;
assert!(token.expose().starts_with("solti_agt_"));
assert!(token.verify(token.expose()));
Source

pub fn from_env(var: &str) -> ModelResult<Self>

Reads a token from an environment variable.

§Errors

Returns ModelError::Invalid when the variable is absent or the value is empty.

§Example
use solti_model::Token;

let token = Token::from_env("SOLTI_AGENT_TOKEN")?;
Source

pub fn from_file(path: impl AsRef<Path>) -> ModelResult<Self>

Reads a token from a UTF-8 file.

Surrounding whitespace is trimmed.

§Errors

Returns ModelError::Invalid when the file cannot be read or the value is empty.

§Example
use solti_model::Token;

let token = Token::from_file("/run/secrets/solti-agent-token")?;
Source

pub fn expose(&self) -> &str

Returns the raw token.

Inbound verification should use Self::verify.

Source

pub fn verify(&self, candidate: &str) -> bool

Verifies a candidate value.

The comparison is constant-time for equal-length strings. A length mismatch returns false.

§Example
use solti_model::Token;

let token = Token::new("secret").unwrap();
assert!(token.verify("secret"));
assert!(!token.verify("Secret"));

Trait Implementations§

Source§

impl Clone for Token

Source§

fn clone(&self) -> Token

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

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.