|
| 1 | +//! Types for QR code login |
| 2 | +
|
| 3 | +use matrix_sdk_crypto::types::qr_login; |
| 4 | +use url::Url; |
| 5 | +use wasm_bindgen::prelude::*; |
| 6 | + |
| 7 | +use crate::vodozemac::Curve25519PublicKey; |
| 8 | + |
| 9 | +/// The mode of the QR code login. |
| 10 | +/// |
| 11 | +/// The QR code login mechanism supports both, the new device, as well as the |
| 12 | +/// existing device to display the QR code. |
| 13 | +/// |
| 14 | +/// The different modes have an explicit one-byte identifier which gets added to |
| 15 | +/// the QR code data. |
| 16 | +#[wasm_bindgen] |
| 17 | +#[derive(Debug)] |
| 18 | +pub enum QrCodeMode { |
| 19 | + /// Enum variant for the case where the new device is displaying the QR |
| 20 | + /// code. |
| 21 | + Login, |
| 22 | + /// Enum variant for the case where the existing device is displaying the QR |
| 23 | + /// code. |
| 24 | + Reciprocate, |
| 25 | +} |
| 26 | + |
| 27 | +impl From<qr_login::QrCodeMode> for QrCodeMode { |
| 28 | + fn from(value: qr_login::QrCodeMode) -> Self { |
| 29 | + match value { |
| 30 | + qr_login::QrCodeMode::Login => Self::Login, |
| 31 | + qr_login::QrCodeMode::Reciprocate => Self::Reciprocate, |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// Data for the QR code login mechanism. |
| 37 | +/// |
| 38 | +/// The [`QrCodeData`] can be serialized and encoded as a QR code or it can be |
| 39 | +/// decoded from a QR code. |
| 40 | +#[wasm_bindgen] |
| 41 | +#[derive(Debug)] |
| 42 | +pub struct QrCodeData { |
| 43 | + inner: qr_login::QrCodeData, |
| 44 | +} |
| 45 | + |
| 46 | +#[wasm_bindgen] |
| 47 | +impl QrCodeData { |
| 48 | + /// Create new [`QrCodeData`] from a given public key, a rendezvous URL and, |
| 49 | + /// optionally, a homeserver ULR. |
| 50 | + /// |
| 51 | + /// If a homeserver URL is given, then the [`QrCodeData`] mode will be |
| 52 | + /// [`QrCodeMode::Reciprocate`], i.e. the QR code will contain data for the |
| 53 | + /// existing device to display the QR code. |
| 54 | + /// |
| 55 | + /// If no homeserver is given, the [`QrCodeData`] mode will be |
| 56 | + /// [`QrCodeMode::Login`], i.e. the QR code will contain data for the |
| 57 | + /// new device to display the QR code. |
| 58 | + #[wasm_bindgen(constructor)] |
| 59 | + pub fn new( |
| 60 | + public_key: Curve25519PublicKey, |
| 61 | + rendezvous_url: &str, |
| 62 | + homeserver_url: Option<String>, |
| 63 | + ) -> Result<QrCodeData, JsError> { |
| 64 | + let public_key = public_key.inner; |
| 65 | + let rendezvous_url = Url::parse(rendezvous_url)?; |
| 66 | + |
| 67 | + let mode_data = if let Some(homeserver_url) = homeserver_url { |
| 68 | + qr_login::QrCodeModeData::Reciprocate { homeserver_url: Url::parse(&homeserver_url)? } |
| 69 | + } else { |
| 70 | + qr_login::QrCodeModeData::Login |
| 71 | + }; |
| 72 | + |
| 73 | + let inner = qr_login::QrCodeData { public_key, rendezvous_url, mode_data }; |
| 74 | + |
| 75 | + Ok(QrCodeData { inner }) |
| 76 | + } |
| 77 | + |
| 78 | + /// Attempt to decode a slice of bytes into a [`QrCodeData`] object. |
| 79 | + /// |
| 80 | + /// The slice of bytes would generally be returned by a QR code decoder. |
| 81 | + pub fn from_bytes(bytes: &[u8]) -> Result<QrCodeData, JsError> { |
| 82 | + Ok(Self { inner: qr_login::QrCodeData::from_bytes(bytes)? }) |
| 83 | + } |
| 84 | + |
| 85 | + /// Encode the [`QrCodeData`] into a list of bytes. |
| 86 | + /// |
| 87 | + /// The list of bytes can be used by a QR code generator to create an image |
| 88 | + /// containing a QR code. |
| 89 | + pub fn to_bytes(&self) -> Vec<u8> { |
| 90 | + self.inner.to_bytes() |
| 91 | + } |
| 92 | + |
| 93 | + /// Attempt to decode a base64 encoded string into a [`QrCodeData`] object. |
| 94 | + pub fn from_base64(data: &str) -> Result<QrCodeData, JsError> { |
| 95 | + Ok(Self { inner: qr_login::QrCodeData::from_base64(data)? }) |
| 96 | + } |
| 97 | + |
| 98 | + /// Encode the [`QrCodeData`] into a list of bytes. |
| 99 | + /// |
| 100 | + /// The list of bytes can be used by a QR code generator to create an image |
| 101 | + /// containing a QR code. |
| 102 | + pub fn to_base64(&self) -> String { |
| 103 | + self.inner.to_base64() |
| 104 | + } |
| 105 | + |
| 106 | + /// Get the Curve25519 public key embedded in the [`QrCodeData`]. |
| 107 | + /// |
| 108 | + /// This Curve25519 public key should be used to establish an ECIES channel |
| 109 | + /// with the other device. |
| 110 | + #[wasm_bindgen(getter)] |
| 111 | + pub fn public_key(&self) -> Curve25519PublicKey { |
| 112 | + self.inner.public_key.into() |
| 113 | + } |
| 114 | + |
| 115 | + /// Get the URL of the rendezvous server which will be used to exchange |
| 116 | + /// devices between the two devices. |
| 117 | + #[wasm_bindgen(getter)] |
| 118 | + pub fn rendezvous_url(&self) -> String { |
| 119 | + self.inner.rendezvous_url.as_str().to_owned() |
| 120 | + } |
| 121 | + |
| 122 | + /// Get the homeserver URL which the new device will be logged in to. |
| 123 | + /// |
| 124 | + /// This will be only available if the existing device has generated the QR |
| 125 | + /// code and the new device is the one scanning the QR code. |
| 126 | + #[wasm_bindgen(getter)] |
| 127 | + pub fn homeserver_url(&self) -> Option<String> { |
| 128 | + if let qr_login::QrCodeModeData::Reciprocate { homeserver_url } = &self.inner.mode_data { |
| 129 | + Some(homeserver_url.as_str().to_owned()) |
| 130 | + } else { |
| 131 | + None |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /// Get the mode of this [`QrCodeData`] instance. |
| 136 | + #[wasm_bindgen(getter)] |
| 137 | + pub fn mode(&self) -> QrCodeMode { |
| 138 | + self.inner.mode().into() |
| 139 | + } |
| 140 | +} |
0 commit comments