gauth
The library supports the following Google Auth flows:
[]
= "0.8"
OAuth2
- Create your application in Google API Console
a.Credentials>Create credentials>OAuth client ID
b. Set application type toOther
c. Enter your application name
d.Download JSONconfiguration of the newly created application
Client implementation with defaults
use Auth;
async
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.8", features = ["app-blocking"] }
use Auth;
async
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 Auth;
use Error as AnyError;
async
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 ServiceAccount;
async
Bridging sync and async code
The default implementation for acquiring the access token in this library is asynchronous. However, there are scenarios where a synchronous call is necessary. For instance, asynchronous signatures can be cumbersome when used with tonic middlewares. The difficulties of integrating synchronous and asynchronous code are outlined in this GitHub issue.
To resolve this, we adopted an experimental approach by developing a token_provider package. This package includes a Watcher trait, which has been implemented for both the app and serv_account packages. Each implementation of this trait spawns a daemon that periodically polls for and caches token updates at specified intervals. As a result, tokens are consistently refreshed through an asynchronous process. The retrieval of tokens is simplified to a synchronous function that reads from the internal cache.
[dependencies]
gauth = { version = "0.8", features = ["token-watcher"] }
let service_account = from_file;
let tp = new.with_interval;
// the token is updated every 5 seconds
// and cached in AsyncTokenProvider
tp.watch_updates.await;
// sync call to get the access token
let access_token = tp.access_token?;
The full example can be found here
License
License under either or: