Skip to main content

TokenValidator

Struct TokenValidator 

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

Token validator.

Holds the NonceBook (LRU+TTL nonce store) and the TokenPolicyTable (per-vendor policy lookup). The validator is Send + Sync so it can sit behind an Arc and be shared across threads and requests without locking.

§Example

use std::time::Duration;
use stygian_charon::token_lifecycle::{
    ChallengeClass, TokenContract, TokenPolicyTable, TokenValidator,
};
use stygian_charon::vendor_classifier::VendorId;

let policy = TokenPolicyTable::with_builtin_defaults();
let validator = TokenValidator::new(
    stygian_charon::token_lifecycle::NonceBook::with_defaults(),
    policy,
);
let contract = TokenContract {
    token_id: "x".to_string(),
    issued_at_unix_secs: 0,
    ttl: Duration::from_mins(5),
    nonce: "n".to_string(),
    vendor_family: VendorId::Unknown,
    challenge_class: ChallengeClass::None,
    single_use: false,
    bound_session: None,
    description: String::new(),
};
let outcome = validator.validate(&contract, None, 0);
assert!(outcome.is_ok());

Implementations§

Source§

impl TokenValidator

Source

pub const fn new(nonce_book: NonceBook, policy: TokenPolicyTable) -> Self

Build a validator with an explicit nonce book and policy table.

§Example
use std::num::NonZeroUsize;
use std::time::Duration;
use stygian_charon::token_lifecycle::{NonceBook, TokenPolicyTable, TokenValidator};

let validator = TokenValidator::new(
    NonceBook::new(NonZeroUsize::new(8).expect("non-zero"), Duration::from_mins(1)),
    TokenPolicyTable::with_builtin_defaults(),
);
assert_eq!(validator.policy().len(), 11);
Source

pub fn with_defaults(policy: TokenPolicyTable) -> Self

Build a validator with the default NonceBook::with_defaults() nonce book and the supplied policy table.

§Example
use stygian_charon::token_lifecycle::{TokenPolicyTable, TokenValidator};

let validator = TokenValidator::with_defaults(TokenPolicyTable::with_builtin_defaults());
assert!(validator.nonce_book().is_empty());
Source

pub const fn nonce_book(&self) -> &NonceBook

Borrow the nonce book.

Source

pub const fn policy(&self) -> &TokenPolicyTable

Borrow the policy table.

Source

pub fn validate( &self, contract: &TokenContract, session_id: Option<&str>, now_unix_secs: u64, ) -> ValidationOutcome

Validate a TokenContract against the supplied session_id (when the submission carries one) and the supplied now_unix_secs clock.

On accept, the nonce is recorded in the NonceBook (so the next submission is rejected as a replay) and the outcome’s consumed flag is true. On reject, the outcome is ValidationOutcome::Rejected with the structured InvalidationReason the runner can route into the per-family audit log.

Trait Implementations§

Source§

impl Debug for TokenValidator

Source§

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

Formats the value using the given formatter. Read more

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.

§

impl<T> Instrument for T

§

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

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

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.

§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. 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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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