#json #jwks #auth0 #client #web #key

jwks_client_rs

Auth0的JWKS同步客户端实现

9个不稳定版本

0.5.1 2024年4月17日
0.5.0 2023年12月13日
0.4.2 2023年10月18日
0.4.1 2023年4月3日
0.1.1 2021年11月11日

#55身份验证

Download history 534/week @ 2024-04-30 743/week @ 2024-05-07 626/week @ 2024-05-14 598/week @ 2024-05-21 733/week @ 2024-05-28 734/week @ 2024-06-04 1237/week @ 2024-06-11 865/week @ 2024-06-18 880/week @ 2024-06-25 1054/week @ 2024-07-02 922/week @ 2024-07-09 1207/week @ 2024-07-16 1705/week @ 2024-07-23 1174/week @ 2024-07-30 1539/week @ 2024-08-06 1086/week @ 2024-08-13

5,780 每月下载次数
用于 prima_bridge

MIT 许可证

36KB
759 代码行

JWKS客户端

此库用于存储来自您的身份验证提供程序的Json Web Key Set。它在内部缓存中存储JWKS,并在给定时间后自动刷新。

安装

添加到您的 Cargo.toml

# Cargo.toml
[dependencies]
jwks_client_rs = "0.5"

代码示例

// Put in your application context or wherever this can live long enough
use jwks_client_rs::source::WebSource;
use jwks_client_rs::JwksClient;

// here you must join your `BASE_AUTH0_URL` env var with `.well-known/jwks.json` or whatever is the jwks url
let url: reqwest::Url = todo!();
let timeout: std::time::Duration = todo!();
// You can define a different source too using `JwksSource` trait
let source: WebSource = WebSource::builder()
    .with_timeout(timeout)
    .with_connect_timeout(timeout)
    .build(url);

let client: JwksClient<WebSource> = JwksClient::builder()
    .build(source);

// Store your client in your application context or whatever
// ..

// Get jwk by kid
use jwks_client_rs::{JsonWebKey, JwksClientError};

let kid: String = todo!();
let result: Result<JsonWebKey, JwksClientError> = app_context.jwks_client.get(kid).await;

您可以解码您的令牌,验证它是否由您的身份验证提供程序JWKS签名。

#[derive(serde::Deserialize)]
struct Claims {
    aud: String,
}

let client: JwksClient = todo!();
// Here's the token. Remember to remove "Bearer " from your token in case it is present
let token: &str = todo!();
// The audience the token were released for.
let audience: &str = todo!();
let result: Result<Claims, JwksClientError> = client.decode::<Claims>(token, audience).await;

示例

可以在示例文件夹中找到一个工作示例。要运行示例

  • 导出 KID 环境变量(从您的已知jwks租户获取)
  • 导出 BASE_AUTH0_URL(运行localauth0或使用您的auth0租户;URL应该是您的本地auth0在localhost上公开的端口或类似https://{your-tenant}.eu.auth0.com的URL)
  • 在shell中运行 cargo run --example get_jwks

依赖关系

~7–21MB
~342K SLoC