Skip to content

Commit

Permalink
Modularize retrieval of AuthorizedUserRefreshToken data
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed May 27, 2024
1 parent 3a33cd3 commit e01b5fd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/config_default_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fs;
use std::sync::Arc;

use async_trait::async_trait;
Expand Down Expand Up @@ -35,11 +34,7 @@ impl ConfigDefaultCredentials {
let mut home = home::home_dir().ok_or(Error::Str("home directory not found"))?;
home.push(USER_CREDENTIALS_PATH);

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

let credentials = AuthorizedUserRefreshToken::from_file(&home)?;
debug!(project = ?credentials.quota_project_id, client = credentials.client_id, "found user credentials");

Ok(Self {
Expand Down
9 changes: 9 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@ pub(crate) struct AuthorizedUserRefreshToken {
pub(crate) refresh_token: String,
}

impl AuthorizedUserRefreshToken {
pub(crate) fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
let file = File::open(path.as_ref())
.map_err(|err| Error::Io("failed to open application credentials file", err))?;
serde_json::from_reader(file)
.map_err(|err| Error::Json("failed to deserialize ApplicationCredentials", err))
}
}

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

0 comments on commit e01b5fd

Please sign in to comment.