#json #key-set #jwt #web #parse-json #jsonwebtoken #jwk

jwks

用于检索和解析 JSON Web Key Sets (JWKS) 的库

2 个版本

0.1.3 2024 年 2 月 12 日
0.1.2 2024 年 1 月 26 日

网页编程 类别中排名 #1925

Download history 2/week @ 2024-03-11 4/week @ 2024-03-25 30/week @ 2024-04-01 1/week @ 2024-05-20 9/week @ 2024-06-03 14/week @ 2024-06-10 13/week @ 2024-06-17 25/week @ 2024-06-24

每月下载量 61

MIT 许可证

17KB
287

jwks

获取并解析 JSON Web Key Set (JWKS)

cargo add jwks

https://crates.io/crates/jwks

用法

从 jwks url。

let jwks_url = "https://www.googleapis.com/oauth2/v3/certs";
let jwks = Jwks::from_jwks_url(jwks_url).await.unwrap();

从 openid 配置 url。

let openid_config_url = "https://accounts.google.com/.well-known/openid-configuration";
let jwks = Jwks::from_oidc_url(openid_config_url).await.unwrap();

jsonwebtoken 结合使用以验证 jwt

use jsonwebtoken::{decode, decode_header, Algorithm, TokenData, Validation};

let jwt = "...base64-encoded-jwt...";

// get the kid from jwt
let header = decode_header(jwt).expect("jwt header should be decoded");
let kid = header.kid.as_ref().expect("jwt header should have a kid");

// get a jwk from jwks by kid
let jwks_url = "https://www.googleapis.com/oauth2/v3/certs";
let jwks = Jwks::from_jwks_url().await.unwrap();
let jwk = jwks.keys.get(kid).expect("jwt refer to a unknown key id");

let validation = Validation::default();
let decoded_token: TokenData<Claims> = decode::<Claims>(jwt, &jwk.decoding_key, &validation).expect("jwt should be valid");

依赖项

~6–19MB
~301K SLoC