Skip to main content

encode_with

Function encode_with 

Source
pub fn encode_with(key: &Key, claims: &Claims) -> String
Expand description

Signs claims with key and returns the compact token header.payload.signature.

The header is always {"alg":"HS256","typ":"JWT"}; the three parts are URL-safe base64 without padding. Handlers use encode, which takes the key from SECRET_KEY_BASE.

ยงExamples

use ocre::jwt::{Claims, Key, encode_with};

let key = Key::from_secret_key_base(&"x".repeat(64));
let token = encode_with(&key, &Claims { sub: "42".to_owned(), iat: 0, exp: 60 });
assert!(token.starts_with("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."));
assert_eq!(token.split('.').count(), 3);