refactor(crypto): Rename QrCodeMode into QRCodeIntent
This commit is contained in:
@@ -22,7 +22,7 @@ use thiserror::Error;
|
||||
|
||||
mod msc_4108;
|
||||
|
||||
pub use msc_4108::{QrCodeMode, QrCodeModeData};
|
||||
pub use msc_4108::{QrCodeIntent, QrCodeModeData};
|
||||
use url::Url;
|
||||
use vodozemac::{Curve25519PublicKey, base64_decode, base64_encode};
|
||||
|
||||
@@ -138,13 +138,13 @@ impl QrCodeData {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the [`QrCodeMode`] of this [`QrCodeData`] object.
|
||||
/// Get the [`QrCodeIntent`] of this [`QrCodeData`] object.
|
||||
///
|
||||
/// This tells us if the creator of the QR code wants to log in or if they
|
||||
/// want to log another device in.
|
||||
pub fn mode(&self) -> QrCodeMode {
|
||||
pub fn intent(&self) -> QrCodeIntent {
|
||||
match &self.0 {
|
||||
QrCodeDataInner::Msc4108(qr_code_data) => qr_code_data.mode(),
|
||||
QrCodeDataInner::Msc4108(qr_code_data) => qr_code_data.intent(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,23 +49,15 @@ pub enum QrCodeModeData {
|
||||
},
|
||||
}
|
||||
|
||||
impl QrCodeModeData {
|
||||
/// Get the [`QrCodeMode`] which is associated to this [`QrCodeModeData`]
|
||||
/// instance.
|
||||
pub fn mode(&self) -> QrCodeMode {
|
||||
self.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// The mode of the QR code login.
|
||||
/// The intent of the device that generated/displayed the QR code.
|
||||
///
|
||||
/// 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.
|
||||
/// The different intents have an explicit one-byte identifier which gets added
|
||||
/// to the QR code data.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum QrCodeMode {
|
||||
pub enum QrCodeIntent {
|
||||
/// Enum variant for the case where the new device is displaying the QR
|
||||
/// code.
|
||||
Login = 0x03,
|
||||
@@ -74,7 +66,7 @@ pub enum QrCodeMode {
|
||||
Reciprocate = 0x04,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for QrCodeMode {
|
||||
impl TryFrom<u8> for QrCodeIntent {
|
||||
type Error = LoginQrCodeDecodeError;
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
@@ -86,7 +78,7 @@ impl TryFrom<u8> for QrCodeMode {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&QrCodeModeData> for QrCodeMode {
|
||||
impl From<&QrCodeModeData> for QrCodeIntent {
|
||||
fn from(value: &QrCodeModeData) -> Self {
|
||||
match value {
|
||||
QrCodeModeData::Login => Self::Login,
|
||||
@@ -144,7 +136,7 @@ impl QrCodeData {
|
||||
if version == VERSION {
|
||||
// 3. The intent/mode is the next one to parse, we return an error imediatelly
|
||||
// the intent isn't 0x03 or 0x04.
|
||||
let mode = QrCodeMode::try_from(reader.read_u8()?)?;
|
||||
let mode = QrCodeIntent::try_from(reader.read_u8()?)?;
|
||||
|
||||
// 4. Let's get the public key and convert it to our strongly typed
|
||||
// Curve25519PublicKey type.
|
||||
@@ -160,8 +152,8 @@ impl QrCodeData {
|
||||
let rendezvous_url = Url::parse(str::from_utf8(&rendezvous_url)?)?;
|
||||
|
||||
let mode_data = match mode {
|
||||
QrCodeMode::Login => QrCodeModeData::Login,
|
||||
QrCodeMode::Reciprocate => {
|
||||
QrCodeIntent::Login => QrCodeModeData::Login,
|
||||
QrCodeIntent::Reciprocate => {
|
||||
// 7. If the mode is 0x04, we attempt to read the two bytes for the length of
|
||||
// the homeserver URL.
|
||||
let server_name_len = reader.read_u16::<BigEndian>()?;
|
||||
@@ -191,7 +183,7 @@ impl QrCodeData {
|
||||
let encoded = [
|
||||
PREFIX,
|
||||
&[VERSION],
|
||||
&[self.mode_data.mode() as u8],
|
||||
&[self.intent() as u8],
|
||||
self.public_key.as_bytes().as_slice(),
|
||||
&rendezvous_url_len,
|
||||
self.rendezvous_url.as_str().as_bytes(),
|
||||
@@ -207,9 +199,9 @@ impl QrCodeData {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the mode of this [`QrCodeData`] instance.
|
||||
pub fn mode(&self) -> QrCodeMode {
|
||||
self.mode_data.mode()
|
||||
/// Get the intent of this [`QrCodeData`] instance.
|
||||
pub fn intent(&self) -> QrCodeIntent {
|
||||
(&self.mode_data).into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,8 +269,8 @@ pub(super) mod test {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
data.mode(),
|
||||
QrCodeMode::Login,
|
||||
data.intent(),
|
||||
QrCodeIntent::Login,
|
||||
"The mode in the test bytes vector should be Login"
|
||||
);
|
||||
|
||||
@@ -313,8 +305,8 @@ pub(super) mod test {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
data.mode(),
|
||||
QrCodeMode::Reciprocate,
|
||||
data.intent(),
|
||||
QrCodeIntent::Reciprocate,
|
||||
"The mode in the test bytes vector should be Reciprocate"
|
||||
);
|
||||
|
||||
@@ -351,8 +343,8 @@ pub(super) mod test {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
data.mode(),
|
||||
QrCodeMode::Reciprocate,
|
||||
data.intent(),
|
||||
QrCodeIntent::Reciprocate,
|
||||
"The mode in the test bytes vector should be Reciprocate"
|
||||
);
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ use matrix_sdk_base::{
|
||||
boxed_into_future,
|
||||
crypto::types::{
|
||||
SecretsBundle,
|
||||
qr_login::{QrCodeData, QrCodeMode},
|
||||
qr_login::{QrCodeData, QrCodeIntent},
|
||||
},
|
||||
};
|
||||
use oauth2::VerificationUriComplete;
|
||||
@@ -248,7 +248,7 @@ impl<'a> IntoFuture for GrantLoginWithScannedQrCode<'a> {
|
||||
let mut channel = EstablishedSecureChannel::from_qr_code(
|
||||
self.client.inner.http_client.inner.clone(),
|
||||
self.qr_code_data,
|
||||
QrCodeMode::Reciprocate,
|
||||
QrCodeIntent::Reciprocate,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -427,7 +427,7 @@ mod test {
|
||||
let mut bob = EstablishedSecureChannel::from_qr_code(
|
||||
reqwest::Client::new(),
|
||||
&qr_code_data,
|
||||
QrCodeMode::Login,
|
||||
QrCodeIntent::Login,
|
||||
)
|
||||
.await
|
||||
.expect("Bob should be able to connect the secure channel");
|
||||
|
||||
@@ -18,7 +18,7 @@ use eyeball::SharedObservable;
|
||||
use futures_core::Stream;
|
||||
use matrix_sdk_base::{
|
||||
SessionMeta, boxed_into_future,
|
||||
crypto::types::qr_login::{QrCodeData, QrCodeMode},
|
||||
crypto::types::qr_login::{QrCodeData, QrCodeIntent},
|
||||
store::RoomLoadSettings,
|
||||
};
|
||||
use oauth2::{DeviceCodeErrorResponseType, StandardDeviceAuthorizationResponse};
|
||||
@@ -339,7 +339,7 @@ impl<'a> LoginWithQrCode<'a> {
|
||||
let channel = EstablishedSecureChannel::from_qr_code(
|
||||
http_client,
|
||||
self.qr_code_data,
|
||||
QrCodeMode::Login,
|
||||
QrCodeIntent::Login,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -673,7 +673,7 @@ mod test {
|
||||
let mut channel = EstablishedSecureChannel::from_qr_code(
|
||||
alice.inner.http_client.inner.clone(),
|
||||
&qr_code_data,
|
||||
QrCodeMode::Reciprocate,
|
||||
QrCodeIntent::Reciprocate,
|
||||
)
|
||||
.await
|
||||
.expect("Alice should be able to establish the secure channel");
|
||||
|
||||
@@ -25,7 +25,7 @@ use std::sync::Arc;
|
||||
|
||||
use as_variant::as_variant;
|
||||
pub use matrix_sdk_base::crypto::types::qr_login::{
|
||||
LoginQrCodeDecodeError, QrCodeData, QrCodeMode, QrCodeModeData,
|
||||
LoginQrCodeDecodeError, QrCodeData, QrCodeIntent, QrCodeModeData,
|
||||
};
|
||||
use matrix_sdk_base::crypto::{SecretImportError, store::SecretsBundleExportError};
|
||||
pub use oauth2::{
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
use crypto_channel::*;
|
||||
use matrix_sdk_base::crypto::types::qr_login::{QrCodeData, QrCodeMode, QrCodeModeData};
|
||||
use matrix_sdk_base::crypto::types::qr_login::{QrCodeData, QrCodeIntent, QrCodeModeData};
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use tracing::{instrument, trace};
|
||||
use url::Url;
|
||||
@@ -143,13 +143,13 @@ impl EstablishedSecureChannel {
|
||||
pub(super) async fn from_qr_code(
|
||||
client: reqwest::Client,
|
||||
qr_code_data: &QrCodeData,
|
||||
expected_mode: QrCodeMode,
|
||||
expected_mode: QrCodeIntent,
|
||||
) -> Result<Self, Error> {
|
||||
enum ChannelType {
|
||||
Ecies(EstablishedEcies),
|
||||
}
|
||||
|
||||
if qr_code_data.mode() == expected_mode {
|
||||
if qr_code_data.intent() == expected_mode {
|
||||
Err(Error::InvalidIntent)
|
||||
} else {
|
||||
trace!("Attempting to create a new inbound secure channel from a QR code.");
|
||||
@@ -259,7 +259,7 @@ pub(super) mod test {
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use matrix_sdk_base::crypto::types::qr_login::QrCodeMode;
|
||||
use matrix_sdk_base::crypto::types::qr_login::QrCodeIntent;
|
||||
use matrix_sdk_common::executor::spawn;
|
||||
use matrix_sdk_test::async_test;
|
||||
use ruma::time::Instant;
|
||||
@@ -424,7 +424,7 @@ pub(super) mod test {
|
||||
EstablishedSecureChannel::from_qr_code(
|
||||
reqwest::Client::new(),
|
||||
&qr_code_data,
|
||||
QrCodeMode::Login,
|
||||
QrCodeIntent::Login,
|
||||
)
|
||||
.await
|
||||
.expect("Bob should be able to fully establish the secure channel.")
|
||||
|
||||
Reference in New Issue
Block a user