Skip to main content

Claims

Struct Claims 

Source
pub struct Claims {
    inner: Map<String, Value>,
}
Expand description

Represents a set of claims made by a JWT.

Fields§

§inner: Map<String, Value>

Implementations§

Source§

impl Claims

Source

pub fn new() -> Self

Source

pub fn check<'s, V: Deserialize<'s>>( self, name: &'static str, expectation: impl FnOnce(&'static str, Option<V>) -> Result<(), Error>, ) -> Result<Self, Error>

Checks that the claim with name meets the given expectation, and removes it from the set. The Deserializer is given ownership of the claim’s contents, so for V you probably want to use an owned type like String instead of &str.

Source

pub fn check_present_and<'s, V: Deserialize<'s>>( self, name: &'static str, expectation: impl FnOnce(&'static str, V) -> Result<(), Error>, ) -> Result<Self, Error>

Check that the claim with name exists and meets the given expectation, removing the claim from the set afterwards. Variation on Claims::check.

Source

pub fn check_no(self, name: &'static str) -> Result<Self, Error>

Checks that there is no claim with name.

Source

pub fn extract<V: DeserializeOwned>( &mut self, name: &'static str, ) -> Result<Option<V>, Error>

Returns the contents of claim with name, if it exists, enabling manual checking.

Source

pub fn ignore(self, name: &'static str) -> Self

Removes named claim from this set, if present, effectively ignoring it.

Source

pub fn check_iss( self, expectation: impl FnOnce(&'static str, Option<String>) -> Result<(), Error>, ) -> Result<Self, Error>

Self::check for iss claim.

Source

pub fn check_sub( self, expectation: impl FnOnce(&'static str, Option<String>) -> Result<(), Error>, ) -> Result<Self, Error>

Self::check for sub claim.

Source

pub fn default_check_timestamps(self) -> Result<Self, Error>

Checks timestamps iat, exp and nbf. When present they should be a valid NumericDate, and the current moment should be between nbf and exp.

Source

pub fn default_check_common_claims(self) -> Result<Self, Error>

Checks and removes iat, exp, and nbf, and makes sure sub and iss have already been checked (i.e. are no longer present).

Source

pub fn visit_custom<C: DeserializeOwned, R>( self, visitor: impl FnOnce(C) -> R, ) -> Result<R, Error>

Deserializes remaining, custom, claims into a type C and calls the given visitor function on it.

Will call Claims::default_check_common_claims first, checking iat, etc..

We can, in general, not return C directly, because C might borrow from the Deserializer.

For the caller’s convenience, we pass along anything that’s returned by the visitor.

Consumes self to prevent deserializing to C twice.

Source

pub fn into_custom<C: DeserializeOwned>(self) -> Result<C, Error>

Like Self::visit_custom, but returns C (which is not possible when C borrows from its Deserializer.)

Source

pub fn from_custom<C: Serialize>(claims: C) -> Result<Self, Error>

Creates new Claims from an object that serializes to a json map.

Source

pub fn claim<V: Serialize>( self, name: &'static str, value: V, ) -> Result<Self, Error>

Adds the named claim with the given value. Returns an error when a claim with the same name was already present.

Source

pub fn iat_now(self) -> Result<Self, Error>

Adds the iat claim using the current timestamp

Source

pub fn exp_after(self, duration: Duration) -> Result<Self, Error>

Sets exp claim such that the jwt is valid for the given duration.

Source

pub fn nbf(self) -> Result<Self, Error>

Sets nbf to the current timestamp, minus 30 seconds leeway.

Source

pub fn sign<SK: SigningKey>(&self, sk: &SK) -> Result<JWT, Error>

Signs these claims, returning a JWT.

Trait Implementations§

Source§

impl Clone for Claims

Source§

fn clone(&self) -> Claims

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Claims

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Claims

Source§

fn default() -> Claims

Returns the “default value” for a type. Read more
Source§

impl Serialize for Claims

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> 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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

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
Source§

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

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

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