matrix_sdk/encryption/secret_storage/
futures.rs

1// Copyright 2023 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{future::IntoFuture, pin::Pin};
16
17use futures_core::Future;
18use matrix_sdk_base::crypto::secret_storage::SecretStorageKey;
19use ruma::events::secret_storage::default_key::SecretStorageDefaultKeyEventContent;
20
21use super::{Result, SecretStorage, SecretStore};
22
23/// Future returned by [`SecretStorage::create_secret_store()`].
24#[derive(Debug)]
25pub struct CreateStore<'a> {
26    pub(super) secret_storage: &'a SecretStorage,
27    pub(super) passphrase: Option<&'a str>,
28}
29
30impl<'a> CreateStore<'a> {
31    /// Set the passphrase for the new [`SecretStore`].
32    ///
33    /// See the documentation for the [`SecretStorage::create_secret_store()`]
34    /// method for more info.
35    pub fn with_passphrase(mut self, passphrase: &'a str) -> Self {
36        self.passphrase = Some(passphrase);
37
38        self
39    }
40}
41
42impl<'a> IntoFuture for CreateStore<'a> {
43    type Output = Result<SecretStore>;
44    #[cfg(target_arch = "wasm32")]
45    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>;
46    #[cfg(not(target_arch = "wasm32"))]
47    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
48
49    fn into_future(self) -> Self::IntoFuture {
50        let Self { secret_storage, passphrase } = self;
51
52        Box::pin(async move {
53            // Prevent multiple simultaneous calls to this method.
54            //
55            // See the documentation for the lock in the `store_secret` method for more
56            // info.
57            let client_copy = secret_storage.client.to_owned();
58            let _guard = client_copy.locks().open_secret_store_lock.lock().await;
59
60            let new_key = if let Some(passphrase) = passphrase {
61                SecretStorageKey::new_from_passphrase(passphrase)
62            } else {
63                SecretStorageKey::new()
64            };
65
66            let content = new_key.event_content().to_owned();
67
68            secret_storage.client.account().set_account_data(content).await?;
69
70            let store = SecretStore { client: secret_storage.client.to_owned(), key: new_key };
71            store.export_secrets().await?;
72
73            let default_key_content =
74                SecretStorageDefaultKeyEventContent::new(store.key.key_id().to_owned());
75
76            store.client.account().set_account_data(default_key_content).await?;
77
78            Ok(store)
79        })
80    }
81}