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
impl Jwt
Sourcepub fn sign(&self, key: &Key) -> Result<String>
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 flagsError::Key
if the key is invalid or doesn’t match the algorithmError::Internal
if signing fails
Source§impl Jwt
impl Jwt
Sourcepub fn verify_with_jwks(&self, jwks: &Jwks, opts: VerifyOptions) -> Result<()>
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 keysopts
- Verification options
§Returns
Returns Ok(())
if verification succeeds, or an error if it fails
§Errors
Error::MissingKid
if the JWT header doesn’t contain akid
fieldError::KidNotFound
if thekid
is not found in the JWKS
Source§impl Jwt
impl Jwt
Sourcepub fn set(&mut self, path: &str, value: Value) -> Result<()>
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§
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> 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