pub struct UntrustedToken<'a, H = Empty> { /* private fields */ }
Expand description

Parsed, but unvalidated token.

The type param (Empty by default) corresponds to the additional information enclosed in the token Header.

An UntrustedToken can be parsed from a string using the TryFrom implementation. This checks that a token is well-formed (has a header, claims and a signature), but does not validate the signature. As a shortcut, a token without additional header info can be created using Self::new().

Examples

let token_str = "eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9.eyJp\
    c3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leG\
    FtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.dBjftJeZ4CVP-mB92K27uhbUJ\
    U1p1r_wW1gFWFOEjXk";
let token: UntrustedToken = token_str.try_into()?;
// The same operation using a shortcut:
let same_token = UntrustedToken::new(token_str)?;
// Token header can be accessed to select the verifying key etc.
let key_id: Option<&str> = token.header().key_id.as_deref();

Handling tokens with custom header fields

#[derive(Debug, Clone, Deserialize)]
struct HeaderExtensions {
    custom: String,
}

let token_str = "eyJhbGciOiJIUzI1NiIsImtpZCI6InRlc3Rfa2V5Iiwid\
    HlwIjoiSldUIiwiY3VzdG9tIjoiY3VzdG9tIn0.eyJzdWIiOiIxMjM0NTY\
    3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9._27Fb6nF\
    Tg-HSt3vO4ylaLGcU_ZV2VhMJR4HL7KaQik";
let token: UntrustedToken<HeaderExtensions> = token_str.try_into()?;
let extensions = &token.header().other_fields;
println!("{}", extensions.custom);

Implementations§

source§

impl<'a> UntrustedToken<'a>

source

pub fn new<S: AsRef<str> + ?Sized>(s: &'a S) -> Result<Self, ParseError>

Creates an untrusted token from a string. This is a shortcut for calling the TryFrom conversion.

source§

impl<H> UntrustedToken<'_, H>

source

pub fn into_owned(self) -> UntrustedToken<'static, H>

Converts this token to an owned form.

source

pub fn header(&self) -> &Header<H>

Gets the token header.

source

pub fn algorithm(&self) -> &str

Gets the integrity algorithm used to secure the token.

source

pub fn signature_bytes(&self) -> &[u8]

Returns signature bytes from the token. These bytes are not guaranteed to form a valid signature.

source

pub fn deserialize_claims_unchecked<T>( &self ) -> Result<Claims<T>, ValidationError>

Deserializes claims from this token without checking token integrity. The resulting claims are thus not guaranteed to be valid.

Trait Implementations§

source§

impl<'a, H: Clone> Clone for UntrustedToken<'a, H>

source§

fn clone(&self) -> UntrustedToken<'a, H>

Returns a copy 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<'a, H: Debug> Debug for UntrustedToken<'a, H>

source§

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

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

impl<'a, H: DeserializeOwned> TryFrom<&'a str> for UntrustedToken<'a, H>

§

type Error = ParseError

The type returned in the event of a conversion error.
source§

fn try_from(s: &'a str) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'a, H> RefUnwindSafe for UntrustedToken<'a, H>
where H: RefUnwindSafe,

§

impl<'a, H> Send for UntrustedToken<'a, H>
where H: Send,

§

impl<'a, H> Sync for UntrustedToken<'a, H>
where H: Sync,

§

impl<'a, H> Unpin for UntrustedToken<'a, H>
where H: Unpin,

§

impl<'a, H> UnwindSafe for UntrustedToken<'a, H>
where H: UnwindSafe,

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

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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