Skip to content

Commit 1f7f082

Browse files
feat(binding): Expose remove_device_data
Closes #11892
1 parent 5abc59a commit 1f7f082

File tree

8 files changed

+197
-1
lines changed

8 files changed

+197
-1
lines changed

bindings/electron/src/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,6 +3202,15 @@ export type PkiGetAddrError =
32023202
| PkiGetAddrErrorInternal
32033203

32043204

3205+
// RemoveDeviceDataError
3206+
export interface RemoveDeviceDataErrorFailedToRemoveData {
3207+
tag: "RemoveDeviceDataErrorFailedToRemoveData"
3208+
error: string
3209+
}
3210+
export type RemoveDeviceDataError =
3211+
| RemoveDeviceDataErrorFailedToRemoveData
3212+
3213+
32053214
// RemoveDeviceError
32063215
export interface RemoveDeviceErrorInternal {
32073216
tag: "RemoveDeviceErrorInternal"
@@ -5186,6 +5195,10 @@ export function pkiRemoveLocalPending(
51865195
config: ClientConfig,
51875196
id: string
51885197
): Promise<Result<null, RemoveDeviceError>>
5198+
export function removeDeviceData(
5199+
config: ClientConfig,
5200+
device_id: string
5201+
): Promise<Result<null, RemoveDeviceDataError>>
51895202
export function showCertificateSelectionDialogWindowsOnly(
51905203
): Promise<Result<X509CertificateReference | null, ShowCertificateSelectionDialogError>>
51915204
export function testCheckMailbox(

bindings/electron/src/meths.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13879,6 +13879,26 @@ fn variant_pki_get_addr_error_rs_to_js<'a>(
1387913879
Ok(js_obj)
1388013880
}
1388113881

13882+
// RemoveDeviceDataError
13883+
13884+
#[allow(dead_code)]
13885+
fn variant_remove_device_data_error_rs_to_js<'a>(
13886+
cx: &mut impl Context<'a>,
13887+
rs_obj: libparsec::RemoveDeviceDataError,
13888+
) -> NeonResult<Handle<'a, JsObject>> {
13889+
let js_obj = cx.empty_object();
13890+
let js_display = JsString::try_new(cx, &rs_obj.to_string()).or_throw(cx)?;
13891+
js_obj.set(cx, "error", js_display)?;
13892+
match rs_obj {
13893+
libparsec::RemoveDeviceDataError::FailedToRemoveData { .. } => {
13894+
let js_tag =
13895+
JsString::try_new(cx, "RemoveDeviceDataErrorFailedToRemoveData").or_throw(cx)?;
13896+
js_obj.set(cx, "tag", js_tag)?;
13897+
}
13898+
}
13899+
Ok(js_obj)
13900+
}
13901+
1388213902
// RemoveDeviceError
1388313903

1388413904
#[allow(dead_code)]
@@ -25470,6 +25490,65 @@ fn pki_remove_local_pending(mut cx: FunctionContext) -> JsResult<JsPromise> {
2547025490
Ok(promise)
2547125491
}
2547225492

25493+
// remove_device_data
25494+
fn remove_device_data(mut cx: FunctionContext) -> JsResult<JsPromise> {
25495+
crate::init_sentry();
25496+
let config = {
25497+
let js_val = cx.argument::<JsObject>(0)?;
25498+
struct_client_config_js_to_rs(&mut cx, js_val)?
25499+
};
25500+
let device_id = {
25501+
let js_val = cx.argument::<JsString>(1)?;
25502+
{
25503+
let custom_from_rs_string = |s: String| -> Result<libparsec::DeviceID, _> {
25504+
libparsec::DeviceID::from_hex(s.as_str()).map_err(|e| e.to_string())
25505+
};
25506+
match custom_from_rs_string(js_val.value(&mut cx)) {
25507+
Ok(val) => val,
25508+
Err(err) => return cx.throw_type_error(err),
25509+
}
25510+
}
25511+
};
25512+
let channel = cx.channel();
25513+
let (deferred, promise) = cx.promise();
25514+
25515+
// TODO: Promises are not cancellable in Javascript by default, should we add a custom cancel method ?
25516+
let _handle = crate::TOKIO_RUNTIME
25517+
.lock()
25518+
.expect("Mutex is poisoned")
25519+
.spawn(async move {
25520+
let ret = libparsec::remove_device_data(config, device_id).await;
25521+
25522+
deferred.settle_with(&channel, move |mut cx| {
25523+
let js_ret = match ret {
25524+
Ok(ok) => {
25525+
let js_obj = JsObject::new(&mut cx);
25526+
let js_tag = JsBoolean::new(&mut cx, true);
25527+
js_obj.set(&mut cx, "ok", js_tag)?;
25528+
let js_value = {
25529+
#[allow(clippy::let_unit_value)]
25530+
let _ = ok;
25531+
JsNull::new(&mut cx)
25532+
};
25533+
js_obj.set(&mut cx, "value", js_value)?;
25534+
js_obj
25535+
}
25536+
Err(err) => {
25537+
let js_obj = cx.empty_object();
25538+
let js_tag = JsBoolean::new(&mut cx, false);
25539+
js_obj.set(&mut cx, "ok", js_tag)?;
25540+
let js_err = variant_remove_device_data_error_rs_to_js(&mut cx, err)?;
25541+
js_obj.set(&mut cx, "error", js_err)?;
25542+
js_obj
25543+
}
25544+
};
25545+
Ok(js_ret)
25546+
});
25547+
});
25548+
25549+
Ok(promise)
25550+
}
25551+
2547325552
// show_certificate_selection_dialog_windows_only
2547425553
fn show_certificate_selection_dialog_windows_only(mut cx: FunctionContext) -> JsResult<JsPromise> {
2547525554
crate::init_sentry();
@@ -29902,6 +29981,7 @@ pub fn register_meths(cx: &mut ModuleContext) -> NeonResult<()> {
2990229981
cx.export_function("pkiEnrollmentInfo", pki_enrollment_info)?;
2990329982
cx.export_function("pkiEnrollmentSubmit", pki_enrollment_submit)?;
2990429983
cx.export_function("pkiRemoveLocalPending", pki_remove_local_pending)?;
29984+
cx.export_function("removeDeviceData", remove_device_data)?;
2990529985
cx.export_function(
2990629986
"showCertificateSelectionDialogWindowsOnly",
2990729987
show_certificate_selection_dialog_windows_only,

bindings/generator/api/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ class Handle(U32BasedType):
176176
pass
177177

178178

179+
class AnyhowError(StrBasedType):
180+
custom_to_rs_string = DISPLAY_TO_STRING
181+
182+
179183
class ApiVersion(StrBasedType):
180184
custom_from_rs_string = "|s: String| -> Result<_, String> { libparsec::ApiVersion::try_from(s.as_str()).map_err(|e| e.to_string()) }"
181185
custom_to_rs_string = DISPLAY_TO_STRING

bindings/generator/api/device.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
VariantItemUnit,
2020
X509CertificateReference,
2121
)
22+
from .config import ClientConfig
2223

2324

2425
class AvailableDeviceType(Variant):
@@ -129,6 +130,16 @@ async def archive_device(
129130
raise NotImplementedError
130131

131132

133+
class RemoveDeviceDataError(ErrorVariant):
134+
class FailedToRemoveData: ...
135+
136+
137+
async def remove_device_data(
138+
config: ClientConfig, device_id: DeviceID
139+
) -> Result[None, RemoveDeviceDataError]:
140+
raise NotImplementedError
141+
142+
132143
class UpdateDeviceError(ErrorVariant):
133144
class StorageNotAvailable:
134145
pass

bindings/web/src/meths.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15198,6 +15198,27 @@ fn variant_pki_get_addr_error_rs_to_js(
1519815198
Ok(js_obj)
1519915199
}
1520015200

15201+
// RemoveDeviceDataError
15202+
15203+
#[allow(dead_code)]
15204+
fn variant_remove_device_data_error_rs_to_js(
15205+
rs_obj: libparsec::RemoveDeviceDataError,
15206+
) -> Result<JsValue, JsValue> {
15207+
let js_obj = Object::new().into();
15208+
let js_display = &rs_obj.to_string();
15209+
Reflect::set(&js_obj, &"error".into(), &js_display.into())?;
15210+
match rs_obj {
15211+
libparsec::RemoveDeviceDataError::FailedToRemoveData { .. } => {
15212+
Reflect::set(
15213+
&js_obj,
15214+
&"tag".into(),
15215+
&"RemoveDeviceDataErrorFailedToRemoveData".into(),
15216+
)?;
15217+
}
15218+
}
15219+
Ok(js_obj)
15220+
}
15221+
1520115222
// RemoveDeviceError
1520215223

1520315224
#[allow(dead_code)]
@@ -23733,6 +23754,43 @@ pub fn pkiRemoveLocalPending(config: Object, id: String) -> Promise {
2373323754
}))
2373423755
}
2373523756

23757+
// remove_device_data
23758+
#[allow(non_snake_case)]
23759+
#[wasm_bindgen]
23760+
pub fn removeDeviceData(config: Object, device_id: String) -> Promise {
23761+
future_to_promise(libparsec::WithTaskIDFuture::from(async move {
23762+
let config = config.into();
23763+
let config = struct_client_config_js_to_rs(config)?;
23764+
23765+
let device_id = {
23766+
let custom_from_rs_string = |s: String| -> Result<libparsec::DeviceID, _> {
23767+
libparsec::DeviceID::from_hex(s.as_str()).map_err(|e| e.to_string())
23768+
};
23769+
custom_from_rs_string(device_id).map_err(|e| TypeError::new(e.as_ref()))
23770+
}?;
23771+
let ret = libparsec::remove_device_data(config, device_id).await;
23772+
Ok(match ret {
23773+
Ok(value) => {
23774+
let js_obj = Object::new().into();
23775+
Reflect::set(&js_obj, &"ok".into(), &true.into())?;
23776+
let js_value = {
23777+
let _ = value;
23778+
JsValue::null()
23779+
};
23780+
Reflect::set(&js_obj, &"value".into(), &js_value)?;
23781+
js_obj
23782+
}
23783+
Err(err) => {
23784+
let js_obj = Object::new().into();
23785+
Reflect::set(&js_obj, &"ok".into(), &false.into())?;
23786+
let js_err = variant_remove_device_data_error_rs_to_js(err)?;
23787+
Reflect::set(&js_obj, &"error".into(), &js_err)?;
23788+
js_obj
23789+
}
23790+
})
23791+
}))
23792+
}
23793+
2373623794
// show_certificate_selection_dialog_windows_only
2373723795
#[allow(non_snake_case)]
2373823796
#[wasm_bindgen]

client/src/plugins/libparsec/definitions.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export enum UserProfile {
8484
}
8585
export type AccountAuthMethodID = string
8686
export type AccountVaultItemOpaqueKeyID = string
87+
export type AnyhowError = string
8788
export type ApiVersion = string
8889
export type DeviceID = string
8990
export type DeviceLabel = string
@@ -3807,6 +3808,18 @@ export interface PkiGetAddrErrorInternal {
38073808
export type PkiGetAddrError =
38083809
| PkiGetAddrErrorInternal
38093810

3811+
// RemoveDeviceDataError
3812+
export enum RemoveDeviceDataErrorTag {
3813+
FailedToRemoveData = 'RemoveDeviceDataErrorFailedToRemoveData',
3814+
}
3815+
3816+
export interface RemoveDeviceDataErrorFailedToRemoveData {
3817+
tag: RemoveDeviceDataErrorTag.FailedToRemoveData
3818+
error: string
3819+
}
3820+
export type RemoveDeviceDataError =
3821+
| RemoveDeviceDataErrorFailedToRemoveData
3822+
38103823
// RemoveDeviceError
38113824
export enum RemoveDeviceErrorTag {
38123825
Internal = 'RemoveDeviceErrorInternal',
@@ -6131,6 +6144,10 @@ export interface LibParsecPlugin {
61316144
config: ClientConfig,
61326145
id: PKIEnrollmentID
61336146
): Promise<Result<null, RemoveDeviceError>>
6147+
removeDeviceData(
6148+
config: ClientConfig,
6149+
device_id: DeviceID
6150+
): Promise<Result<null, RemoveDeviceDataError>>
61346151
showCertificateSelectionDialogWindowsOnly(
61356152
): Promise<Result<X509CertificateReference | null, ShowCertificateSelectionDialogError>>
61366153
testCheckMailbox(

libparsec/src/device.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS
22

3-
use std::path::Path;
3+
use std::{path::Path, sync::Arc};
44

5+
use crate::config::ClientConfig;
56
pub use libparsec_platform_device_loader::{
67
ArchiveDeviceError, AvailableDevice, AvailableDeviceType, ListAvailableDeviceError,
78
ListPkiLocalPendingError, UpdateDeviceError,
89
};
10+
pub use libparsec_platform_storage::RemoveDeviceDataError;
911
use libparsec_types::prelude::*;
1012

1113
mod strategy {
@@ -393,6 +395,15 @@ pub async fn archive_device(
393395
libparsec_platform_device_loader::archive_device(config_dir, device_path).await
394396
}
395397

398+
pub async fn remove_device_data(
399+
config: ClientConfig,
400+
device_id: DeviceID,
401+
) -> Result<(), RemoveDeviceDataError> {
402+
let config: Arc<libparsec_client::ClientConfig> = config.into();
403+
404+
libparsec_platform_storage::remove_device_data(&config.data_base_dir, device_id).await
405+
}
406+
396407
pub async fn update_device_change_authentication(
397408
config_dir: &Path,
398409
current_auth: DeviceAccessStrategy,

libparsec/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub use validation::*;
4646
pub use workspace::*;
4747
pub use workspace_history::*;
4848

49+
pub use libparsec_types::anyhow::Error as AnyhowError;
50+
4951
pub mod internal {
5052
pub use libparsec_client::{
5153
claimer_retrieve_info, AnyClaimRetrievedInfoCtx, Client, ClientConfig,

0 commit comments

Comments
 (0)