Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bindings/matrix-sdk-ffi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.

### Features

- Add `CheckCodeSender::validate()` to allow `CheckCode` to be validated without sending.
([#5957](https://github.com/matrix-org/matrix-rust-sdk/pull/5957))
- Add `SpaceService::get_space_room` to get a space given its id from the space graph if available.
[#5944](https://github.com/matrix-org/matrix-rust-sdk/pull/5944)
- Add `QrCodeData::to_bytes()` to allow generation of a QR code.
Expand Down
12 changes: 12 additions & 0 deletions bindings/matrix-sdk-ffi/src/qr_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,16 @@ impl CheckCodeSender {
pub async fn send(&self, code: u8) -> Result<(), HumanQrLoginError> {
self.inner.send(code).await.map_err(HumanQrLoginError::from)
}

/// Validate the [`CheckCode`] without sending it.
///
/// This can be used to provide immediate feedback to the user.
///
/// # Arguments
/// * `check_code` - The check code in digits representation.
///
/// Returns `true` if the code is valid, `false` otherwise.
pub fn validate(&self, check_code: u8) -> bool {
self.inner.validate(check_code)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why introducing a new method? The existing send could fail fast if the code is not correct after a local check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it's probably not a good idea to never send the code. So maybe add a parameter to the send method like validate: Boolean?
This is quite equivalent, so feel free to ignore my comments ;)

}
2 changes: 2 additions & 0 deletions crates/matrix-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.

### Features

- Add `CheckCodeSender::validate()` to allow `CheckCode` to be validated without sending.
([#5957](https://github.com/matrix-org/matrix-rust-sdk/pull/5957))
- Sending `MessageLike` and `RawMessageLike` events through a `Room` now returns
the used `EncryptionInfo`, if any.
([#5936](https://github.com/matrix-org/matrix-rust-sdk/pull/5936))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl<'a> IntoFuture for GrantLoginWithGeneratedQrCode<'a> {
// -- MSC4108 Secure channel setup step 6
let (tx, rx) = tokio::sync::oneshot::channel();
self.state.set(GrantLoginProgress::EstablishingSecureChannel(
GeneratedQrProgress::QrScanned(CheckCodeSender::new(tx)),
GeneratedQrProgress::QrScanned(CheckCodeSender::new(tx, channel.check_code())),
));
let check_code = rx.await.map_err(|_| SecureChannelError::CannotReceiveCheckCode)?;

Expand Down Expand Up @@ -756,6 +756,8 @@ mod test {
.expect("The checkcode should only be forwarded once")
.await
.expect("Alice should receive the checkcode");
assert!(checkcode_sender.validate(checkcode));
assert!(!checkcode_sender.validate((checkcode + 1) % 100));
checkcode_sender
.send(checkcode)
.await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ impl<'a> LoginWithGeneratedQrCode<'a> {
trace!("Waiting for checkcode.");
let (tx, rx) = tokio::sync::oneshot::channel();
self.state.set(LoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrScanned(
CheckCodeSender::new(tx),
CheckCodeSender::new(tx, channel.check_code()),
)));

// Retrieve the entered checkcode and verify it to confirm that the channel is
Expand Down
20 changes: 18 additions & 2 deletions crates/matrix-sdk/src/authentication/oauth/qrcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,15 @@ pub enum GeneratedQrProgress {
#[derive(Clone, Debug)]
pub struct CheckCodeSender {
inner: Arc<Mutex<Option<tokio::sync::oneshot::Sender<u8>>>>,
expected_check_code: CheckCode,
}

impl CheckCodeSender {
pub(crate) fn new(tx: tokio::sync::oneshot::Sender<u8>) -> Self {
Self { inner: Arc::new(Mutex::new(Some(tx))) }
pub(crate) fn new(
tx: tokio::sync::oneshot::Sender<u8>,
expected_check_code: CheckCode,
) -> Self {
Self { inner: Arc::new(Mutex::new(Some(tx))), expected_check_code }
}

/// Send the checkcode.
Expand All @@ -349,6 +353,18 @@ impl CheckCodeSender {
None => Err(CheckCodeSenderError::AlreadySent),
}
}

/// Validate the [`CheckCode`] without sending it.
///
/// This can be used to provide immediate feedback to the user.
///
/// # Arguments
/// * `check_code` - The check code in digits representation.
///
/// Returns `true` if the code is valid, `false` otherwise.
pub fn validate(&self, check_code: u8) -> bool {
self.expected_check_code.to_digit() == check_code
}
}

/// Possible errors when calling [`CheckCodeSender::send`].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ impl AlmostEstablishedSecureChannel {
Err(Error::InvalidCheckCode)
}
}

pub fn check_code(&self) -> CheckCode {
self.secure_channel.check_code().clone()
}
}

pub(super) struct EstablishedSecureChannel {
Expand Down
Loading