Skip to main content

Ephemeral

Trait Ephemeral 

pub trait Ephemeral {
    // Required method
    fn exp_unix(&self) -> u64;

    // Provided method
    fn is_expired(&self) -> bool { ... }
}
Expand description

A value that expires at a known point in time.

Implement this trait alongside Claims on your JWT claims struct. is_expired has a default implementation based on exp_unix and does not need to be overridden in normal use.

§Example

ⓘ
use rust_auth_lib::{Claims, Ephemeral};
use serde::Deserialize;

#[derive(Deserialize)]
struct MyClaims {
    sub: String,
    exp: u64,
    iat: u64,
}

impl Ephemeral for MyClaims {
    fn exp_unix(&self) -> u64 { self.exp }
}

impl Claims for MyClaims {
    fn sub(&self) -> &str              { &self.sub }
    fn iat(&self) -> u64               { self.iat }
    fn iss(&self) -> Option<&str>      { None }
    fn preferred_username(&self) -> Option<&str> { None }
}

Required Methods§

fn exp_unix(&self) -> u64

The token’s expiry time as seconds since the Unix epoch (exp claim).

Return the raw exp field from your claims struct. The default is_expired implementation compares this value against the current wall-clock time.

Provided Methods§

fn is_expired(&self) -> bool

Returns true if the token has expired.

The default implementation returns true when exp_unix is less than or equal to the current Unix timestamp. Override only if you need custom clock-skew tolerance.

Note: You do not need to call this after TokenValidator::validate — expiry is already enforced by the JWT decoding step. This method is useful when you hold a claims value independently of validation, for example after deserializing from a cache.

Implementors§