#oauth #证书 #oauth-token #提供商 #运行时 #读写 #

oauth-certs

项目在运行时从提供商获取OAUTH证书,并存储在静态读写锁中。

6个版本 (破坏性)

0.6.0 2024年8月6日
0.5.0 2024年1月31日
0.4.0 2024年1月31日
0.3.0 2023年9月7日
0.1.0 2023年8月6日

#3#oauth-token

Download history 118/week @ 2024-08-04

每月118次 下载

MIT 许可证

8KB
108 代码行

oauth-certs CI

项目在运行时从提供商获取OAUTH证书,并存储在静态读写锁中。

基本使用方法摘要

/// `GooglePayload` is the user data from google.
/// see https://developers.google.com/identity/openid-connect/openid-connect for more info.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ClaimsGoogle {
    // These fields are marked `always`.
    aud: String,
    exp: u64,
    iat: u64,
    iss: String,
    sub: String,

    /// Email
    email: String,
    /// Email verified or not
    email_verified: Option<bool>,
}

/// decode JWT token into Claims
pub async fn decode(token: &str) -> Result<ClaimsGoogle> {
    let header = jsonwebtoken::decode_header(token)?;

    let certs = oauth_certs::google::oauth2_v3_certs().await.map_err(|e| {
        // tracing::error!("Failed to get google oauth certs: {}", e);
        jsonwebtoken::errors::ErrorKind::RsaFailedSigning
    })?;

    let jwt_key = (certs)
        .keys
        .iter()
        .find(|cert| cert.common.key_id == header.kid)
        .ok_or::<jsonwebtoken::errors::Error>(
            jsonwebtoken::errors::ErrorKind::InvalidKeyFormat.into(),
        )?;

    let decoding_key = DecodingKey::from_jwk(jwt_key)?;

    jsonwebtoken::decode::<ClaimsGoogle>(token, &decoding_key, &validation())
        .map(|token_data| token_data.claims)
}

依赖项

~6–18MB
~269K SLoC