Skip to content

Commit

Permalink
Rename credential types
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed May 27, 2024
1 parent 4deb466 commit e3e967b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
11 changes: 7 additions & 4 deletions src/config_default_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::Serialize;
use tokio::sync::RwLock;
use tracing::{debug, instrument, Level};

use crate::types::{HttpClient, Token, UserCredentials};
use crate::types::{AuthorizedUserRefreshToken, HttpClient, Token};
use crate::{Error, TokenProvider};

/// A token provider that uses the default user credentials
Expand All @@ -20,7 +20,7 @@ use crate::{Error, TokenProvider};
pub struct ConfigDefaultCredentials {
client: HttpClient,
token: RwLock<Arc<Token>>,
credentials: UserCredentials,
credentials: AuthorizedUserRefreshToken,
}

impl ConfigDefaultCredentials {
Expand All @@ -37,7 +37,7 @@ impl ConfigDefaultCredentials {

let file = fs::File::open(home)
.map_err(|err| Error::Io("failed to open user credentials path", err))?;
let credentials = serde_json::from_reader::<_, UserCredentials>(file)
let credentials = serde_json::from_reader::<_, AuthorizedUserRefreshToken>(file)
.map_err(|err| Error::Json("failed to deserialize UserCredentials", err))?;

debug!(project = ?credentials.quota_project_id, client = credentials.client_id, "found user credentials");
Expand All @@ -50,7 +50,10 @@ impl ConfigDefaultCredentials {
}

#[instrument(level = Level::DEBUG, skip(cred, client))]
async fn fetch_token(cred: &UserCredentials, client: &HttpClient) -> Result<Arc<Token>, Error> {
async fn fetch_token(
cred: &AuthorizedUserRefreshToken,
client: &HttpClient,
) -> Result<Arc<Token>, Error> {
client
.token(
&|| {
Expand Down
18 changes: 7 additions & 11 deletions src/custom_service_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tokio::sync::RwLock;
use tracing::{debug, instrument, Level};
use url::form_urlencoded;

use crate::types::{ApplicationCredentials, HttpClient, Signer, Token};
use crate::types::{HttpClient, ServiceAccountKey, Signer, Token};
use crate::{Error, TokenProvider};

/// A custom service account containing credentials
Expand All @@ -27,7 +27,7 @@ use crate::{Error, TokenProvider};
#[derive(Debug)]
pub struct CustomServiceAccount {
client: HttpClient,
credentials: ApplicationCredentials,
credentials: ServiceAccountKey,
signer: Signer,
tokens: RwLock<HashMap<Vec<String>, Arc<Token>>>,
subject: Option<String>,
Expand All @@ -37,20 +37,20 @@ impl CustomServiceAccount {
/// Check `GOOGLE_APPLICATION_CREDENTIALS` environment variable for a path to JSON credentials
pub fn from_env() -> Result<Option<Self>, Error> {
debug!("check for GOOGLE_APPLICATION_CREDENTIALS env var");
match ApplicationCredentials::from_env()? {
match ServiceAccountKey::from_env()? {
Some(credentials) => Self::new(credentials, HttpClient::new()?).map(Some),
None => Ok(None),
}
}

/// Read service account credentials from the given JSON file
pub fn from_file<T: AsRef<Path>>(path: T) -> Result<Self, Error> {
Self::new(ApplicationCredentials::from_file(path)?, HttpClient::new()?)
Self::new(ServiceAccountKey::from_file(path)?, HttpClient::new()?)
}

/// Read service account credentials from the given JSON string
pub fn from_json(s: &str) -> Result<Self, Error> {
Self::new(ApplicationCredentials::from_str(s)?, HttpClient::new()?)
Self::new(ServiceAccountKey::from_str(s)?, HttpClient::new()?)
}

/// Set the `subject` to impersonate a user
Expand All @@ -59,7 +59,7 @@ impl CustomServiceAccount {
self
}

fn new(credentials: ApplicationCredentials, client: HttpClient) -> Result<Self, Error> {
fn new(credentials: ServiceAccountKey, client: HttpClient) -> Result<Self, Error> {
debug!(project = ?credentials.project_id, email = credentials.client_email, "found credentials");
Ok(Self {
client,
Expand Down Expand Up @@ -156,11 +156,7 @@ pub(crate) struct Claims<'a> {
}

impl<'a> Claims<'a> {
pub(crate) fn new(
key: &'a ApplicationCredentials,
scopes: &[&str],
sub: Option<&'a str>,
) -> Self {
pub(crate) fn new(key: &'a ServiceAccountKey, scopes: &[&str], sub: Option<&'a str>) -> Self {
let mut scope = String::with_capacity(16);
for (i, s) in scopes.iter().enumerate() {
if i != 0 {
Expand Down
12 changes: 6 additions & 6 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ where
}

#[derive(Deserialize)]
pub(crate) struct ApplicationCredentials {
pub(crate) struct ServiceAccountKey {
/// project_id
pub(crate) project_id: Option<Arc<str>>,
/// private_key
Expand All @@ -235,7 +235,7 @@ pub(crate) struct ApplicationCredentials {
pub(crate) token_uri: String,
}

impl ApplicationCredentials {
impl ServiceAccountKey {
pub(crate) fn from_env() -> Result<Option<Self>, Error> {
env::var_os("GOOGLE_APPLICATION_CREDENTIALS")
.map(|path| {
Expand All @@ -256,7 +256,7 @@ impl ApplicationCredentials {
}
}

impl FromStr for ApplicationCredentials {
impl FromStr for ServiceAccountKey {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand All @@ -265,7 +265,7 @@ impl FromStr for ApplicationCredentials {
}
}

impl fmt::Debug for ApplicationCredentials {
impl fmt::Debug for ServiceAccountKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ApplicationCredentials")
.field("client_email", &self.client_email)
Expand All @@ -275,7 +275,7 @@ impl fmt::Debug for ApplicationCredentials {
}

#[derive(Deserialize)]
pub(crate) struct UserCredentials {
pub(crate) struct AuthorizedUserRefreshToken {
/// Client id
pub(crate) client_id: String,
/// Client secret
Expand All @@ -286,7 +286,7 @@ pub(crate) struct UserCredentials {
pub(crate) refresh_token: String,
}

impl fmt::Debug for UserCredentials {
impl fmt::Debug for AuthorizedUserRefreshToken {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("UserCredentials")
.field("client_id", &self.client_id)
Expand Down

0 comments on commit e3e967b

Please sign in to comment.