Trait itsdangerous::TimestampSigner
source · [−]pub trait TimestampSigner {
fn separator(&self) -> Separator;
fn sign_with_timestamp<S: AsRef<str>>(
&self,
value: S,
timestamp: SystemTime
) -> String;
fn sign<S: AsRef<str>>(&self, value: S) -> String;
fn unsign<'a>(
&'a self,
value: &'a str
) -> Result<UnsignedValue<'_>, BadTimedSignature<'a>>;
}
Expand description
A TimestampSigner wraps an inner Signer, giving it the ability to dish out signatures with timestamps.
Basic Usage
use std::time::Duration;
use itsdangerous::{default_builder, Signer, TimestampSigner, IntoTimestampSigner};
// Create a signer using the default builder, and an arbitrary secret key.
let signer = default_builder("secret key").build().into_timestamp_signer();
// Sign an arbitrary string.
let signed = signer.sign("hello world!");
// Unsign the string and validate whether or not its expired.
let unsigned = signer.unsign(&signed).expect("Signature was not valid");
let value = unsigned
.value_if_not_expired(Duration::from_secs(60))
.expect("Signature was expired");
assert_eq!(value, "hello world!");
Required methods
fn sign_with_timestamp<S: AsRef<str>>(
&self,
value: S,
timestamp: SystemTime
) -> String
fn sign_with_timestamp<S: AsRef<str>>(
&self,
value: S,
timestamp: SystemTime
) -> String
Signs a value with an arbitrary timestamp.
Signs a value using the current system timestamp (as provided by SystemTime::now
).
fn unsign<'a>(
&'a self,
value: &'a str
) -> Result<UnsignedValue<'_>, BadTimedSignature<'a>>
fn unsign<'a>(
&'a self,
value: &'a str
) -> Result<UnsignedValue<'_>, BadTimedSignature<'a>>
The inverse of sign
/ sign_with_timestamp
, returning an UnsignedValue
, which you
can grab the value, timestamp, and assert the max age of the signed value with.
Remarks
This method performs zero copies or heap allocations and returns a reference to a slice
of the provided value
, inside of the UnsignedValue
that is returned. If you need a
copy, consider doing unsigned_value.value().to_owned()
to convert the &str
to a String
.