solana/storage-bigtable/src/access_token.rs

129 lines
4.0 KiB
Rust
Raw Normal View History

pub use goauth::scopes::Scope;
2020-07-17 13:31:10 -07:00
/// A module for managing a Google API access token
use {
crate::CredentialType,
goauth::{
auth::{JwtClaims, Token},
credentials::Credentials,
},
log::*,
smpl_jwt::Jwt,
std::{
str::FromStr,
sync::{
atomic::{AtomicBool, Ordering},
{Arc, RwLock},
},
time::Instant,
2020-07-24 13:53:02 -07:00
},
};
2020-07-17 13:31:10 -07:00
fn load_credentials(filepath: Option<String>) -> Result<Credentials, String> {
let path = match filepath {
Some(f) => f,
None => std::env::var("GOOGLE_APPLICATION_CREDENTIALS").map_err(|_| {
"GOOGLE_APPLICATION_CREDENTIALS environment variable not found".to_string()
})?,
};
Credentials::from_file(&path)
.map_err(|err| format!("Failed to read GCP credentials from {}: {}", path, err))
2020-07-17 13:31:10 -07:00
}
fn load_stringified_credentials(credential: String) -> Result<Credentials, String> {
Credentials::from_str(&credential).map_err(|err| format!("{}", err))
}
2020-07-24 13:53:02 -07:00
#[derive(Clone)]
2020-07-17 13:31:10 -07:00
pub struct AccessToken {
credentials: Credentials,
2020-07-24 13:53:02 -07:00
scope: Scope,
refresh_active: Arc<AtomicBool>,
token: Arc<RwLock<(Token, Instant)>>,
2020-07-17 13:31:10 -07:00
}
impl AccessToken {
pub async fn new(scope: Scope, credential_type: CredentialType) -> Result<Self, String> {
let credentials = match credential_type {
CredentialType::Filepath(fp) => load_credentials(fp)?,
CredentialType::Stringified(s) => load_stringified_credentials(s)?,
};
2020-07-24 13:53:02 -07:00
if let Err(err) = credentials.rsa_key() {
Err(format!("Invalid rsa key: {}", err))
} else {
let token = Arc::new(RwLock::new(Self::get_token(&credentials, &scope).await?));
let access_token = Self {
credentials,
scope,
token,
refresh_active: Arc::new(AtomicBool::new(false)),
};
Ok(access_token)
}
}
2020-07-17 13:31:10 -07:00
2020-07-24 13:53:02 -07:00
/// The project that this token grants access to
pub fn project(&self) -> String {
self.credentials.project()
}
async fn get_token(
credentials: &Credentials,
scope: &Scope,
) -> Result<(Token, Instant), String> {
info!("Requesting token for {:?} scope", scope);
2020-07-17 13:31:10 -07:00
let claims = JwtClaims::new(
credentials.iss(),
2020-07-24 13:53:02 -07:00
scope,
2020-07-17 13:31:10 -07:00
credentials.token_uri(),
None,
None,
);
2020-07-24 13:53:02 -07:00
let jwt = Jwt::new(claims, credentials.rsa_key().unwrap(), None);
2020-07-17 13:31:10 -07:00
2020-07-24 13:53:02 -07:00
let token = goauth::get_token(&jwt, credentials)
.await
.map_err(|err| format!("Failed to refresh access token: {}", err))?;
2020-07-17 13:31:10 -07:00
2020-07-24 13:53:02 -07:00
info!("Token expires in {} seconds", token.expires_in());
Ok((token, Instant::now()))
2020-07-17 13:31:10 -07:00
}
2020-07-24 13:53:02 -07:00
/// Call this function regularly to ensure the access token does not expire
pub async fn refresh(&self) {
// Check if it's time to try a token refresh
{
let token_r = self.token.read().unwrap();
if token_r.1.elapsed().as_secs() < token_r.0.expires_in() as u64 / 2 {
return;
}
2021-01-23 11:55:15 -08:00
#[allow(deprecated)]
2020-07-24 13:53:02 -07:00
if self
.refresh_active
.compare_and_swap(false, true, Ordering::Relaxed)
{
// Refresh already pending
2020-07-17 13:31:10 -07:00
return;
}
}
info!("Refreshing token");
2020-07-24 13:53:02 -07:00
let new_token = Self::get_token(&self.credentials, &self.scope).await;
{
let mut token_w = self.token.write().unwrap();
match new_token {
Ok(new_token) => *token_w = new_token,
Err(err) => warn!("{}", err),
2020-07-17 13:31:10 -07:00
}
2020-07-24 13:53:02 -07:00
self.refresh_active.store(false, Ordering::Relaxed);
2020-07-17 13:31:10 -07:00
}
}
/// Return an access token suitable for use in an HTTP authorization header
2020-07-24 13:53:02 -07:00
pub fn get(&self) -> String {
let token_r = self.token.read().unwrap();
format!("{} {}", token_r.0.token_type(), token_r.0.access_token())
2020-07-17 13:31:10 -07:00
}
}