JWT
A JSON Web Token library.
Usage
Note, for legacy support (not recommended), import from jwt::legacy
instead
of directly from jwt
. Everything should work as before, with some small
improvements.
Only Claims
If you don't care about that header as long as the header is verified, signing and verification can be done with just a few traits.
Signing
Claims can be any serde::Serialize
type, usually derived with
serde_derive
.
extern crate hmac;
extern crate jwt;
extern crate sha2;
use ;
use SignWithKey;
use Sha256;
use BTreeMap;
let key: = new_varkey.unwrap;
let mut claims = new;
claims.insert;
let token_str = claims.sign_with_key.unwrap;
assert_eq!;
Verification
Claims can be any serde::de::DeserializeOwned
type, usually derived with
serde_derive
.
extern crate hmac;
extern crate jwt;
extern crate sha2;
use ;
use VerifyWithKey;
use Sha256;
use BTreeMap;
let key: = new_varkey.unwrap;
let token_str = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzb21lb25lIn0.5wwE1sBrs-vftww_BGIuTVDeHtc1Jsjo-fiHhDwR8m0";
let claims: = verify_with_key.unwrap;
assert_eq!;
Header and Claims
If you need to customize the header, you can use the Token
struct. For
convenience, a Header
struct is provided for all of the commonly defined
fields, but any type that implements JoseHeader
can be used.
Signing
Both header and claims have to implement serde::Serialize
.
extern crate hmac;
extern crate jwt;
extern crate sha2;
use ;
use ;
use Sha384;
use BTreeMap;
let key: = new_varkey.unwrap;
let header = Header ;
let mut claims = new;
claims.insert;
let token = new.sign_with_key.unwrap;
assert_eq!;
Verification
Both header and claims have to implement serde::de::DeserializeOwned
.
extern crate hmac;
extern crate jwt;
extern crate sha2;
use ;
use ;
use Sha384;
use BTreeMap;
let key: = new_varkey.unwrap;
let token_str = "eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiJzb21lb25lIn0.WM_WnPUkHK6zm6Wz7zk1kmIxz990Te7nlDjQ3vzcye29szZ-Sj47rLNSTJNzpQd_";
let token: = verify_with_key.unwrap;
let header = token.header;
let claims = token.claims;
assert_eq!;
assert_eq!;