Skip to content

New Token API

Compare
Choose a tag to compare
@mikkyang mikkyang released this 23 May 09:59
· 119 commits to master since this release
afd2f15
  • Move all of the previous structs to the legacy module
  • Introduce more idiomatic Header, Claims, and Token types
  • Support more algorithms with optional OpenSSL support
  • Convenience methods to just sign and verify claims

For most, the following should be enough with their own defined claims types:

With the following key:

let key: Hmac<Sha256> = Hmac::new_varkey(b"some-secret").unwrap();

Signing:

let mut claims = BTreeMap::new();
claims.insert("sub", "someone");

let token_str = claims.sign_with_key(&key).unwrap();

Verification:

let token_str = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzb21lb25lIn0.5wwE1sBrs-vftww_BGIuTVDeHtc1Jsjo-fiHhDwR8m0";

let claims: BTreeMap<String, String> = VerifyWithKey::verify_with_key(token_str, &key).unwrap();