Skip to main content

OneTimeSecret

Struct OneTimeSecret 

Source
pub struct OneTimeSecret { /* private fields */ }
Expand description

A TTL-bound secret that can be consumed at most once.

Typical use is an out-of-band one-time token: the server mints a value, hands it to the operator/user once (console line, invite link, e-mail), and the endpoint that accepts it calls consume. The check-and-execute window is a single Mutex critical section, so N concurrent requests carrying the same value produce exactly one true (compare-and-swap, not “check now, mark later”).

§Semantics

  • issue replaces the slot and re-arms it; only one secret is active at a time. Empty values are rejected with Error::Validation and leave the current state untouched.
  • consume returns true exactly once per issued secret, only while it is neither expired nor consumed. A wrong candidate consumes nothing, so the legitimate holder can still use the secret.
  • The clock is Instant: a TTL is wall-clock independent and survives system time adjustments; the store itself is process-local, so a consumed or expired secret dies with the process.

§Example

let setup = OneTimeSecret::new(Duration::from_secs(30 * 60));
setup.issue("install-token").unwrap();

assert_eq!(setup.state(), OneTimeSecretState::Pending);
assert!(!setup.consume("wrong-token"), "a wrong guess burns nothing");
assert!(setup.consume("install-token"));
assert!(!setup.consume("install-token"), "the second use fails");
assert_eq!(setup.state(), OneTimeSecretState::Consumed);

// A consumed token can still be recognized for replay diagnostics (410).
assert!(setup.matches("install-token"));

§Expiry

A secret is valid while less than the TTL has elapsed:

let expired = OneTimeSecret::new(Duration::ZERO);
expired.issue("too-late").unwrap();
assert_eq!(expired.state(), OneTimeSecretState::Expired);
assert!(!expired.consume("too-late"));
assert!(!expired.matches("too-late"));

(Use a zero TTL only in tests — a real secret needs a positive lifetime.)

Implementations§

Source§

impl OneTimeSecret

Source

pub fn new(ttl: Duration) -> Self

Create an empty slot whose secrets live for ttl.

A zero TTL expires the next issued secret immediately; that is useful in tests and useless in production, so pick a real lifetime.

Source

pub fn ttl(&self) -> Duration

Configured TTL.

Source

pub fn issue(&self, secret: impl Into<String>) -> Result<()>

Arm the slot with secret, replacing (and thereby revoking) any previous value.

The caller supplies the value — see the module-level docs for the RNG and hash-at-rest recipes.

An empty secret is rejected with Error::Validation because it would make consume("") succeed; the previous secret stays in place so a failed re-issue cannot silently extend or shorten the current one.

Source

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

Consume the slot if candidate matches the issued secret.

Atomic check-and-consume: among concurrent callers with the same candidate exactly one observes true. Returns false for an empty slot, a consumed secret, an expired secret, or a mismatch.

Source

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

Whether candidate is the issued secret, without consuming it.

Meant for classifying a failed consume — e.g. an installation endpoint that answers 410 Gone when the consumed token is replayed instead of 401. Do not use it as the authorization gate: it does not consume, so two concurrent requests could both pass it.

Source

pub fn state(&self) -> OneTimeSecretState

Current lifecycle state (diagnostics, tests, 401-vs-410 decisions).

Source

pub fn revoke(&self)

Drop the slot without consuming it (cancel an outstanding secret).

Trait Implementations§

Source§

impl Debug for OneTimeSecret

Source§

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

Never renders the secret itself, only the TTL and the lifecycle state.

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more