From f248be0ee83611964fcb4b1a12bd7cc550b517fb Mon Sep 17 00:00:00 2001 From: Colton Hurst Date: Mon, 5 Feb 2024 20:51:14 -0500 Subject: [PATCH] SM-1096: Refactor state_file_path and others to state_path --- crates/bitwarden/README.md | 2 +- crates/bitwarden/src/auth/client_auth.rs | 2 +- .../bitwarden/src/auth/login/access_token.rs | 18 +++++++++--------- crates/bitwarden/src/auth/renew.rs | 8 ++++---- crates/bitwarden/src/client/client.rs | 2 +- crates/bitwarden/src/lib.rs | 2 +- crates/bitwarden/src/secrets_manager/state.rs | 8 ++++---- crates/bws/src/main.rs | 4 ++-- .../python/bitwarden_sdk/bitwarden_client.py | 5 ++--- 9 files changed, 25 insertions(+), 26 deletions(-) diff --git a/crates/bitwarden/README.md b/crates/bitwarden/README.md index abb2b5dd4..5bc634740 100644 --- a/crates/bitwarden/README.md +++ b/crates/bitwarden/README.md @@ -41,7 +41,7 @@ async fn test() -> Result<()> { let mut client = Client::new(Some(settings)); // Before we operate, we need to authenticate with a token - let token = AccessTokenLoginRequest { access_token: String::from(""), state_file: None }; + let token = AccessTokenLoginRequest { access_token: String::from(""), state_path: None }; client.auth().login_access_token(&token).await.unwrap(); let org_id = SecretIdentifiersRequest { organization_id: Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap() }; diff --git a/crates/bitwarden/src/auth/client_auth.rs b/crates/bitwarden/src/auth/client_auth.rs index f08daf0b7..47979522a 100644 --- a/crates/bitwarden/src/auth/client_auth.rs +++ b/crates/bitwarden/src/auth/client_auth.rs @@ -227,7 +227,7 @@ mod tests { .auth() .login_access_token(&AccessTokenLoginRequest { access_token: "0.ec2c1d46-6a4b-4751-a310-af9601317f2d.C2IgxjjLF7qSshsbwe8JGcbM075YXw:X8vbvA0bduihIDe/qrzIQQ==".into(), - state_file: None, + state_path: None, }) .await .unwrap(); diff --git a/crates/bitwarden/src/auth/login/access_token.rs b/crates/bitwarden/src/auth/login/access_token.rs index d540b31a8..313f0be3c 100644 --- a/crates/bitwarden/src/auth/login/access_token.rs +++ b/crates/bitwarden/src/auth/login/access_token.rs @@ -28,13 +28,13 @@ pub(crate) async fn login_access_token( let access_token: AccessToken = input.access_token.parse()?; - if let Some(state_file) = &input.state_file { - if let Ok(organization_id) = load_tokens_from_state(client, state_file, &access_token) { + if let Some(state_path) = &input.state_path { + if let Ok(organization_id) = load_tokens_from_state(client, state_path, &access_token) { client.set_login_method(LoginMethod::ServiceAccount( ServiceAccountLoginMethod::AccessToken { access_token, organization_id, - state_file: Some(state_file.to_path_buf()), + state_path: Some(state_path.to_path_buf()), }, )); @@ -75,9 +75,9 @@ pub(crate) async fn login_access_token( .parse() .map_err(|_| Error::InvalidResponse)?; - if let Some(state_file) = &input.state_file { + if let Some(state_path) = &input.state_path { let state = ClientState::new(r.access_token.clone(), payload.encryption_key); - _ = state::set(state_file, &access_token, state); + _ = state::set(state_path, &access_token, state); } client.set_tokens( @@ -89,7 +89,7 @@ pub(crate) async fn login_access_token( ServiceAccountLoginMethod::AccessToken { access_token, organization_id, - state_file: input.state_file.clone(), + state_path: input.state_path.clone(), }, )); @@ -111,10 +111,10 @@ async fn request_access_token( fn load_tokens_from_state( client: &mut Client, - state_file: &Path, + state_path: &Path, access_token: &AccessToken, ) -> Result { - let client_state = state::get(state_file, access_token)?; + let client_state = state::get(state_path, access_token)?; let token: JWTToken = client_state.token.parse()?; @@ -143,7 +143,7 @@ fn load_tokens_from_state( pub struct AccessTokenLoginRequest { /// Bitwarden service API access token pub access_token: String, - pub state_file: Option, + pub state_path: Option, } #[derive(Serialize, Deserialize, Debug, JsonSchema)] diff --git a/crates/bitwarden/src/auth/renew.rs b/crates/bitwarden/src/auth/renew.rs index 6f93f7482..6c070dcc9 100644 --- a/crates/bitwarden/src/auth/renew.rs +++ b/crates/bitwarden/src/auth/renew.rs @@ -46,7 +46,7 @@ pub(crate) async fn renew_token(client: &mut Client) -> Result<()> { LoginMethod::ServiceAccount(s) => match s { ServiceAccountLoginMethod::AccessToken { access_token, - state_file, + state_path, .. } => { let result = AccessTokenRequest::new( @@ -58,14 +58,14 @@ pub(crate) async fn renew_token(client: &mut Client) -> Result<()> { if let ( IdentityTokenResponse::Authenticated(r), - Some(state_file), + Some(state_path), Ok(enc_settings), - ) = (&result, state_file, client.get_encryption_settings()) + ) = (&result, state_path, client.get_encryption_settings()) { if let Some(enc_key) = enc_settings.get_key(&None) { let state = ClientState::new(r.access_token.clone(), enc_key.to_base64()); - _ = state::set(state_file, access_token, state); + _ = state::set(state_path, access_token, state); } } diff --git a/crates/bitwarden/src/client/client.rs b/crates/bitwarden/src/client/client.rs index 5b2c8b7e0..fced884d0 100644 --- a/crates/bitwarden/src/client/client.rs +++ b/crates/bitwarden/src/client/client.rs @@ -66,7 +66,7 @@ pub(crate) enum ServiceAccountLoginMethod { AccessToken { access_token: AccessToken, organization_id: Uuid, - state_file: Option, + state_path: Option, }, } diff --git a/crates/bitwarden/src/lib.rs b/crates/bitwarden/src/lib.rs index 28eb521de..116b872f2 100644 --- a/crates/bitwarden/src/lib.rs +++ b/crates/bitwarden/src/lib.rs @@ -38,7 +38,7 @@ //! let mut client = Client::new(Some(settings)); //! //! // Before we operate, we need to authenticate with a token -//! let token = AccessTokenLoginRequest { access_token: String::from(""), state_file: None }; +//! let token = AccessTokenLoginRequest { access_token: String::from(""), state_path: None }; //! client.auth().login_access_token(&token).await.unwrap(); //! //! let org_id = SecretIdentifiersRequest { organization_id: Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap() }; diff --git a/crates/bitwarden/src/secrets_manager/state.rs b/crates/bitwarden/src/secrets_manager/state.rs index 4efa4403b..5ab11ed1a 100644 --- a/crates/bitwarden/src/secrets_manager/state.rs +++ b/crates/bitwarden/src/secrets_manager/state.rs @@ -28,8 +28,8 @@ impl ClientState { } } -pub fn get(state_file: &Path, access_token: &AccessToken) -> Result { - let file_content = std::fs::read_to_string(state_file)?; +pub fn get(state_path: &Path, access_token: &AccessToken) -> Result { + let file_content = std::fs::read_to_string(state_path)?; let encrypted_state: EncString = file_content.parse()?; let decrypted_state: String = encrypted_state.decrypt_with_key(&access_token.encryption_key)?; @@ -42,12 +42,12 @@ pub fn get(state_file: &Path, access_token: &AccessToken) -> Result Ok(client_state) } -pub fn set(state_file: &Path, access_token: &AccessToken, state: ClientState) -> Result<()> { +pub fn set(state_path: &Path, access_token: &AccessToken, state: ClientState) -> Result<()> { let serialized_state: String = serde_json::to_string(&state)?; let encrypted_state: EncString = serialized_state.encrypt_with_key(&access_token.encryption_key)?; let state_string: String = encrypted_state.to_string(); - std::fs::write(state_file, state_string) + std::fs::write(state_path, state_string) .map_err(|_| "Failure writing to the state file.".into()) } diff --git a/crates/bws/src/main.rs b/crates/bws/src/main.rs index e55df5082..cfc2db22a 100644 --- a/crates/bws/src/main.rs +++ b/crates/bws/src/main.rs @@ -323,7 +323,7 @@ async fn process_commands() -> Result<()> { }) .transpose()?; - let state_file_path = state::get_state_file_path( + let state_path = state::get_state_file_path( profile.and_then(|p| p.state_file_dir).map(Into::into), access_token_obj.access_token_id.to_string(), ); @@ -335,7 +335,7 @@ async fn process_commands() -> Result<()> { .auth() .login_access_token(&AccessTokenLoginRequest { access_token, - state_file: state_file_path, + state_path: state_path, }) .await?; diff --git a/languages/python/bitwarden_sdk/bitwarden_client.py b/languages/python/bitwarden_sdk/bitwarden_client.py index b5ea48c44..b2717f245 100644 --- a/languages/python/bitwarden_sdk/bitwarden_client.py +++ b/languages/python/bitwarden_sdk/bitwarden_client.py @@ -12,10 +12,9 @@ def __init__(self, settings: ClientSettings = None): settings_json = json.dumps(settings.to_dict()) self.inner = bitwarden_py.BitwardenClient(settings_json) - def access_token_login(self, access_token: str, - state_file_path: str = None): + def access_token_login(self, access_token: str, state_path: str = None): self._run_command( - Command(access_token_login=AccessTokenLoginRequest(access_token, state_file_path)) + Command(access_token_login=AccessTokenLoginRequest(access_token, state_path)) ) def secrets(self):