Function jsonwebtokens::encode

source ·
pub fn encode<H: Serialize, C: Serialize>(
    header: &H,
    claims: &C,
    algorithm: &Algorithm
) -> Result<String, Error>
Expand description

Encodes a Json Web Token

For example, to encode and sign a token with a symmetric secret:

let alg = Algorithm::new_hmac(AlgorithmID::HS256, "secret")?;
let header = json!({ "alg": alg.name() });
let claims = json!({ "foo": "bar" });
let token = jwt::encode(&header, &claims, &alg)?;

Or to encode and sign a token with an RSA private key:

let pem_data = include_bytes!("../tests/rsa/private_rsa_key_pkcs1.pem");
let alg = Algorithm::new_rsa_pem_signer(AlgorithmID::RS256, pem_data)?;
let header = json!({ "alg": alg.name() });
let claims = json!({ "foo": "bar" });
let token = jwt::encode(&header, &claims, &alg)?;