Struct mini_functions::jwt::JWT

source ·
pub struct JWT {
    pub header: Header,
    pub claims: Claims,
    pub signature: Vec<u8>,
    pub token: String,
}
Expand description

JWT is a struct that holds the JWT token and its associated claims. Provides a set of utility functions for working with JSON Web Tokens (JWTs) and JSON Web Signatures (JWSs).

Fields§

§header: Header

The header of the JWT.

§claims: Claims

The claims associated with the JWT.

§signature: Vec<u8>

The signature of the JWT.

§token: String

The JWT token.

Implementations§

source§

impl JWT

source

pub fn claims() -> Claims

Claims returns a default Claims struct.

source

pub fn decode(&mut self, secret: &[u8]) -> Result<String, JwtError>

Decodes a JWT token. takes a mutable reference to a JWT struct and a reference to a slice of bytes representing a secret, and it returns a Result containing a string or an Error variant. The function splits the JWT stored in the token field of the JWT struct into its header, claims, and signature, decodes the header and claims from base64, deserializes the header and claims from JSON, and then verifies the JWT’s signature using the provided secret.

Arguments
  • secret - A byte array containing the secret used to sign the JWT.
Returns
  • Ok(String) - The decoded JWT as a string.
  • Err(Error) - An error if the JWT is invalid or if there was a problem decoding it.
source

pub fn encode( header: Header, claims: Claims, secret: &[u8] ) -> Result<String, JwtError>

Encodes a JWT token using the provided header, claims, and secret. It returns a Result containing a string or an Error variant. The function serializes the header and claims to JSON, base64-encodes the header and claims, concatenates the encoded header, claims, and separators, and then signs the JWT with the provided secret.

source

pub fn generate(secret: &[u8]) -> Result<String, JwtError>

Generates a JWT token.

source

pub fn get_token(jwt: JWT) -> String

Returns the token field of the JWT struct.

source

pub fn get_token_header(jwt: JWT) -> Header

Returns the header field of the JWT struct.

source

pub fn get_token_length(jwt: JWT) -> usize

Get the token length.

source

pub fn validate(&self, secret: &[u8]) -> Result<(), JwtError>

Validates a JWT token.

Trait Implementations§

source§

impl Clone for JWT

source§

fn clone(&self) -> JWT

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 Debug for JWT

source§

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

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

impl Default for JWT

source§

fn default() -> JWT

Returns a default JWT struct.

source§

impl<'de> Deserialize<'de> for JWT

source§

fn deserialize<__D>( __deserializer: __D ) -> Result<JWT, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for JWT

source§

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

Formats the JWT struct for printing.

source§

impl Serialize for JWT

source§

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

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for JWT

§

impl Send for JWT

§

impl Sync for JWT

§

impl Unpin for JWT

§

impl UnwindSafe for JWT

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.

§

impl<T> FromBase64 for T
where T: for<'de> Deserialize<'de>,

§

fn from_base64<Input>(raw: &Input) -> Result<T, Error>
where Input: AsRef<[u8]> + ?Sized,

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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<C> SignWithKey<String> for C
where C: ToBase64,

§

fn sign_with_key(self, key: &impl SigningAlgorithm) -> Result<String, Error>

§

impl<T> ToBase64 for T
where T: Serialize,

§

fn to_base64(&self) -> Result<Cow<'_, str>, Error>

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,