Skip to content

Commit

Permalink
Implement ShamirRecoveryClaimRecoverDeviceCtx::recover_device
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Nov 28, 2024
1 parent 1a1118b commit 08dde62
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
3 changes: 2 additions & 1 deletion libparsec/crates/client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ use libparsec_platform_async::lock::Mutex as AsyncMutex;

use libparsec_types::prelude::*;
pub use recovery_device::{
import_recovery_device, ClientExportRecoveryDeviceError, ImportRecoveryDeviceError,
import_recovery_device, register_new_device, ClientExportRecoveryDeviceError,
ImportRecoveryDeviceError,
};

// Re-exposed for public API
Expand Down
4 changes: 2 additions & 2 deletions libparsec/crates/client/src/client/recovery_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl From<SaveDeviceError> for ImportRecoveryDeviceError {
}
}

async fn register_new_device(
pub async fn register_new_device(
cmds: &AuthenticatedCmds,
new_device: &LocalDevice,
new_device_purpose: DevicePurpose,
Expand Down Expand Up @@ -242,7 +242,7 @@ pub(crate) fn generate_new_device_certificates(
}

#[derive(Debug, thiserror::Error)]
enum RegisterNewDeviceError {
pub enum RegisterNewDeviceError {
#[error("Component has stopped")]
Stopped,
#[error("Cannot reach the server")]
Expand Down
51 changes: 50 additions & 1 deletion libparsec/crates/client/src/invite/claimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use std::num::NonZeroU8;
use std::{path::PathBuf, sync::Arc};

use invited_cmds::latest::invite_claimer_step;
use libparsec_client_connection::AuthenticatedCmds;
use libparsec_client_connection::{protocol::invited_cmds, ConnectionError, InvitedCmds};
use libparsec_protocol::invited_cmds::v4::invite_info::ShamirRecoveryRecipient;
use libparsec_types::prelude::*;

use crate::client::register_new_device;
use crate::invite::common::{Throttle, WAIT_PEER_MAX_ATTEMPTS};
use crate::ClientConfig;

Expand Down Expand Up @@ -413,7 +415,54 @@ impl ShamirRecoveryClaimRecoverDeviceCtx {
self,
requested_device_label: DeviceLabel,
) -> Result<ShamirRecoveryClaimFinalizeCtx, ClaimInProgressError> {
panic!("{requested_device_label}")
let ciphered_data = {
use invited_cmds::latest::invite_shamir_recovery_reveal::{Rep, Req};

let rep = self
.cmds
.send(Req {
reveal_token: self.secret.reveal_token,
})
.await?;

match rep {
Rep::Ok { ciphered_data } => Ok(ciphered_data),
// TODO: specialize error
Rep::NotFound => Err(ClaimInProgressError::NotFound),
Rep::UnknownStatus { .. } => {
Err(anyhow::anyhow!("Unexpected server response: {:?}", rep).into())
}
}?
};

let recovery_device = Arc::new(
LocalDevice::decrypt_and_load(&ciphered_data, &self.secret.data_key)
.map_err(|e| ClaimInProgressError::Internal(anyhow::Error::msg(e)))?,
);

let new_local_device =
LocalDevice::from_existing_device_for_user(&recovery_device, requested_device_label);

let recovery_cmds = AuthenticatedCmds::new(
&self.config.config_dir,
recovery_device.clone(),
self.config.proxy.clone(),
)?;

register_new_device(
&recovery_cmds,
&new_local_device,
DevicePurpose::Standard,
&recovery_device,
)
.await
// TODO: specialize error
.map_err(|e| ClaimInProgressError::Internal(e.into()))?;

Ok(ShamirRecoveryClaimFinalizeCtx {
config: self.config,
new_local_device: Arc::new(new_local_device),
})
}
}

Expand Down

0 comments on commit 08dde62

Please sign in to comment.