#google-cloud #oauth #cloud #google #server #engine

goauth

Crate for authenticating Server to Server Apps for Google Cloud Engine

35 releases (16 breaking)

0.17.0-alpha.1 Jun 17, 2024
0.15.0 Jun 17, 2024
0.14.0 Oct 17, 2023
0.13.1 Jul 20, 2022
0.1.2 Dec 29, 2016

#132 in Web programming

Download history 65357/week @ 2024-08-21 50412/week @ 2024-08-28 64319/week @ 2024-09-04 48777/week @ 2024-09-11 51709/week @ 2024-09-18 55615/week @ 2024-09-25 57470/week @ 2024-10-02 44671/week @ 2024-10-09 68703/week @ 2024-10-16 54112/week @ 2024-10-23 51752/week @ 2024-10-30 38118/week @ 2024-11-06 116653/week @ 2024-11-13 73146/week @ 2024-11-20 49170/week @ 2024-11-27 80756/week @ 2024-12-04

329,254 downloads per month
Used in 95 crates (12 directly)

MIT license

59KB
1K SLoC

build MIT licensed

rust-goauth [docs]

Crate for using OAuth 2.0 with Server to Server Applications for Google Cloud Engine, with tentative support for all supported Scopes. Supports sync or async requests via Futures.

Provides a serialisable Token struct for use in other applications that require authenticated interactions with Google Cloud.

Usage

#[macro_use]
extern crate log;

use goauth::auth::JwtClaims;
use goauth::scopes::Scope;
use goauth::{get_token, get_token_blocking, GoErr};
use goauth::credentials::Credentials;
use goauth::fetcher::TokenFetcher;
use smpl_jwt::{RSAKey, Jwt};
use time::Duration;

fn main() -> Result<(), GoErr>{
  let token_url = "https://www.googleapis.com/oauth2/v4/token";
  let iss = "<some-iss>"; // https://developers.google.com/identity/protocols/OAuth2ServiceAccount

  let credentials = Credentials::from_file("dummy_credentials_file_for_tests.json").unwrap();
  let claims = JwtClaims::new(String::from(iss),
                             &[Scope::DevStorageReadWrite],
                             String::from(token_url),
                             None, None);
  let jwt = Jwt::new(claims, credentials.rsa_key().unwrap(), None);

  // Use async
  let token = async {
    match get_token(&jwt, &credentials).await {
      Ok(token) => token,
      Err(e) => panic!(e)
    }
  };

  // Or sync
  let token = get_token_blocking(&jwt, &credentials)?;

  // Token provides `access_token` method that outputs a value that should be placed in the Authorization header

  // Or use the TokenFetcher abstraction which will automatically refresh tokens
  let fetcher = TokenFetcher::new(jwt, credentials, Duration::new(1, 0));

  let token = async {
    match fetcher.fetch_token().await {
      Ok(token) => token,
      Err(e) => panic!(e)
    }
  };

  // Now a couple seconds later we want the token again - the initial token is cached so it will re-use
  // the same token, saving a network trip to fetch another token
  let new_token = async {
    match fetcher.fetch_token().await {
      Ok(token) => token,
      Err(e) => panic!(e)
    }
  };

  assert_eq!(token, new_token);

  // Now say the token has expired or is close to expiring ("close" defined by the configurable
  // `refresh_buffer` parameter) at this point "later in the program." The next call to
  // `fetch_token` will notice this and automatically fetch a new token, cache it, and return it.
  let new_token = async {
    match fetcher.fetch_token().await {
      Ok(token) => token,
      Err(e) => panic!(e)
    }
  };

  assert_ne!(token, new_token);

  Ok(())
}

Dependencies

~5.5–8.5MB
~160K SLoC