Skip to content

Commit

Permalink
SM-1096: Refactor state_file_path and others to state_path
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonhurst committed Feb 6, 2024
1 parent 69a5d7b commit f248be0
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crates/bitwarden/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/auth/client_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions crates/bitwarden/src/auth/login/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 32 in crates/bitwarden/src/auth/login/access_token.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/access_token.rs#L32

Added line #L32 was not covered by tests
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()),

Check warning on line 37 in crates/bitwarden/src/auth/login/access_token.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/access_token.rs#L37

Added line #L37 was not covered by tests
},
));

Expand Down Expand Up @@ -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);

Check warning on line 80 in crates/bitwarden/src/auth/login/access_token.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/access_token.rs#L80

Added line #L80 was not covered by tests
}

client.set_tokens(
Expand All @@ -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(),
},
));

Expand All @@ -111,10 +111,10 @@ async fn request_access_token(

fn load_tokens_from_state(
client: &mut Client,
state_file: &Path,
state_path: &Path,

Check warning on line 114 in crates/bitwarden/src/auth/login/access_token.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/access_token.rs#L114

Added line #L114 was not covered by tests
access_token: &AccessToken,
) -> Result<Uuid> {
let client_state = state::get(state_file, access_token)?;
let client_state = state::get(state_path, access_token)?;

Check warning on line 117 in crates/bitwarden/src/auth/login/access_token.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/access_token.rs#L117

Added line #L117 was not covered by tests

let token: JWTToken = client_state.token.parse()?;

Expand Down Expand Up @@ -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<PathBuf>,
pub state_path: Option<PathBuf>,
}

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand Down
8 changes: 4 additions & 4 deletions crates/bitwarden/src/auth/renew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check warning on line 49 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L49

Added line #L49 was not covered by tests
..
} => {
let result = AccessTokenRequest::new(
Expand All @@ -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),

Check warning on line 61 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L61

Added line #L61 was not covered by tests
Ok(enc_settings),
) = (&result, state_file, client.get_encryption_settings())
) = (&result, state_path, client.get_encryption_settings())

Check warning on line 63 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L63

Added line #L63 was not covered by tests
{
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);

Check warning on line 68 in crates/bitwarden/src/auth/renew.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/renew.rs#L68

Added line #L68 was not covered by tests
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) enum ServiceAccountLoginMethod {
AccessToken {
access_token: AccessToken,
organization_id: Uuid,
state_file: Option<PathBuf>,
state_path: Option<PathBuf>,
},
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
Expand Down
8 changes: 4 additions & 4 deletions crates/bitwarden/src/secrets_manager/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl ClientState {
}
}

pub fn get(state_file: &Path, access_token: &AccessToken) -> Result<ClientState> {
let file_content = std::fs::read_to_string(state_file)?;
pub fn get(state_path: &Path, access_token: &AccessToken) -> Result<ClientState> {
let file_content = std::fs::read_to_string(state_path)?;

Check warning on line 32 in crates/bitwarden/src/secrets_manager/state.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/state.rs#L31-L32

Added lines #L31 - L32 were not covered by tests

let encrypted_state: EncString = file_content.parse()?;
let decrypted_state: String = encrypted_state.decrypt_with_key(&access_token.encryption_key)?;
Expand All @@ -42,12 +42,12 @@ pub fn get(state_file: &Path, access_token: &AccessToken) -> Result<ClientState>
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<()> {

Check warning on line 45 in crates/bitwarden/src/secrets_manager/state.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/state.rs#L45

Added line #L45 was not covered by tests
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)

Check warning on line 51 in crates/bitwarden/src/secrets_manager/state.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/secrets_manager/state.rs#L51

Added line #L51 was not covered by tests
.map_err(|_| "Failure writing to the state file.".into())
}
4 changes: 2 additions & 2 deletions crates/bws/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 326 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L326

Added line #L326 was not covered by tests
profile.and_then(|p| p.state_file_dir).map(Into::into),
access_token_obj.access_token_id.to_string(),
);
Expand All @@ -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,

Check warning on line 338 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L338

Added line #L338 was not covered by tests
})
.await?;

Expand Down
5 changes: 2 additions & 3 deletions languages/python/bitwarden_sdk/bitwarden_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit f248be0

Please sign in to comment.