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 在 身份验证 中
5,780 每月下载次数
用于 prima_bridge
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