1// Copyright 2024 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.
1415use matrix_sdk_base::crypto::types::SecretsBundle;
16use matrix_sdk_common::deserialized_responses::PrivOwnedStr;
17use oauth2::{
18 EndUserVerificationUrl, StandardDeviceAuthorizationResponse, VerificationUriComplete,
19};
20use ruma::serde::StringEnum;
21use serde::{Deserialize, Deserializer, Serialize, Serializer};
22use url::Url;
23use vodozemac::Curve25519PublicKey;
2425#[cfg(doc)]
26use super::QRCodeLoginError::SecureChannel;
2728/// Messages that will be exchanged over the [`SecureChannel`] to log in a new
29/// device using a QR code.
30#[derive(Debug, Serialize, Deserialize)]
31#[serde(tag = "type")]
32pub enum QrAuthMessage {
33/// Message declaring the available protocols for sign in. Sent by the
34 /// existing device.
35#[serde(rename = "m.login.protocols")]
36LoginProtocols {
37/// The login protocols the existing device supports.
38protocols: Vec<LoginProtocolType>,
39/// The homeserver we're going to log in to.
40homeserver: Url,
41 },
4243/// Message declaring which protocols from the previous `m.login.protocols`
44 /// message the new device has picked. Sent by the new device.
45#[serde(rename = "m.login.protocol")]
46LoginProtocol {
47/// The device authorization grant the OAuth 2.0 server has given to the
48 /// new device, contains the URL the existing device should use to
49 /// confirm the log in.
50device_authorization_grant: AuthorizationGrant,
51/// The protocol the new device has picked.
52protocol: LoginProtocolType,
53#[serde(
54 deserialize_with = "deserialize_curve_key",
55 serialize_with = "serialize_curve_key"
56)]
57/// The device ID the new device will be using.
58device_id: Curve25519PublicKey,
59 },
6061/// Message declaring that the protocol in the previous `m.login.protocol`
62 /// message was accepted. Sent by the existing device.
63#[serde(rename = "m.login.protocol_accepted")]
64LoginProtocolAccepted,
6566/// Message that informs the existing device that it successfully obtained
67 /// an access token from the OAuth 2.0 server. Sent by the new device.
68#[serde(rename = "m.login.success")]
69LoginSuccess,
7071/// Message that informs the existing device that the OAuth 2.0 server has
72 /// declined to give us an access token, i.e. because the user declined the
73 /// log in. Sent by the new device.
74#[serde(rename = "m.login.declined")]
75LoginDeclined,
7677/// Message signaling that a failure happened during the login. Can be sent
78 /// by either device.
79#[serde(rename = "m.login.failure")]
80LoginFailure {
81/// The claimed reason for the login failure.
82reason: LoginFailureReason,
83/// The homeserver that we attempted to log in to.
84homeserver: Option<Url>,
85 },
8687/// Message containing end-to-end encryption related secrets, the new device
88 /// can use these secrets to mark itself as verified, connect to a room
89 /// key backup, and login other devices via a QR login. Sent by the
90 /// existing device.
91#[serde(rename = "m.login.secrets")]
92LoginSecrets(SecretsBundle),
93}
9495impl QrAuthMessage {
96/// Create a new [`QrAuthMessage::LoginProtocol`] message with the
97 /// [`LoginProtocolType::DeviceAuthorizationGrant`] protocol type.
98pub fn authorization_grant_login_protocol(
99 device_authorization_grant: AuthorizationGrant,
100 device_id: Curve25519PublicKey,
101 ) -> QrAuthMessage {
102 QrAuthMessage::LoginProtocol {
103 device_id,
104 device_authorization_grant,
105 protocol: LoginProtocolType::DeviceAuthorizationGrant,
106 }
107 }
108}
109110impl From<&StandardDeviceAuthorizationResponse> for AuthorizationGrant {
111fn from(value: &StandardDeviceAuthorizationResponse) -> Self {
112Self {
113 verification_uri: value.verification_uri().clone(),
114 verification_uri_complete: value.verification_uri_complete().cloned(),
115 }
116 }
117}
118119/// Data for the device authorization grant login protocol.
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct AuthorizationGrant {
122/// The verification URL the user should open to log the new device in.
123pub verification_uri: EndUserVerificationUrl,
124125/// The verification URL, with the user code pre-filled, which the user
126 /// should open to log the new device in. If this URL is available, the
127 /// user should be presented with it instead of the one in the
128 /// [`AuthorizationGrant::verification_uri`] field.
129pub verification_uri_complete: Option<VerificationUriComplete>,
130}
131132/// Reasons why the login might have failed.
133#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, StringEnum)]
134#[ruma_enum(rename_all = "snake_case")]
135pub enum LoginFailureReason {
136/// The Device Authorization Grant expired.
137AuthorizationExpired,
138/// The device ID specified by the new device already exists in the
139 /// homeserver provided device list.
140DeviceAlreadyExists,
141/// The new device is not present in the device list as returned by the
142 /// homeserver.
143DeviceNotFound,
144/// Sent by either device to indicate that they received a message of a type
145 /// that they weren't expecting.
146UnexpectedMessageReceived,
147/// Sent by a device where no suitable protocol is available or the
148 /// requested protocol requested is not supported.
149UnsupportedProtocol,
150/// Sent by either new or existing device to indicate that the user has
151 /// cancelled the login.
152UserCancelled,
153#[doc(hidden)]
154_Custom(PrivOwnedStr),
155}
156157/// Enum containing known login protocol types.
158#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, StringEnum)]
159#[ruma_enum(rename_all = "snake_case")]
160pub enum LoginProtocolType {
161/// The `device_authorization_grant` login protocol type.
162DeviceAuthorizationGrant,
163#[doc(hidden)]
164_Custom(PrivOwnedStr),
165}
166167// Vodozemac serializes Curve25519 keys directly as a byteslice, while Matrix
168// likes to base64 encode all byte slices.
169//
170// This ensures that we serialize/deserialize in a Matrix-compatible way.
171pub(crate) fn deserialize_curve_key<'de, D>(de: D) -> Result<Curve25519PublicKey, D::Error>
172where
173D: Deserializer<'de>,
174{
175let key: String = Deserialize::deserialize(de)?;
176177 Curve25519PublicKey::from_base64(&key).map_err(serde::de::Error::custom)
178}
179180pub(crate) fn serialize_curve_key<S>(key: &Curve25519PublicKey, s: S) -> Result<S::Ok, S::Error>
181where
182S: Serializer,
183{
184 s.serialize_str(&key.to_base64())
185}
186187#[cfg(test)]
188mod test {
189use assert_matches2::assert_let;
190use matrix_sdk_base::crypto::types::BackupSecrets;
191use serde_json::json;
192use similar_asserts::assert_eq;
193194use super::*;
195196#[test]
197fn test_protocols_serialization() {
198let json = json!({
199"type": "m.login.protocols",
200"protocols": ["device_authorization_grant"],
201"homeserver": "https://matrix-client.matrix.org/"
202203});
204205let message: QrAuthMessage = serde_json::from_value(json.clone()).unwrap();
206assert_let!(QrAuthMessage::LoginProtocols { protocols, .. } = &message);
207assert!(protocols.contains(&LoginProtocolType::DeviceAuthorizationGrant));
208209let serialized = serde_json::to_value(&message).unwrap();
210assert_eq!(json, serialized);
211 }
212213#[test]
214fn test_protocol_serialization() {
215let json = json!({
216"type": "m.login.protocol",
217"protocol": "device_authorization_grant",
218"device_authorization_grant": {
219"verification_uri_complete": "https://id.matrix.org/device/abcde",
220"verification_uri": "https://id.matrix.org/device/abcde?code=ABCDE"
221},
222"device_id": "wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4"
223});
224let curve_key =
225 Curve25519PublicKey::from_base64("wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4")
226 .unwrap();
227228let message: QrAuthMessage = serde_json::from_value(json.clone()).unwrap();
229assert_let!(QrAuthMessage::LoginProtocol { protocol, device_id, .. } = &message);
230assert_eq!(protocol, &LoginProtocolType::DeviceAuthorizationGrant);
231assert_eq!(device_id, &curve_key);
232let serialized = serde_json::to_value(&message).unwrap();
233assert_eq!(json, serialized);
234 }
235236#[test]
237fn test_protocol_accepted_serialization() {
238let json = json!({
239"type": "m.login.protocol_accepted",
240 });
241242let message: QrAuthMessage = serde_json::from_value(json.clone()).unwrap();
243assert_let!(QrAuthMessage::LoginProtocolAccepted = &message);
244let serialized = serde_json::to_value(&message).unwrap();
245assert_eq!(json, serialized);
246 }
247248#[test]
249fn test_login_success() {
250let json = json!({
251"type": "m.login.success",
252 });
253254let message: QrAuthMessage = serde_json::from_value(json.clone()).unwrap();
255assert_let!(QrAuthMessage::LoginSuccess = &message);
256let serialized = serde_json::to_value(&message).unwrap();
257assert_eq!(json, serialized);
258 }
259260#[test]
261fn test_login_declined() {
262let json = json!({
263"type": "m.login.declined",
264 });
265266let message: QrAuthMessage = serde_json::from_value(json.clone()).unwrap();
267assert_let!(QrAuthMessage::LoginDeclined = &message);
268let serialized = serde_json::to_value(&message).unwrap();
269assert_eq!(json, serialized);
270 }
271272#[test]
273fn test_login_failure() {
274let json = json!({
275"type": "m.login.failure",
276"reason": "unsupported_protocol",
277"homeserver": "https://matrix-client.matrix.org/"
278});
279280let message: QrAuthMessage = serde_json::from_value(json.clone()).unwrap();
281assert_let!(QrAuthMessage::LoginFailure { reason, .. } = &message);
282assert_eq!(reason, &LoginFailureReason::UnsupportedProtocol);
283let serialized = serde_json::to_value(&message).unwrap();
284assert_eq!(json, serialized);
285 }
286287#[test]
288fn test_login_secrets() {
289let json = json!({
290"type": "m.login.secrets",
291"cross_signing": {
292"master_key": "rTtSv67XGS6k/rg6/yTG/m573cyFTPFRqluFhQY+hSw",
293"self_signing_key": "4jbPt7jh5D2iyM4U+3IDa+WthgJB87IQN1ATdkau+xk",
294"user_signing_key": "YkFKtkjcsTxF6UAzIIG/l6Nog/G2RigCRfWj3cjNWeM",
295 },
296"backup": {
297"algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
298"backup_version": "2",
299"key": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
300},
301 });
302303let message: QrAuthMessage = serde_json::from_value(json.clone()).unwrap();
304assert_let!(
305 QrAuthMessage::LoginSecrets(SecretsBundle { cross_signing, backup }) = &message
306 );
307assert_eq!(cross_signing.master_key, "rTtSv67XGS6k/rg6/yTG/m573cyFTPFRqluFhQY+hSw");
308assert_eq!(cross_signing.self_signing_key, "4jbPt7jh5D2iyM4U+3IDa+WthgJB87IQN1ATdkau+xk");
309assert_eq!(cross_signing.user_signing_key, "YkFKtkjcsTxF6UAzIIG/l6Nog/G2RigCRfWj3cjNWeM");
310311assert_let!(Some(BackupSecrets::MegolmBackupV1Curve25519AesSha2(backup)) = backup);
312assert_eq!(backup.backup_version, "2");
313assert_eq!(&backup.key.to_base64(), "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
314315let serialized = serde_json::to_value(&message).unwrap();
316assert_eq!(json, serialized);
317 }
318}