-
Notifications
You must be signed in to change notification settings - Fork 13
Expose QR login related structs and methods #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
//! Types for QR code login | ||
|
||
use matrix_sdk_crypto::types::qr_login; | ||
use url::Url; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use crate::vodozemac::Curve25519PublicKey; | ||
|
||
/// The mode of the QR code login. | ||
/// | ||
/// The QR code login mechanism supports both, the new device, as well as the | ||
/// existing device to display the QR code. | ||
/// | ||
/// The different modes have an explicit one-byte identifier which gets added to | ||
/// the QR code data. | ||
#[wasm_bindgen] | ||
#[derive(Debug)] | ||
pub enum QrCodeMode { | ||
/// The new device is displaying the QR code. | ||
Login, | ||
/// The existing device is displaying the QR code. | ||
Reciprocate, | ||
} | ||
|
||
impl From<qr_login::QrCodeMode> for QrCodeMode { | ||
fn from(value: qr_login::QrCodeMode) -> Self { | ||
match value { | ||
qr_login::QrCodeMode::Login => Self::Login, | ||
qr_login::QrCodeMode::Reciprocate => Self::Reciprocate, | ||
} | ||
} | ||
} | ||
|
||
/// Data for the QR code login mechanism. | ||
/// | ||
/// The [`QrCodeData`] can be serialized and encoded as a QR code or it can be | ||
/// decoded from a QR code. | ||
#[wasm_bindgen] | ||
#[derive(Debug)] | ||
pub struct QrCodeData { | ||
inner: qr_login::QrCodeData, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl QrCodeData { | ||
/// Create new [`QrCodeData`] from a given public key, a rendezvous URL and, | ||
/// optionally, a homeserver URL. | ||
/// | ||
/// If a homeserver URL is given, then the [`QrCodeData`] mode will be | ||
/// [`QrCodeMode::Reciprocate`], i.e. the QR code will contain data for the | ||
/// existing device to display the QR code. | ||
/// | ||
/// If no homeserver is given, the [`QrCodeData`] mode will be | ||
/// [`QrCodeMode::Login`], i.e. the QR code will contain data for the | ||
/// new device to display the QR code. | ||
#[wasm_bindgen(constructor)] | ||
pub fn new( | ||
public_key: Curve25519PublicKey, | ||
rendezvous_url: &str, | ||
homeserver_url: Option<String>, | ||
) -> Result<QrCodeData, JsError> { | ||
let public_key = public_key.inner; | ||
let rendezvous_url = Url::parse(rendezvous_url)?; | ||
|
||
let mode_data = if let Some(homeserver_url) = homeserver_url { | ||
qr_login::QrCodeModeData::Reciprocate { homeserver_url: Url::parse(&homeserver_url)? } | ||
} else { | ||
qr_login::QrCodeModeData::Login | ||
}; | ||
|
||
let inner = qr_login::QrCodeData { public_key, rendezvous_url, mode_data }; | ||
|
||
Ok(QrCodeData { inner }) | ||
} | ||
|
||
/// Attempt to decode a slice of bytes into a [`QrCodeData`] object. | ||
/// | ||
/// The slice of bytes would generally be returned by a QR code decoder. | ||
pub fn from_bytes(bytes: &[u8]) -> Result<QrCodeData, JsError> { | ||
Ok(Self { inner: qr_login::QrCodeData::from_bytes(bytes)? }) | ||
} | ||
|
||
/// Encode the [`QrCodeData`] into a list of bytes. | ||
/// | ||
/// The list of bytes can be used by a QR code generator to create an image | ||
/// containing a QR code. | ||
pub fn to_bytes(&self) -> Vec<u8> { | ||
self.inner.to_bytes() | ||
} | ||
|
||
/// Attempt to decode a base64 encoded string into a [`QrCodeData`] object. | ||
pub fn from_base64(data: &str) -> Result<QrCodeData, JsError> { | ||
Ok(Self { inner: qr_login::QrCodeData::from_base64(data)? }) | ||
} | ||
|
||
/// Encode the [`QrCodeData`] into a string using base64. | ||
/// | ||
/// This format can be used for debugging purposes and the | ||
/// [`QrcodeData::from_base64()`] method can be used to parse the string | ||
/// again. | ||
pub fn to_base64(&self) -> String { | ||
self.inner.to_base64() | ||
} | ||
|
||
/// Get the Curve25519 public key embedded in the [`QrCodeData`]. | ||
/// | ||
/// This Curve25519 public key should be used to establish an | ||
/// [ECIES](https://en.wikipedia.org/wiki/Integrated_Encryption_Scheme) | ||
/// (Elliptic Curve Integrated Encryption Scheme) channel with the other | ||
/// device. | ||
#[wasm_bindgen(getter)] | ||
pub fn public_key(&self) -> Curve25519PublicKey { | ||
self.inner.public_key.into() | ||
} | ||
|
||
/// Get the URL of the rendezvous server which will be used to exchange | ||
/// messages between the two devices. | ||
#[wasm_bindgen(getter)] | ||
pub fn rendezvous_url(&self) -> String { | ||
self.inner.rendezvous_url.as_str().to_owned() | ||
} | ||
|
||
/// Get the homeserver URL which the new device will be logged in to. | ||
/// | ||
/// This will be only available if the existing device has generated the QR | ||
/// code and the new device is the one scanning the QR code. | ||
#[wasm_bindgen(getter)] | ||
pub fn homeserver_url(&self) -> Option<String> { | ||
if let qr_login::QrCodeModeData::Reciprocate { homeserver_url } = &self.inner.mode_data { | ||
Some(homeserver_url.as_str().to_owned()) | ||
} else { | ||
None | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we're adding new methods to the WASM bindings, please can we use |
||
|
||
/// Get the mode of this [`QrCodeData`] instance. | ||
#[wasm_bindgen(getter)] | ||
pub fn mode(&self) -> QrCodeMode { | ||
self.inner.mode().into() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please bear in mind that
[`QrCodeData`]
doesn't work in the documentation generated bywasm_bindgen
: see https://matrix-org.github.io/matrix-rust-sdk-crypto-wasm/classes/QrCodeData.html#constructor.You need to use tsdoc-style
{@link ... }
links.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the style in: 2c9bd6f.