Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
eliykat committed Oct 26, 2023
1 parent a1010d4 commit 6058e32
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 79 deletions.
9 changes: 7 additions & 2 deletions crates/bitwarden/src/admin_console/auth_requests/approve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub(crate) async fn approve_auth_request(
client: &mut Client,
input: &AuthApproveRequest,
) -> Result<()> {
let device_request = get_pending_request(input.organization_id, input.organization_user_id, client).await;
let device_request =
get_pending_request(input.organization_id, input.organization_user_id, client).await;

// Get user reset password details
let reset_password_details =
Expand Down Expand Up @@ -58,7 +59,11 @@ pub(crate) async fn approve_auth_request(
Ok(())
}

async fn get_pending_request(organization_id: Uuid, organization_user_id: Uuid, client: &mut Client) -> PendingAuthRequestResponse {
async fn get_pending_request(
organization_id: Uuid,
organization_user_id: Uuid,
client: &mut Client,
) -> PendingAuthRequestResponse {
// hack: get all approval details and then find the one we want
// when we settle on an identifier then we should just give ourselves a better server API
// or do we require the caller to pass all this info in?
Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden/src/admin_console/auth_requests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod list;
mod approve;
mod list;

pub(crate) use list::list_pending_requests;
pub use list::{
PendingAuthRequestsRequest, PendingAuthRequestsResponse, PendingAuthRequestResponse
PendingAuthRequestResponse, PendingAuthRequestsRequest, PendingAuthRequestsResponse,
};

pub(crate) use approve::approve_auth_request;
Expand Down
13 changes: 6 additions & 7 deletions crates/bitwarden/src/admin_console/client_auth_requests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{
admin_console::auth_requests::{approve_auth_request, AuthApproveRequest},
admin_console::auth_requests::{
list_pending_requests, PendingAuthRequestsRequest, PendingAuthRequestsResponse,
},
error::Result,
Client,
admin_console::auth_requests::{PendingAuthRequestsRequest, PendingAuthRequestsResponse, list_pending_requests},
admin_console::auth_requests::{AuthApproveRequest, approve_auth_request}
};

pub struct ClientAuthRequests<'a> {
Expand All @@ -17,11 +19,8 @@ impl<'a> ClientAuthRequests<'a> {
list_pending_requests(self.client, input).await
}

pub async fn approve(
&mut self,
input: &AuthApproveRequest
) -> Result<()> {
approve_auth_request(self.client, input).await
pub async fn approve(&mut self, input: &AuthApproveRequest) -> Result<()> {
approve_auth_request(self.client, input).await
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/crypto/enc_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use uuid::Uuid;

use crate::{
client::encryption_settings::EncryptionSettings,
crypto::{decrypt_aes256_hmac, Decryptable, Encryptable, SymmetricCryptoKey, rsa::decrypt_rsa},
crypto::{decrypt_aes256_hmac, rsa::decrypt_rsa, Decryptable, Encryptable, SymmetricCryptoKey},
error::{CryptoError, EncStringParseError, Error, Result},
util::BASE64_ENGINE,
};
Expand Down
7 changes: 3 additions & 4 deletions crates/bitwarden/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ pub(crate) use user_key::UserKey;
// #[cfg(feature = "internal")]
mod rsa;
#[cfg(feature = "internal")]
pub use self::rsa::RsaKeyPair;
#[cfg(feature = "internal")]
pub use self::rsa::encrypt_rsa;
#[cfg(feature = "internal")]
pub use self::rsa::private_key_from_bytes;
#[cfg(feature = "internal")]
pub use self::rsa::public_key_from_b64;
#[cfg(feature = "internal")]
pub use self::rsa::private_key_from_bytes;
pub use self::rsa::RsaKeyPair;
#[cfg(feature = "internal")]

#[cfg(feature = "internal")]
mod fingerprint;
#[cfg(feature = "internal")]
Expand Down
77 changes: 39 additions & 38 deletions crates/bitwarden/src/crypto/rsa.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use base64::Engine;
use rsa::{
pkcs8::{der::Decode, EncodePrivateKey, EncodePublicKey, SubjectPublicKeyInfo, DecodePrivateKey},
RsaPrivateKey, RsaPublicKey,
Oaep
pkcs8::{
der::Decode, DecodePrivateKey, EncodePrivateKey, EncodePublicKey, SubjectPublicKeyInfo,
},
Oaep, RsaPrivateKey, RsaPublicKey,
};
use sha1::Sha1;

use crate::{
crypto::{encrypt_aes256_hmac, EncString, SymmetricCryptoKey},
error::{Error, Result, CryptoError},
error::{CryptoError, Error, Result},
util::BASE64_ENGINE,
};

Expand Down Expand Up @@ -44,14 +45,14 @@ pub(super) fn make_key_pair(key: &SymmetricCryptoKey) -> Result<RsaKeyPair> {
}

pub(super) fn decrypt_rsa(data: Vec<u8>, key: &RsaPrivateKey) -> Result<Vec<u8>> {
key.decrypt(Oaep::new::<Sha1>(), &data)
.map_err(|_| CryptoError::InvalidKey.into()) // need better error
key.decrypt(Oaep::new::<Sha1>(), &data)
.map_err(|_| CryptoError::InvalidKey.into()) // need better error
}

pub fn encrypt_rsa(data: Vec<u8>, key: &RsaPublicKey) -> Result<Vec<u8>> {
let mut rng = rand::thread_rng();
key.encrypt(&mut rng, Oaep::new::<Sha1>(), &data)
.map_err(|_| CryptoError::InvalidKey.into()) // need better error
let mut rng = rand::thread_rng();
key.encrypt(&mut rng, Oaep::new::<Sha1>(), &data)
.map_err(|_| CryptoError::InvalidKey.into()) // need better error
}

pub fn public_key_from_b64(b64: &str) -> Result<RsaPublicKey> {
Expand All @@ -61,17 +62,17 @@ pub fn public_key_from_b64(b64: &str) -> Result<RsaPublicKey> {
}

pub fn private_key_from_bytes(bytes: &Vec<u8>) -> Result<RsaPrivateKey> {
rsa::RsaPrivateKey::from_pkcs8_der(bytes).map_err(|_| Error::Crypto(CryptoError::InvalidKey))
rsa::RsaPrivateKey::from_pkcs8_der(bytes).map_err(|_| Error::Crypto(CryptoError::InvalidKey))
}

#[cfg(test)]
mod tests {
use base64::Engine;
use rsa::pkcs8::{DecodePrivateKey, der::Decode, SubjectPublicKeyInfo};
use crate::util::BASE64_ENGINE;
use super::*;
use super::*;
use crate::util::BASE64_ENGINE;
use base64::Engine;
use rsa::pkcs8::{der::Decode, DecodePrivateKey, SubjectPublicKeyInfo};

const PRIVATE_KEY_B64: &str = concat!(
const PRIVATE_KEY_B64: &str = concat!(
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXRVrCX+2hfOQS8Hz",
"YUS2oc/jGVTZpv+/Ryuoh9d8ihYX9dd0cYh2tl6KWdFc88lPUH11Oxqy20Rk2e5r/RF6T9yM0Me3NPnaKt+hlhLtfoc0h86L",
"nhD56A9FDUfuI0dVnPcrwNv0YJIo94LwxtbqBULNvXl6wJ7WAbODrCQy5ZgMVg+iH+gGpwiqsZqHt+KuoHWcN53MSPDfaF4/",
Expand All @@ -91,45 +92,45 @@ mod tests {
"+tPVgppLcG0+tMdLjigFQiDUQk2y3WjyxP5ZvXu7U96jaJRI8PFMoE06WeVYcdIzrID2HvqH+w0UQJFrLJ/0Mn4stFAEzXKZ",
"BokBGnjFnTnKcs7nv/O8=");

const PUBLIC_KEY_B64: &str = concat!(
const PUBLIC_KEY_B64: &str = concat!(
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl0Vawl/toXzkEvB82FEtqHP",
"4xlU2ab/v0crqIfXfIoWF/XXdHGIdrZeilnRXPPJT1B9dTsasttEZNnua/0Rek/cjNDHtzT52irfoZYS7X6HNIfOi54Q+egP",
"RQ1H7iNHVZz3K8Db9GCSKPeC8MbW6gVCzb15esCe1gGzg6wkMuWYDFYPoh/oBqcIqrGah7firqB1nDedzEjw32heP2DAffVN",
"084iTDjiWrJNUxBJ2pDD5Z9dT3MzQ2s09ew1yMWK2z37rT3YerC7OgEDmo3WYo3xL3qYJznu3EO2nmrYjiRa40wKSjxsTlUc",
"xDF+F0uMW8oR9EMUHgepdepfAtLsSAQIDAQAB");

const DATA_B64: &str = concat!(
const DATA_B64: &str = concat!(
"A1/p8BQzN9UrbdYxUY2Va5+kPLyfZXF9JsZrjeEXcaclsnHurdxVAJcnbEqYMP3UXV",
"4YAS/mpf+Rxe6/X0WS1boQdA0MAHSgx95hIlAraZYpiMLLiJRKeo2u8YivCdTM9V5vuAEJwf9Tof/qFsFci3sApdbATkorCT",
"zFOIEPF2S1zgperEP23M01mr4dWVdYN18B32YF67xdJHMbFhp5dkQwv9CmscoWq7OE5HIfOb+JAh7BEZb+CmKhM3yWJvoR/D",
"/5jcercUtK2o+XrzNrL4UQ7yLZcFz6Bfwb/j6ICYvqd/YJwXNE6dwlL57OfwJyCdw2rRYf0/qI00t9u8Iitw==");

#[test]
fn test_decrypt_rsa() {
let private_key_bytes = BASE64_ENGINE.decode(PRIVATE_KEY_B64).unwrap();
let private_key = rsa::RsaPrivateKey::from_pkcs8_der(&private_key_bytes).unwrap();
let data_bytes = BASE64_ENGINE.decode(DATA_B64).unwrap();
#[test]
fn test_decrypt_rsa() {
let private_key_bytes = BASE64_ENGINE.decode(PRIVATE_KEY_B64).unwrap();
let private_key = rsa::RsaPrivateKey::from_pkcs8_der(&private_key_bytes).unwrap();
let data_bytes = BASE64_ENGINE.decode(DATA_B64).unwrap();

let result = decrypt_rsa(data_bytes, &private_key).unwrap();
let result_string = String::from_utf8(result).unwrap();
let result = decrypt_rsa(data_bytes, &private_key).unwrap();
let result_string = String::from_utf8(result).unwrap();

assert_eq!(result_string, "EncryptMe!");
}
assert_eq!(result_string, "EncryptMe!");
}

#[test]
fn test_encrypt_rsa() {
let public_key_bytes = BASE64_ENGINE.decode(PUBLIC_KEY_B64).unwrap();
let info = SubjectPublicKeyInfo::from_der(&public_key_bytes).unwrap();
let public_key = RsaPublicKey::try_from(info).unwrap();
#[test]
fn test_encrypt_rsa() {
let public_key_bytes = BASE64_ENGINE.decode(PUBLIC_KEY_B64).unwrap();
let info = SubjectPublicKeyInfo::from_der(&public_key_bytes).unwrap();
let public_key = RsaPublicKey::try_from(info).unwrap();

let private_key_bytes = BASE64_ENGINE.decode(PRIVATE_KEY_B64).unwrap();
let private_key = rsa::RsaPrivateKey::from_pkcs8_der(&private_key_bytes).unwrap();
let private_key_bytes = BASE64_ENGINE.decode(PRIVATE_KEY_B64).unwrap();
let private_key = rsa::RsaPrivateKey::from_pkcs8_der(&private_key_bytes).unwrap();

let encrypted = encrypt_rsa("EncryptMe!".as_bytes().to_vec(), &public_key).unwrap();
let decrypted = decrypt_rsa(encrypted, &private_key).unwrap();
let encrypted = encrypt_rsa("EncryptMe!".as_bytes().to_vec(), &public_key).unwrap();
let decrypted = decrypt_rsa(encrypted, &private_key).unwrap();

let result_string = String::from_utf8(decrypted).unwrap();
let result_string = String::from_utf8(decrypted).unwrap();

assert_eq!(result_string, "EncryptMe!");
}
assert_eq!(result_string, "EncryptMe!");
}
}
2 changes: 1 addition & 1 deletion crates/bitwarden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#[cfg(feature = "mobile")]
uniffi::setup_scaffolding!();

pub mod admin_console;
pub mod auth;
pub mod client;
pub mod crypto;
Expand All @@ -69,7 +70,6 @@ mod util;
#[cfg(feature = "mobile")]
pub mod vault;
pub mod wordlist;
pub mod admin_console;

pub use client::Client;

Expand Down
58 changes: 36 additions & 22 deletions crates/bw/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use bitwarden::{
auth::RegisterRequest, client::client_settings::ClientSettings, tool::PasswordGeneratorRequest,
admin_console::auth_requests::{PendingAuthRequestsRequest, AuthApproveRequest}, Client
admin_console::auth_requests::{AuthApproveRequest, PendingAuthRequestsRequest},
auth::RegisterRequest,
client::client_settings::ClientSettings,
tool::PasswordGeneratorRequest,
Client,
};
use bitwarden_cli::{install_color_eyre, text_prompt_when_none, Color};
use clap::{command, Args, CommandFactory, Parser, Subcommand};
use color_eyre::eyre::Result;
use inquire::Password;
use render::{Output, serialize_response};
use render::{serialize_response, Output};
use uuid::Uuid;

mod auth;
Expand Down Expand Up @@ -100,8 +103,13 @@ enum GeneratorCommands {

#[derive(Subcommand, Clone)]
enum AdminConsoleCommands {
ListDevices { organization_id: Uuid },
ApproveDevice { organization_id: Uuid, organization_user_id: Uuid }
ListDevices {
organization_id: Uuid,
},
ApproveDevice {
organization_id: Uuid,
organization_user_id: Uuid,
},
}

#[derive(Args, Clone)]
Expand Down Expand Up @@ -176,7 +184,7 @@ async fn process_commands() -> Result<()> {
client_id,
client_secret,
} => {
auth::api_key_login(client, client_id, client_secret).await?;
auth::api_key_login(client, client_id, client_secret).await?;
}
}
return Ok(());
Expand Down Expand Up @@ -238,24 +246,30 @@ async fn process_commands() -> Result<()> {
GeneratorCommands::Passphrase {} => todo!(),
},
Commands::AdminConsole { command } => match command {
AdminConsoleCommands::ListDevices { organization_id } => {
let mut client = hack_login().await;
let auth_requests = client
.client_auth_requests()
.list(&PendingAuthRequestsRequest { organization_id })
.await?;
AdminConsoleCommands::ListDevices { organization_id } => {
let mut client = hack_login().await;
let auth_requests = client
.client_auth_requests()
.list(&PendingAuthRequestsRequest { organization_id })
.await?;

serialize_response(auth_requests.data, cli.output, false);
},
AdminConsoleCommands::ApproveDevice { organization_id, organization_user_id } => {
let mut client = hack_login().await;
client
.client_auth_requests()
.approve(&AuthApproveRequest { organization_id, organization_user_id })
.await
.unwrap(); // error handling?
}
}
}
AdminConsoleCommands::ApproveDevice {
organization_id,
organization_user_id,
} => {
let mut client = hack_login().await;
client
.client_auth_requests()
.approve(&AuthApproveRequest {
organization_id,
organization_user_id,
})
.await
.unwrap(); // error handling?
}
},
};

Ok(())
Expand Down
6 changes: 4 additions & 2 deletions crates/bw/src/render.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use bitwarden::admin_console::auth_requests::{PendingAuthRequestsResponse, PendingAuthRequestResponse};
use bitwarden::admin_console::auth_requests::{
PendingAuthRequestResponse, PendingAuthRequestsResponse,
};
use chrono::{DateTime, Utc};
use clap::ValueEnum;
use comfy_table::Table;
Expand Down Expand Up @@ -119,7 +121,7 @@ impl TableSerialize<4> for PendingAuthRequestResponse {
self.id.to_string(),
self.user_id.to_string(),
self.organization_user_id.to_string(),
self.email.clone()
self.email.clone(),
]]
}
}

0 comments on commit 6058e32

Please sign in to comment.