Skip to content

PoC for performing API requests using SDK #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bitwarden-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ macro_rules! impl_bitwarden_error {
/// Errors from performing network requests.
#[allow(missing_docs)]
#[derive(Debug, Error)]
#[bitwarden_error(flat)]
pub enum ApiError {
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
Expand Down
54 changes: 54 additions & 0 deletions crates/bitwarden-core/src/key_management/km_api_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use bitwarden_api_api::{
apis::accounts_key_management_api::accounts_key_management_regenerate_keys_post,
models::KeyRegenerationRequestModel,
};
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use tsify::Tsify;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::{ApiError, Client};

/// A client for the crypto operations.
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct KeyManagementApiClient {
pub(crate) client: crate::Client,
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl KeyManagementApiClient {
/// Performs API request to `accounts/key-management/regenerate-keys`.
pub async fn accounts_key_management_regenerate_keys_post(
&self,
req: KeyRegenerationRequest,
) -> Result<(), ApiError> {
let config = self.client.internal.get_api_configurations().await;
Ok(accounts_key_management_regenerate_keys_post(&config.api, Some(req.into())).await?)
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct KeyRegenerationRequest {
user_public_key: String,
user_key_encrypted_user_private_key: String,
}

impl From<KeyRegenerationRequest> for KeyRegenerationRequestModel {
fn from(req: KeyRegenerationRequest) -> Self {
KeyRegenerationRequestModel {
user_public_key: Some(req.user_public_key),
user_key_encrypted_user_private_key: Some(req.user_key_encrypted_user_private_key),
}
}
}

impl Client {
/// Temporary client for performing API requests for key management endpoints.
pub fn km_api(&self) -> KeyManagementApiClient {
KeyManagementApiClient {
client: self.clone(),
}
}
}
4 changes: 4 additions & 0 deletions crates/bitwarden-core/src/key_management/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub mod crypto;
mod crypto_client;
#[cfg(feature = "internal")]
pub use crypto_client::CryptoClient;
#[cfg(feature = "internal")]
mod km_api_client;
#[cfg(feature = "internal")]
pub use km_api_client::KeyManagementApiClient;

#[cfg(feature = "internal")]
mod security_state;
Expand Down
9 changes: 8 additions & 1 deletion crates/bitwarden-wasm-internal/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
extern crate console_error_panic_hook;
use std::{fmt::Display, sync::Arc};

use bitwarden_core::{key_management::CryptoClient, Client, ClientSettings};
use bitwarden_core::{
key_management::{CryptoClient, KeyManagementApiClient},
Client, ClientSettings,
};
use bitwarden_error::bitwarden_error;
use bitwarden_exporters::ExporterClientExt;
use bitwarden_generators::GeneratorClientsExt;
Expand Down Expand Up @@ -73,6 +76,10 @@ impl BitwardenClient {
pub fn exporters(&self) -> bitwarden_exporters::ExporterClient {
self.0.exporters()
}

pub fn km_api(&self) -> KeyManagementApiClient {
self.0.km_api()
}
}

#[bitwarden_error(basic)]
Expand Down