Jwt

Struct Jwt 

Source
pub struct Jwt {
    pub header: Header,
    pub claims: Claims,
    pub signature_b64: String,
    pub raw_header_b64: String,
    pub raw_payload_b64: String,
}
Expand description

A decoded JWT token

Fields§

§header: Header

The JWT header

§claims: Claims

The JWT claims (payload)

§signature_b64: String

The signature as base64url-encoded string

§raw_header_b64: String

The raw header as base64url-encoded string

§raw_payload_b64: String

The raw payload as base64url-encoded string

Implementations§

Source§

impl Jwt

Source

pub fn decode(token: &str) -> Result<Self>

Decode a JWT token into its components

This is a convenience method that calls the decode function.

§Arguments
  • token - The JWT token string to decode
§Returns

Returns a Result containing the decoded JWT or an error if the token is malformed

Source§

impl Jwt

Source

pub fn sign(&self, key: &Key) -> Result<String>

Sign this JWT with the given key

§Arguments
  • key - The signing key (must match the algorithm in the header)
§Returns

Returns a Result containing the signed JWT token or an error

§Errors
  • Error::DisabledAlg if the algorithm is not enabled via feature flags
  • Error::Key if the key is invalid or doesn’t match the algorithm
  • Error::Internal if signing fails
Source§

impl Jwt

Source

pub fn verify(&self, key: &Key, opts: VerifyOptions) -> Result<()>

Verify this JWT with the given key and options

§Arguments
  • key - The verification key
  • opts - Verification options
§Returns

Returns Ok(()) if verification succeeds, or an error if it fails

Source

pub fn verify_with_jwks(&self, jwks: &Jwks, opts: VerifyOptions) -> Result<()>

Verify this JWT using a JWKS (JSON Web Key Set)

§Arguments
  • jwks - The JWKS containing the verification keys
  • opts - Verification options
§Returns

Returns Ok(()) if verification succeeds, or an error if it fails

§Errors
  • Error::MissingKid if the JWT header doesn’t contain a kid field
  • Error::KidNotFound if the kid is not found in the JWKS
Source§

impl Jwt

Source

pub fn set(&mut self, path: &str, value: Value) -> Result<()>

Set a claim value using a JSON pointer path

§Arguments
  • path - JSON pointer path (e.g., “/sub”, “/user/name”)
  • value - The value to set
§Returns

Returns Ok(()) if the value was set successfully, or an error if the path is invalid

§Errors
  • Error::Claims if the path is invalid or traverses a non-object
§Examples
use jwt_lab::{Jwt, Algorithm, Header, Claims};
use serde_json::json;

let mut jwt = Jwt {
    header: Header { alg: Algorithm::HS256, typ: None, kid: None, extra: Default::default() },
    claims: Claims(serde_json::Map::new()),
    signature_b64: String::new(),
    raw_header_b64: String::new(),
    raw_payload_b64: String::new(),
};

jwt.set("/sub", json!("user123"))?;
jwt.set("/user/name", json!("John Doe"))?;

Trait Implementations§

Source§

impl Clone for Jwt

Source§

fn clone(&self) -> Jwt

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 Jwt

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Jwt

§

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