From 9e850b1c167a64469faef00be64e4b68bd78dc8f Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 26 May 2024 17:33:10 +0200 Subject: [PATCH] Modularize retrieval of AuthorizedUserRefreshToken data --- src/config_default_credentials.rs | 7 +------ src/types.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/config_default_credentials.rs b/src/config_default_credentials.rs index 0b8aded..6f078fb 100644 --- a/src/config_default_credentials.rs +++ b/src/config_default_credentials.rs @@ -1,4 +1,3 @@ -use std::fs; use std::sync::Arc; use async_trait::async_trait; @@ -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 { diff --git a/src/types.rs b/src/types.rs index 1a48e23..d2d2c33 100644 --- a/src/types.rs +++ b/src/types.rs @@ -286,6 +286,15 @@ pub(crate) struct AuthorizedUserRefreshToken { pub(crate) refresh_token: String, } +impl AuthorizedUserRefreshToken { + pub(crate) fn from_file(path: impl AsRef) -> Result { + 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")