Expand description
JSON Web Signature (JWS) implementation following RFC 7515 and RFC 7797 (Unencoded Payload Option).
§Usage
The entry point to store and verify JWS is the &Jws type, borrowing
the JWS, just like a &str borrows a text string.
The JwsBuf type is the owned version of this type, owning the JWS,
just like a String owns a text string.
§Decoding & Verification
Use JwsSlice::verify to decode a JWS.
use serde_json::json;
use ssi_jwk::JWK;
use ssi_jws::Jws;
let jws = Jws::new(b"eyJhbGciOiJFUzI1NiJ9.cGF5bG9hZA.LW6XkHmgfNnb2CA-2qdeMVGpekAoxRNsAHoeLpnton3QMaQ3dMj-5G9SlP8dHj7cHf2HtRPdy6-9LbxYKvumKw").unwrap();
let jwk: JWK = json!({
"kty": "EC",
"use": "sig",
"crv": "P-256",
"x": "dxdB360AJqJFYhdctoKZD_a_P6vLGAxtEVaCLnyraXQ",
"y": "iH6o0l5AECsfRuEw2Eghbrp-6Fob3j98-1Cbe1YOmwM",
"alg": "ES256"
}).try_into().unwrap();
assert!(jws.verify(&jwk).await.unwrap().is_ok());Internally JwsSlice::verify uses JwsSlice::decode to decode
the JWS, then DecodedJws::verify to validate the signature.
let decoded_jws = jws.to_decoded().unwrap();
let verifiable_jws = decoded_jws.into_verifiable().await.unwrap();
assert_eq!(verifiable_jws.verify(&jwk).await.unwrap().is_ok());You can use this method to decode the payload before the verification
(using DecodedJws::try_map for instance) so it can be verified along the
signature.
§Signature
Use the JwsPayload::sign method to sign a payload into a compact JWS.
use serde_json::json;
use ssi_jwk::JWK;
use ssi_jws::JwsPayload;
let jwk: JWK = json!({
"kty": "EC",
"d": "3KSLs0_obYeQXfEI9I3BBH5y7aOm028bEx3rW6i5UN4",
"use": "sig",
"crv": "P-256",
"x": "dxdB360AJqJFYhdctoKZD_a_P6vLGAxtEVaCLnyraXQ",
"y": "iH6o0l5AECsfRuEw2Eghbrp-6Fob3j98-1Cbe1YOmwM",
"alg": "ES256"
}).try_into().unwrap();
let jwt = "payload".sign(&jwk).await.unwrap();
assert_eq!(jwt, "eyJhbGciOiJFUzI1NiJ9.cGF5bG9hZA.LW6XkHmgfNnb2CA-2qdeMVGpekAoxRNsAHoeLpnton3QMaQ3dMj-5G9SlP8dHj7cHf2HtRPdy6-9LbxYKvumKw")§URL safety and JWS types
RFC 7515 originally defines JWS as URL safe strings due to the payload
being base64 URL-safe encoded.
However, RFC 7797 introduces a b64 header option that makes this
encoding optional. If set to false, the JWS may not be URL-safe. In fact
it may not be UTF-8 encoded at all.
To deal with these different encoding expectations this library provides three families of types for representing JWS:
JwsandJwsBuf: This is the most common type family that follows RFC 7515 to the letter, expecting an URL-safe JWS. It is still possible to use theb64header to embed unencoded payloads but those payloads must use URL-safe base64 bytes/characters.JwsStrandJwsString: This family relaxes the URL-safe payload constraint. Unencoded payloads may use bytes outside of the URL-safe base64 alphabet, but they must be valid UTF-8 strings. This guarantees that the overall JWS is a valid UTF-8 string, even if it is not URL-safe.JwsSliceandJwsVec: This family does not imposes any constraint on unencoded payloads. There is no guaranty that the overall JWS will be an UTF-8 string.
Re-exports§
pub use error::Error;
Modules§
Macros§
- jws
- Creates a new static URL-safe JWS reference from a string literal.
Structs§
- Decoded
Jws - Decoded JWS.
- Decoded
Signing Bytes - JWS decoded signing bytes.
- Header
- JOSE Header.
- Invalid
Jws - JwkWith
Algorithm - Jws
- Borrowed URL-safe JWS.
- JwsBuf
- Owned URL-safe JWS.
- JwsParts
- Decoded JWS parts.
- JwsSignature
- JwsSigner
Info - JwsSlice
- Borrowed JWS without any encoding guaranties.
- JwsStr
- Borrowed UTF-8 encoded JWS.
- JwsString
- Owned UTF-8 encoded JWS.
- JwsVec
- Owned JWS without any encoding guaranties.
Enums§
- Base64
Decode Error - Errors that can occur while decoding.
- Decode
Error - Invalid
Header
Traits§
- JwsPayload
- JWS payload type.
- JwsSigner
- JWS Signer.
- Validate
JwsHeader
Functions§
- complete_
sign_ unencoded_ payload - decode_
jws_ parts - Decode JWS parts (JOSE header, payload, and signature) into useful values. The payload argument is bytes since it may be unencoded if the b64:false header parameter is used; otherwise it must be a base64url-encoded string. Header and signature are always expected to be base64url-encoded. “crit” (critical) header parameters are checked and disallowed if unrecognized/unsupported.
- decode_
unverified - decode_
verify - detached_
recover - Recover a JWK from a JWS and payload, if the algorithm supports that (such as ES256K-R).
- detached_
recover_ legacy_ keccak_ es256kr - detached_
sign_ unencoded_ payload - detached_
verify - Verify a JWS with detached payload. Returns the JWS header on success.
- encode_
sign - encode_
sign_ custom_ header - encode_
unsigned - prepare_
detached_ unencoded_ payload - recover
- Recover a key from a signature and message, if the algorithm supports this. (e.g. ES256K-R)
- sign_
bytes - sign_
bytes_ b64 - split_
detached_ jws - split_
jws - verify_
bytes - verify_
bytes_ warnable