在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Keats/jsonwebtoken开源软件地址:https://github.com/Keats/jsonwebtoken开源编程语言:Rust 100.0%开源软件介绍:jsonwebtokenSee JSON Web Tokens for more information on what JSON Web Tokens are. InstallationAdd the following to Cargo.toml: jsonwebtoken = "8"
# If you do not need pem decoding, you can disable the default feature `use_pem` that way:
# jsonwebtoken = {version = "8", default-features = false }
serde = {version = "1.0", features = ["derive"] } The minimum required Rust version is 1.56. AlgorithmsThis library currently supports the following:
How to useComplete examples are available in the examples directory: a basic one and one with a custom header. In terms of imports and structs: use serde::{Serialize, Deserialize};
use jsonwebtoken::{encode, decode, Header, Algorithm, Validation, EncodingKey, DecodingKey};
/// Our claims struct, it needs to derive `Serialize` and/or `Deserialize`
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
sub: String,
company: String,
exp: usize,
} ClaimsThe claims fields which can be validated. (see validation) #[derive(Debug, Serialize, Deserialize)]
struct Claims {
aud: String, // Optional. Audience
exp: usize, // Required (validate_exp defaults to true in validation). Expiration time (as UTC timestamp)
iat: usize, // Optional. Issued at (as UTC timestamp)
iss: String, // Optional. Issuer
nbf: usize, // Optional. Not Before (as UTC timestamp)
sub: String, // Optional. Subject (whom token refers to)
} HeaderThe default algorithm is HS256, which uses a shared secret. let token = encode(&Header::default(), &my_claims, &EncodingKey::from_secret("secret".as_ref()))?; Custom headers & changing algorithmAll the parameters from the RFC are supported but the default header only has let mut header = Header::new(Algorithm::HS512);
header.kid = Some("blabla".to_owned());
let token = encode(&header, &my_claims, &EncodingKey::from_secret("secret".as_ref()))?; Look at Encoding// HS256
let token = encode(&Header::default(), &my_claims, &EncodingKey::from_secret("secret".as_ref()))?;
// RSA
let token = encode(&Header::new(Algorithm::RS256), &my_claims, &EncodingKey::from_rsa_pem(include_bytes!("privkey.pem"))?)?; Encoding a JWT takes 3 parameters:
When using HS256, HS2384 or HS512, the key is always a shared secret like in the example above. When using RSA/EC, the key should always be the content of the private key in the PEM or DER format. If your key is in PEM format, it is better performance wise to generate the Decoding// `token` is a struct with 2 fields: `header` and `claims` where `claims` is your own struct.
let token = decode::<Claims>(&token, &DecodingKey::from_secret("secret".as_ref()), &Validation::default())?;
As with encoding, when using HS256, HS2384 or HS512, the key is always a shared secret like in the example above. When using RSA/EC, the key should always be the content of the public key in the PEM or DER format. In some cases, for example if you don't know the algorithm used or need to grab the let header = decode_header(&token)?; This does not perform any signature verification or validate the token claims. You can also decode a token using the public key components of a RSA key in base64 format. The main use-case is for JWK where your public key is in a JSON format like so: {
"kty":"RSA",
"e":"AQAB",
"kid":"6a7a119f-0876-4f7e-8d0f-bf3ea1391dd8",
"n":"yRE6rHuNR0QbHO3H3Kt2pOKGVhQqGZXInOduQNxXzuKlvQTLUTv4l4sggh5_CYYi_cvI-SXVT9kPWSKXxJXBXd_4LkvcPuUakBoAkfh-eiFVMh2VrUyWyj3MFl0HTVF9KwRXLAcwkREiS3npThHRyIxuy0ZMeZfxVL5arMhw1SRELB8HoGfG_AtH89BIE9jDBHZ9dLelK9a184zAf8LwoPLxvJb3Il5nncqPcSfKDDodMFBIMc4lQzDKL5gvmiXLXB1AGLm8KBjfE8s3L5xqi-yUod-j8MtvIj812dkS4QMiRVN_by2h3ZY8LYVGrqZXZTcgn2ujn8uKjXLZVD5TdQ"
} // `token` is a struct with 2 fields: `header` and `claims` where `claims` is your own struct.
let token = decode::<Claims>(&token, &DecodingKey::from_rsa_components(jwk["n"], jwk["e"]), &Validation::new(Algorithm::RS256))?; If your key is in PEM format, it is better performance wise to generate the Convert SEC1 private key to PKCS8
openssl pkcs8 -topk8 -nocrypt -in sec1.pem -out pkcs8.pem ValidationThis library validates automatically the Since validating time fields is always a bit tricky due to clock skew,
you can add some leeway to the Last but not least, you will need to set the algorithm(s) allowed for this token if you are not using Look at |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论