Skip to main content

coven/
handle_pairing.rs

1//! Adding a device of the person's own by pairing, on the device that shows
2//! the code (ยง13.2, E10).
3
4use super::*;
5use coven_storage::{ConnectionCredentials, StorageCredentials};
6
7impl CovenHandle {
8    /// Starts a pairing to add another of this member's devices, in a write
9    /// of its own, with or without storage, and returns its id and codes at
10    /// once (`CovenWrite::add_device`). Any registered device of an active
11    /// member may; one that isn't fails with `Rejected(NotAllowed)`. On S3
12    /// the QR code carries the key this device connects with, read from its
13    /// custody now; one holding no S3 key fails with `InvalidConfiguration`,
14    /// and a locked keychain with `KeychainTemporarilyUnavailable`.
15    pub async fn add_device(&self) -> Result<AddDevice, SyncError> {
16        let key = match self.codes.connection().await? {
17            Some(ConnectionCredentials { credentials: StorageCredentials::S3(key), .. }) => {
18                Some(key)
19            }
20            Some(_) | None => None,
21        };
22        self.write(move |write| write.add_device(key)).await
23    }
24
25    /// The pairing's status, from its start until `end_add_device` forgets it;
26    /// `None` for a pairing this device never made or has forgotten.
27    pub async fn add_device_status(
28        &self,
29        pairing: AddDeviceId,
30    ) -> CovenResult<Option<AddDeviceStatus>> {
31        let records =
32            self.database.operations_of_kind(coven_database::operation_kind::PAIRING).await?;
33        Ok(coven_sync::pairing_status(&records, pairing)?)
34    }
35
36    /// The same internal query, live (E4), for the screen that shows the code.
37    pub fn follow_add_device(
38        &self,
39        pairing: AddDeviceId,
40    ) -> LiveQuery<Option<AddDeviceStatus>, CovenError> {
41        self.database
42            .subscribe_operations_of_kind(coven_database::operation_kind::PAIRING)
43            .process(move |records| Ok(coven_sync::pairing_status(&records, pairing)?))
44    }
45
46    /// Approves exactly this checked request, in a write of its own
47    /// (`CovenWrite::approve_add_device`).
48    pub async fn approve_add_device(&self, request: &AddDeviceRequest) -> Result<(), SyncError> {
49        let request = request.clone();
50        self.write(move |write| write.approve_add_device(&request)).await
51    }
52
53    /// Declines exactly this checked request, in a write of its own
54    /// (`CovenWrite::decline_add_device`).
55    pub async fn decline_add_device(&self, request: &AddDeviceRequest) -> Result<(), SyncError> {
56        let request = request.clone();
57        self.write(move |write| write.decline_add_device(&request)).await
58    }
59
60    /// The screen showing the pairing's code closed: ends the pairing and
61    /// forgets it, in a write of its own (`CovenWrite::end_add_device`).
62    pub async fn end_add_device(&self, pairing: AddDeviceId) -> Result<(), SyncError> {
63        self.write(move |write| write.end_add_device(pairing)).await
64    }
65}