Struct jwt_compact::UntrustedToken
source · 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>
impl<'a> UntrustedToken<'a>
source§impl<H> UntrustedToken<'_, H>
impl<H> UntrustedToken<'_, H>
sourcepub fn into_owned(self) -> UntrustedToken<'static, H>
pub fn into_owned(self) -> UntrustedToken<'static, H>
Converts this token to an owned form.
sourcepub fn signature_bytes(&self) -> &[u8] ⓘ
pub fn signature_bytes(&self) -> &[u8] ⓘ
Returns signature bytes from the token. These bytes are not guaranteed to form a valid signature.
sourcepub fn deserialize_claims_unchecked<T>(
&self
) -> Result<Claims<T>, ValidationError>where
T: DeserializeOwned,
pub fn deserialize_claims_unchecked<T>(
&self
) -> Result<Claims<T>, ValidationError>where
T: DeserializeOwned,
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>
impl<'a, H: Clone> Clone for UntrustedToken<'a, H>
source§fn clone(&self) -> UntrustedToken<'a, H>
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<'a, H: Debug> Debug for UntrustedToken<'a, H>
impl<'a, H: Debug> Debug for UntrustedToken<'a, H>
source§impl<'a, H: DeserializeOwned> TryFrom<&'a str> for UntrustedToken<'a, H>
impl<'a, H: DeserializeOwned> TryFrom<&'a str> for UntrustedToken<'a, H>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more