|
| 1 | +// Copyright 2025 The Matrix.org Foundation C.I.C. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::time::{Duration, Instant}; |
| 16 | + |
| 17 | +use eyeball::SharedObservable; |
| 18 | +use matrix_sdk_base::crypto::{store::SecretsBundleExportError, types::SecretsBundle}; |
| 19 | +use oauth2::VerificationUriComplete; |
| 20 | +use ruma::api::client::device; |
| 21 | +use thiserror::Error; |
| 22 | +use url::Url; |
| 23 | + |
| 24 | +use super::{ |
| 25 | + LoginProtocolType, QrAuthMessage, SecureChannelError, |
| 26 | + secure_channel::{AlmostEstablishedSecureChannel, EstablishedSecureChannel, SecureChannel}, |
| 27 | +}; |
| 28 | +use crate::{Client, authentication::oauth::qrcode::LoginFailureReason}; |
| 29 | + |
| 30 | +pub mod scanned; |
| 31 | + |
| 32 | +/// Error type for failures related to reciprocating a login. |
| 33 | +#[derive(Debug, Error)] |
| 34 | +pub enum Error { |
| 35 | + /// Secrets backup not set up. |
| 36 | + #[error("Secrets backup not set up")] |
| 37 | + MissingSecretsBackup(Option<SecretsBundleExportError>), |
| 38 | + |
| 39 | + /// The request took too long to complete. |
| 40 | + #[error("The request took too long to complete")] |
| 41 | + Timeout, |
| 42 | + |
| 43 | + /// The check code was incorrect. |
| 44 | + #[error("The check code was incorrect")] |
| 45 | + InvalidCheckCode, |
| 46 | + |
| 47 | + /// The device could not be created. |
| 48 | + #[error("The device could not be created")] |
| 49 | + UnableToCreateDevice, |
| 50 | + |
| 51 | + /// Auth handshake error. |
| 52 | + #[error("Auth handshake error: {0}")] |
| 53 | + Unknown(String), |
| 54 | + |
| 55 | + /// Unsupported protocol. |
| 56 | + #[error("Unsupported protocol: {0}")] |
| 57 | + UnsupportedProtocol(LoginProtocolType), |
| 58 | + |
| 59 | + /// The requested device ID is already in use. |
| 60 | + #[error("The requested device ID is already in use")] |
| 61 | + DeviceIDAlreadyInUse, |
| 62 | + |
| 63 | + /// Invalid state. |
| 64 | + #[error("Invalid state")] |
| 65 | + InvalidState, |
| 66 | +} |
| 67 | + |
| 68 | +impl From<SecureChannelError> for Error { |
| 69 | + fn from(e: SecureChannelError) -> Self { |
| 70 | + match e { |
| 71 | + SecureChannelError::InvalidCheckCode => Self::InvalidCheckCode, |
| 72 | + e => Self::Unknown(e.to_string()), |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl From<SecretsBundleExportError> for Error { |
| 78 | + fn from(e: SecretsBundleExportError) -> Self { |
| 79 | + Self::MissingSecretsBackup(Some(e)) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// The progress of the reciprocation process. |
| 84 | +enum ReciprocateProgress { |
| 85 | + /// The secure channel has been confirmed and this device is waiting for the |
| 86 | + /// authorization to complete. |
| 87 | + WaitingForAuth { |
| 88 | + /// A URI to open in a (secure) system browser to verify the new login. |
| 89 | + verification_uri: Url, |
| 90 | + }, |
| 91 | + /// The new device has been granted access and this device is sending the |
| 92 | + /// secrets to it. |
| 93 | + SyncingSecrets, |
| 94 | + /// The process is complete. |
| 95 | + Done, |
| 96 | +} |
| 97 | + |
| 98 | +async fn export_secrets_bundle(client: &Client) -> Result<SecretsBundle, Error> { |
| 99 | + let secrets_bundle = client |
| 100 | + .olm_machine() |
| 101 | + .await |
| 102 | + .as_ref() |
| 103 | + .ok_or_else(|| Error::MissingSecretsBackup(None))? |
| 104 | + .store() |
| 105 | + .export_secrets_bundle() |
| 106 | + .await?; |
| 107 | + Ok(secrets_bundle) |
| 108 | +} |
| 109 | + |
| 110 | +async fn finish_login_grant<P: From<ReciprocateProgress>>( |
| 111 | + client: &Client, |
| 112 | + channel: &mut EstablishedSecureChannel, |
| 113 | + secrets_bundle: &SecretsBundle, |
| 114 | + state: &SharedObservable<P>, |
| 115 | +) -> Result<(), Error> { |
| 116 | + // The new device registers with the authorization server and sends it a device |
| 117 | + // authorization authorization request. |
| 118 | + // -- MSC4108 OAuth 2.0 login step 2 |
| 119 | + |
| 120 | + // We wait for the new device to send us the m.login.protocol message with the |
| 121 | + // device authorization grant information. -- MSC4108 OAuth 2.0 login step 3 |
| 122 | + let message = channel.receive_json().await?; |
| 123 | + let QrAuthMessage::LoginProtocol { device_authorization_grant, protocol, device_id } = message |
| 124 | + else { |
| 125 | + return Err(Error::Unknown( |
| 126 | + "Receiving unexpected message when expecting LoginProtocol".to_owned(), |
| 127 | + )); |
| 128 | + }; |
| 129 | + |
| 130 | + // We verify the selected protocol. |
| 131 | + // -- MSC4108 OAuth 2.0 login step 4 |
| 132 | + if protocol != LoginProtocolType::DeviceAuthorizationGrant { |
| 133 | + channel |
| 134 | + .send_json(QrAuthMessage::LoginFailure { |
| 135 | + reason: LoginFailureReason::UnsupportedProtocol, |
| 136 | + homeserver: None, |
| 137 | + }) |
| 138 | + .await?; |
| 139 | + return Err(Error::UnsupportedProtocol(protocol)); |
| 140 | + } |
| 141 | + |
| 142 | + // We check that the device ID is still available. |
| 143 | + // -- MSC4108 OAuth 2.0 login step 4 continued |
| 144 | + let request = device::get_device::v3::Request::new(device_id.to_base64().into()); |
| 145 | + if client.send(request).await.is_ok() { |
| 146 | + channel |
| 147 | + .send_json(QrAuthMessage::LoginFailure { |
| 148 | + reason: LoginFailureReason::DeviceAlreadyExists, |
| 149 | + homeserver: None, |
| 150 | + }) |
| 151 | + .await?; |
| 152 | + return Err(Error::DeviceIDAlreadyInUse); |
| 153 | + } |
| 154 | + |
| 155 | + // We emit an update so that the caller can open the verification URI in a |
| 156 | + // system browser to consent to the login. |
| 157 | + // -- MSC4108 OAuth 2.0 login step 4 continued |
| 158 | + let verification_uri = Url::parse( |
| 159 | + device_authorization_grant |
| 160 | + .verification_uri_complete |
| 161 | + .map(VerificationUriComplete::into_secret) |
| 162 | + .unwrap_or(device_authorization_grant.verification_uri.to_string()) |
| 163 | + .as_str(), |
| 164 | + ) |
| 165 | + .map_err(|_| Error::UnableToCreateDevice)?; |
| 166 | + state.set(ReciprocateProgress::WaitingForAuth { verification_uri }.into()); |
| 167 | + |
| 168 | + // We send the new device the m.login.protocol_accepted message to let it know |
| 169 | + // that the consent process is in progress. |
| 170 | + // -- MSC4108 OAuth 2.0 login step 4 continued |
| 171 | + let message = QrAuthMessage::LoginProtocolAccepted; |
| 172 | + channel.send_json(&message).await?; |
| 173 | + |
| 174 | + // The new device displays the user code it received from the authorization |
| 175 | + // server and starts polling for an access token. In parallel, the user |
| 176 | + // consents to the new login in the browser on this device, while verifying |
| 177 | + // the user code displayed on the other device. -- MSC4108 OAuth 2.0 login |
| 178 | + // steps 5 & 6 |
| 179 | + |
| 180 | + // We wait for the new device to send us the m.login.success message |
| 181 | + let message: QrAuthMessage = channel.receive_json().await?; |
| 182 | + let QrAuthMessage::LoginSuccess = message else { |
| 183 | + return Err(Error::Unknown( |
| 184 | + "Receiving unexpected message when expecting LoginSuccess".to_owned(), |
| 185 | + )); |
| 186 | + }; |
| 187 | + |
| 188 | + // We check that the new device was created successfully allowing for up to 10 |
| 189 | + // seconds of delay. -- MSC4108 Secret sharing and device verification step |
| 190 | + // 1 |
| 191 | + let request = device::get_device::v3::Request::new(device_id.to_base64().into()); |
| 192 | + let deadline = Instant::now() + Duration::from_secs(10); |
| 193 | + loop { |
| 194 | + if client.send(request.clone()).await.is_ok() { |
| 195 | + break; |
| 196 | + } |
| 197 | + // If the deadline hasn't yet passed, give it some time and retry the request. |
| 198 | + if Instant::now() < deadline { |
| 199 | + tokio::time::sleep(Duration::from_secs(1)).await; |
| 200 | + continue; |
| 201 | + } |
| 202 | + // The deadline has passed. Let's fail the login process. |
| 203 | + channel |
| 204 | + .send_json(QrAuthMessage::LoginFailure { |
| 205 | + reason: LoginFailureReason::DeviceNotFound, |
| 206 | + homeserver: None, |
| 207 | + }) |
| 208 | + .await?; |
| 209 | + return Err(Error::DeviceIDAlreadyInUse); |
| 210 | + } |
| 211 | + |
| 212 | + // We send the new device the secrets bundle. |
| 213 | + // -- MSC4108 Secret sharing and device verification step 2 |
| 214 | + state.set(ReciprocateProgress::SyncingSecrets.into()); |
| 215 | + let message = QrAuthMessage::LoginSecrets(secrets_bundle.clone()); |
| 216 | + channel.send_json(&message).await?; |
| 217 | + |
| 218 | + // And we're done. |
| 219 | + state.set(ReciprocateProgress::Done.into()); |
| 220 | + |
| 221 | + Ok(()) |
| 222 | +} |
0 commit comments