7 releases (4 breaking)

0.7.0 Nov 10, 2023
0.6.0 Nov 1, 2023
0.5.4 Oct 31, 2023
0.4.0 Sep 9, 2023
0.3.1 Sep 27, 2022

#162 in Authentication

Download history 35/week @ 2023-11-08 14/week @ 2023-11-15 33/week @ 2023-11-22 36/week @ 2023-11-29 13/week @ 2023-12-06 27/week @ 2023-12-13 52/week @ 2023-12-20 41/week @ 2023-12-27 100/week @ 2024-01-03 10/week @ 2024-01-10 55/week @ 2024-01-17 13/week @ 2024-01-24 31/week @ 2024-01-31 5/week @ 2024-02-07 83/week @ 2024-02-14 302/week @ 2024-02-21

430 downloads per month

MIT/Apache

30KB
593 lines

gauth

CodeScene Code Health

The library supports the following Google Auth flows:

[dependencies]
gauth = "0.7"

OAuth2

  1. Create your application in Google API Console
    a. Credentials > Create credentials > OAuth client ID
    b. Set application type to Other
    c. Enter your application name
    d. Download JSON configuration of the newly created application

Client implementation with defaults

use gauth::app::Auth;

#[tokio::main]
async fn main() {
    let auth_client = Auth::from_file(
        "my_credentials.json",
        vec!["https://www.googleapis.com/auth/drive"],
    )
    .unwrap();

    let token = auth_client.access_token().await.unwrap();
    println!("access token: {}", token);
}

It is also possible to make a blocking call to retrieve an access token. This may be helpful if we want to wrap the logic into a closure.

[dependencies]
gauth = { version = "0.7", features = ["app-blocking"] }
use gauth::app::Auth;

#[tokio::main]
async fn main() {
    let ga = Auth::from_file(
        "client_secret.json",
        vec!["https://www.googleapis.com/auth/drive"]
    ).unwrap();

    let closure = move || {
        // add some logic here
        ga.access_token_blocking()
    };

    let token = closure().unwrap();
    println!("token from closure: {}", token);
}

Custom app name and handler: access token will be stored in $HOME/.{app_name}/access_token.json

To assign a custom directory as access token caching, set env var value: GAUTH_TOKEN_DIR

use gauth::app::Auth;
use anyhow::Error as AnyError;

#[tokio::main]
async fn main() {
    let auth_handler = |consent_uri: String| -> Result<String, AnyError> {
        // business logic
        Ok("auth_code".to_owned())
    };

    let mut auth_client = Auth::from_file(
        "my_credentials.json",
        vec!["https://www.googleapis.com/auth/drive"],
    )
    .unwrap();

    let auth_client = auth_client.app_name("new_name").handler(auth_handler);
    let token = auth_client.access_token().await.unwrap();
    println!("access token: {}", token);
}

Service Account

Follow instructions for creating a service account. After a service account key has been created, it can be used to obtain an access token.

use gauth::serv_account::ServiceAccount;

#[tokio::main]
async fn access_token() {
    let scopes = vec!["https://www.googleapis.com/auth/drive"];
    let key_path = "test_fixtures/service-account-key.json";

    let mut service_account = ServiceAccount::from_file(key_path, scopes);
    let access_token = service_account.access_token().await.unwrap();

    println!("access token {}:", access_token);
}

License

License under either or:

Dependencies

~10–24MB
~468K SLoC