pub struct AuthenticationHandler { /* private fields */ }
Expand description
Authentication handler for storing json credentials and requesting new access_token then necessary.
More details: https://developers.google.com/identity/protocols/oauth2/service-account
//Example if json credentials are stored at the same directory where the program is contained.
let mut dir = env::current_exe().unwrap();
dir.pop();
dir.push("some-name-431008-92e3a679a62f.json");
let json_string = json!({
"type": "service_account",
"project_id": "some-name-0000000",
"private_key_id": "somerandomuuid000000000",
"private_key": "-----BEGIN PRIVATE KEY-----\n SOME CERT DATA \n-----END PRIVATE KEY-----\n",
"client_email": "some-name@some-account-0000000.iam.gserviceaccount.com",
"client_id": "000000000000000",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/some-account.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}).to_string();
//Create the handler.
let handler = AuthenticationHandler::new(dir.into());
//Handler using json `String`
let handler_2 = AuthenticationHandler::new(json_string.into());
//Get a token with scoped read / write access to GCP DNS API.
let token = handler.get_access_token_model(
vec!["https://www.googleapis.com/auth/ndev.clouddns.readwrite".into()]);
println!("Access Token: {}", token.access_token);
Implementations§
Source§impl AuthenticationHandler
impl AuthenticationHandler
Sourcepub fn new(creds: ServiceCredentialsInput) -> AuthenticationHandler
pub fn new(creds: ServiceCredentialsInput) -> AuthenticationHandler
Creates new AuthenticationHandler
. Requires a PathBuf
or json String
containing the service account credentials (key).
Sourcepub fn get_access_token_model(&self, scopes: Vec<String>) -> AuthResponse
pub fn get_access_token_model(&self, scopes: Vec<String>) -> AuthResponse
Creates new access_token
with specific access. Please for complete scopes list refer to: https://developers.google.com/identity/protocols/oauth2/scopes
. Make sure to give the respective access /role to the service account.
Auto Trait Implementations§
impl Freeze for AuthenticationHandler
impl RefUnwindSafe for AuthenticationHandler
impl Send for AuthenticationHandler
impl Sync for AuthenticationHandler
impl Unpin for AuthenticationHandler
impl UnwindSafe for AuthenticationHandler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more