14个稳定版本

1.11.0 2024年4月19日
1.10.1 2024年2月3日
1.8.0 2023年11月16日
1.1.0 2023年5月8日

#151 in Web编程

Download history 367/week @ 2024-04-24 374/week @ 2024-05-01 367/week @ 2024-05-08 313/week @ 2024-05-15 365/week @ 2024-05-22 569/week @ 2024-05-29 381/week @ 2024-06-05 230/week @ 2024-06-12 261/week @ 2024-06-19 266/week @ 2024-06-26 264/week @ 2024-07-03 350/week @ 2024-07-10 322/week @ 2024-07-17 294/week @ 2024-07-24 353/week @ 2024-07-31 176/week @ 2024-08-07

1,210 monthly downloads

MIT 许可证

43KB
777

Google-Oauth

描述

Google-Oauth 是一个用于Google oauth2的服务器端验证库。

Google-Oauth 可以帮助您验证从Google生成的 id_tokenaccess_token

使用方法(异步)

1. 配置

要将 Google-Oauth 添加到您的项目中,请将以下行添加到您的 Cargo.toml 中。

[dependencies]
google-oauth = { version = "1" }

如果您决定使用 async 函数,请选择一个 async 运行时。这里有一些选项供您选择

  1. tokio
  2. async-std
  3. actix-web

我们在示例中使用 tokio,并重构了我们的主函数如下

#[tokio::main]
// #[async_std::main] // when you use [async-std]
// #[actix_web::main] // when you use [actix-web]
async fn main() {}

2. 执行验证(id_token

您可以从Google管理控制台(或其它地方)获取您的 client_id,并且用户已经提供了一个 id_token。它们都是 字符串类型。使用以下代码进行验证

use google_oauth::AsyncClient;

#[tokio::main]
async fn main() {
    let client_id = "your client id";
    let id_token = "the id_token";
    
    let client = AsyncClient::new(client_id);
    /// or, if you want to set the default timeout for fetching certificates from Google, e.g, 30 seconds, you can:
    /// ```rust
    /// let client = AsyncClient::new(client_id).timeout(time::Duration::from_sec(30));
    /// ```
    
    let payload = client.validate_id_token(id_token).await.unwrap(); // In production, remember to handle this error.
    
    // When we get the payload, that mean the id_token is valid.
    // Usually we use `sub` as the identifier for our user...
    println!("Hello, I am {}", &payload.sub);
    
    // If you have multiple client ids, you can:
    let client = AsyncClient::new_with_vec(vec![client_id]);
    // The validation fails when the id_token matches NONE of the provided client ids.
}

不提供任何客户端ID进行验证

当没有为 AsyncClient 提供任何 client_id 时,验证 id_token 时将不会使用 client_id。在这种情况下,AsyncClient 将接受所有 client_id。然而,Google发行者(iss)、过期时间(exp)和JWT哈希 不能 跳过。

3. 执行验证(AccessToken

有时,Google会返回一个 access_token 而不是 id_tokenGoogle-Oauth 仍然提供用于验证从Google获取的 access_token 的API。

注意:当验证 access_token 时,我们不关心 client_id。所以,如果你只需要验证 access_token,你可以简单地传递一个空的 client_id,就像这样

use google_oauth::AsyncClient;

#[tokio::main]
async fn main() {
    let access_token = "the access_token";

    let client = AsyncClient::new("");

    let payload = client.validate_access_token(access_token).await.unwrap(); // In production, remember to handle this error.

    // When we get the payload, that mean the id_token is valid.
    // Usually we use `sub` as the identifier for our user...
    println!("Hello, I am {}", &payload.sub);
}

警告:access_token 的结果与 id_token 的结果不同,尽管它们有一个相同的字段 sub

完整示例,请查看 ./example/async_client/

支持的算法

对于验证 id_token,Google 可能会使用这两种哈希算法来生成 JWTs

  • RS256
  • ES256

然而,我找不到获取有效 ES256 令牌的方法,因此我留下了一个 unimplemented 分支,并在 JWT 是 ES256 哈希时返回一个 Err

如果您有示例,请随时创建一个新问题。PR 欢迎使用。

用法(阻塞)

Google-Oauth 还提供了一个阻塞客户端。您需要启用 blocking 功能

[dependencies]
google-oauth = { version = "1", features = ["blocking"] }

您可以使用 google_oauth::Client 来验证令牌

use google_oauth::Client;

fn main() {
    let client_id = "your client id";
    let id_token = "the id_token";

    let client = Client::new(client_id);

    let payload = client.validate_id_token(id_token).unwrap();
    
    println!("Hello, I am {}", &payload.sub);
}

完整示例,请查看 ./examples/blocking/

WebAssembly (wasm)

Google-Oauth 支持 wasm,需要 wasm 功能。

[dependencies]
google-oauth = { version = "1", features = ["wasm"] }

您可以使用 wasm-pack build --features wasm 来构建此库。(cargo install wasm-pack 首先安装。)

如果您需要将 wasm 导入到您的项目中,您可以使用 google_oauth::Client 来运行异步函数。

功能

  • default:启用 AsyncClient
  • blocking:启用 Client
  • wasm:禁用 AsyncClientClientblocking),启用 Clientwasm)。
  • reqwest-rustls:使用 rustls 作为 Reqwest 客户端的 TLS 后端

依赖项

~8–21MB
~340K SLoC