-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathlogin.rs
More file actions
1406 lines (1204 loc) · 54.7 KB
/
login.rs
File metadata and controls
1406 lines (1204 loc) · 54.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2024 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::future::IntoFuture;
use eyeball::SharedObservable;
use futures_core::Stream;
use matrix_sdk_base::{
SessionMeta, boxed_into_future,
crypto::types::qr_login::{QrCodeData, QrCodeIntent},
store::RoomLoadSettings,
};
use oauth2::{DeviceCodeErrorResponseType, StandardDeviceAuthorizationResponse};
use ruma::{
OwnedDeviceId,
api::client::discovery::get_authorization_server_metadata::v1::AuthorizationServerMetadata,
};
use tracing::trace;
use vodozemac::Curve25519PublicKey;
#[cfg(doc)]
use vodozemac::ecies::CheckCode;
use super::{
DeviceAuthorizationOAuthError, QRCodeLoginError, SecureChannelError,
messages::{LoginFailureReason, QrAuthMessage},
secure_channel::{EstablishedSecureChannel, SecureChannel},
};
use crate::{
Client,
authentication::oauth::{
ClientRegistrationData, OAuth, OAuthError,
qrcode::{CheckCodeSender, GeneratedQrProgress, LoginProtocolType, QrProgress},
},
};
async fn send_unexpected_message_error(
channel: &mut EstablishedSecureChannel,
) -> Result<(), SecureChannelError> {
channel
.send_json(QrAuthMessage::LoginFailure {
reason: LoginFailureReason::UnexpectedMessageReceived,
homeserver: None,
})
.await
}
async fn finish_login<Q>(
client: &Client,
mut channel: EstablishedSecureChannel,
registration_data: Option<&ClientRegistrationData>,
state: SharedObservable<LoginProgress<Q>>,
) -> Result<(), QRCodeLoginError> {
let oauth = client.oauth();
// Register the client with the OAuth 2.0 authorization server.
trace!("Registering the client with the OAuth 2.0 authorization server.");
let server_metadata = register_client(&oauth, registration_data).await?;
// We want to use the Curve25519 public key for the device ID, so let's generate
// a new vodozemac `Account` now.
let account = vodozemac::olm::Account::new();
let public_key = account.identity_keys().curve25519;
let device_id = public_key;
// Let's tell the OAuth 2.0 authorization server that we want to log in using
// the device authorization grant described in [RFC8628](https://datatracker.ietf.org/doc/html/rfc8628).
trace!("Requesting device authorization.");
let auth_grant_response =
request_device_authorization(&oauth, &server_metadata, device_id).await?;
// Now we need to inform the other device of the login protocols we picked and
// the URL they should use to log us in.
trace!("Letting the existing device know about the device authorization grant.");
let message =
QrAuthMessage::authorization_grant_login_protocol((&auth_grant_response).into(), device_id);
channel.send_json(&message).await?;
// Let's see if the other device agreed to our proposed protocols.
match channel.receive_json().await? {
QrAuthMessage::LoginProtocolAccepted => (),
QrAuthMessage::LoginFailure { reason, homeserver } => {
return Err(QRCodeLoginError::LoginFailure { reason, homeserver });
}
message => {
send_unexpected_message_error(&mut channel).await?;
return Err(QRCodeLoginError::UnexpectedMessage {
expected: "m.login.protocol_accepted",
received: message,
});
}
}
// The OAuth 2.0 authorization server may or may not show this user code to
// double check that we're talking to the right server. Let us display this, so
// the other device can double check this as well.
let user_code = auth_grant_response.user_code();
state.set(LoginProgress::WaitingForToken { user_code: user_code.secret().to_owned() });
// Let's now wait for the access token to be provided to use by the OAuth 2.0
// authorization server.
trace!("Waiting for the OAuth 2.0 authorization server to give us the access token.");
if let Err(e) = wait_for_tokens(&oauth, &server_metadata, &auth_grant_response).await {
// If we received an error, and it's one of the ones we should report to the
// other side, do so now.
if let Some(e) = e.as_request_token_error() {
match e {
DeviceCodeErrorResponseType::AccessDenied => {
channel.send_json(QrAuthMessage::LoginDeclined).await?;
}
DeviceCodeErrorResponseType::ExpiredToken => {
channel
.send_json(QrAuthMessage::LoginFailure {
reason: LoginFailureReason::AuthorizationExpired,
homeserver: None,
})
.await?;
}
_ => (),
}
}
return Err(e.into());
}
// We only received an access token from the OAuth 2.0 authorization server, we
// have no clue who we are, so we need to figure out our user ID
// now. TODO: This snippet is almost the same as the
// OAuth::finish_login_method(), why is that method even a public
// method and not called as part of the set session tokens method.
trace!("Discovering our own user id.");
let whoami_response = client.whoami().await.map_err(QRCodeLoginError::UserIdDiscovery)?;
client
.base_client()
.activate(
SessionMeta {
user_id: whoami_response.user_id,
device_id: OwnedDeviceId::from(device_id.to_base64()),
},
RoomLoadSettings::default(),
Some(account),
)
.await
.map_err(|error| QRCodeLoginError::SessionTokens(error.into()))?;
client.oauth().enable_cross_process_lock().await?;
state.set(LoginProgress::SyncingSecrets);
// Tell the existing device that we're logged in.
trace!("Telling the existing device that we successfully logged in.");
let message = QrAuthMessage::LoginSuccess;
channel.send_json(&message).await?;
// Let's wait for the secrets bundle to be sent to us, otherwise we won't be a
// fully E2EE enabled device.
trace!("Waiting for the secrets bundle.");
let bundle = match channel.receive_json().await? {
QrAuthMessage::LoginSecrets(bundle) => bundle,
QrAuthMessage::LoginFailure { reason, homeserver } => {
return Err(QRCodeLoginError::LoginFailure { reason, homeserver });
}
message => {
send_unexpected_message_error(&mut channel).await?;
return Err(QRCodeLoginError::UnexpectedMessage {
expected: "m.login.secrets",
received: message,
});
}
};
// Import the secrets bundle, this will allow us to sign the device keys with
// the master key when we upload them.
client.encryption().import_secrets_bundle_impl(&bundle).await?;
// Upload the device keys, this will ensure that other devices see us as a fully
// verified device ass soon as this method returns.
client
.encryption()
.ensure_device_keys_upload()
.await
.map_err(QRCodeLoginError::DeviceKeyUpload)?;
// Run and wait for the E2EE initialization tasks, this will ensure that we
// ourselves see us as verified and the recovery/backup states will
// be known. If we did receive all the secrets in the secrets
// bundle, then backups will be enabled after this step as well.
client.encryption().spawn_initialization_task(None).await;
client.encryption().wait_for_e2ee_initialization_tasks().await;
trace!("successfully logged in and enabled E2EE.");
// Tell our listener that we're done.
state.set(LoginProgress::Done);
// And indeed, we are done with the login.
Ok(())
}
/// Register the client with the OAuth 2.0 authorization server.
///
/// Returns the authorization server metadata.
async fn register_client(
oauth: &OAuth,
registration_data: Option<&ClientRegistrationData>,
) -> Result<AuthorizationServerMetadata, DeviceAuthorizationOAuthError> {
let server_metadata = oauth.server_metadata().await.map_err(OAuthError::from)?;
oauth.use_registration_data(&server_metadata, registration_data).await?;
Ok(server_metadata)
}
async fn request_device_authorization(
oauth: &OAuth,
server_metadata: &AuthorizationServerMetadata,
device_id: Curve25519PublicKey,
) -> Result<StandardDeviceAuthorizationResponse, DeviceAuthorizationOAuthError> {
let response = oauth
.request_device_authorization(server_metadata, Some(device_id.to_base64().into()))
.await?;
Ok(response)
}
async fn wait_for_tokens(
oauth: &OAuth,
server_metadata: &AuthorizationServerMetadata,
auth_response: &StandardDeviceAuthorizationResponse,
) -> Result<(), DeviceAuthorizationOAuthError> {
oauth.exchange_device_code(server_metadata, auth_response).await?;
Ok(())
}
/// Type telling us about the progress of the QR code login.
#[derive(Clone, Debug, Default)]
pub enum LoginProgress<Q> {
/// We're just starting up, this is the default and initial state.
#[default]
Starting,
/// We have established the secure channel, but need to exchange the
/// [`CheckCode`] so the channel can be verified to indeed be secure.
EstablishingSecureChannel(Q),
/// We're waiting for the OAuth 2.0 authorization server to give us the
/// access token. This will only happen if the other device allows the
/// OAuth 2.0 authorization server to do so.
WaitingForToken {
/// The user code the OAuth 2.0 authorization server has given us, the
/// OAuth 2.0 authorization server might ask the other device to
/// enter this code.
user_code: String,
},
/// We are syncing secrets.
SyncingSecrets,
/// The login process has completed.
Done,
}
/// Named future for logging in by scanning a QR code with the
/// [`OAuth::login_with_qr_code()`] method.
#[derive(Debug)]
pub struct LoginWithQrCode<'a> {
client: &'a Client,
registration_data: Option<&'a ClientRegistrationData>,
qr_code_data: &'a QrCodeData,
state: SharedObservable<LoginProgress<QrProgress>>,
}
impl LoginWithQrCode<'_> {
/// Subscribe to the progress of QR code login.
///
/// It's usually necessary to subscribe to this to let the existing device
/// know about the [`CheckCode`] which is used to verify that the two
/// devices are communicating in a secure manner.
pub fn subscribe_to_progress(&self) -> impl Stream<Item = LoginProgress<QrProgress>> + use<> {
self.state.subscribe()
}
}
impl<'a> IntoFuture for LoginWithQrCode<'a> {
type Output = Result<(), QRCodeLoginError>;
boxed_into_future!(extra_bounds: 'a);
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
// Before we get here, the other device has created a new rendezvous session
// and presented a QR code which this device has scanned.
// -- MSC4108 Secure channel setup steps 1-3
// First things first, establish the secure channel. Since we're the one that
// scanned the QR code, we're certain that the secure channel is
// secure, under the assumption that we didn't scan the wrong QR code.
// -- MSC4108 Secure channel setup steps 3-5
let channel = self.establish_secure_channel().await?;
trace!("Established the secure channel.");
// The other side isn't yet sure that it's talking to the right device, show
// a check code so they can confirm.
// -- MSC4108 Secure channel setup step 6
let check_code = channel.check_code().to_owned();
self.state.set(LoginProgress::EstablishingSecureChannel(QrProgress { check_code }));
// The user now enters the checkcode on the other device which verifies it
// and will only facilitate the login if the code matches.
// -- MSC4108 Secure channel setup step 7
// Now attempt to finish the login.
// -- MSC4108 OAuth 2.0 login all steps
finish_login(self.client, channel, self.registration_data, self.state).await
})
}
}
impl<'a> LoginWithQrCode<'a> {
pub(crate) fn new(
client: &'a Client,
qr_code_data: &'a QrCodeData,
registration_data: Option<&'a ClientRegistrationData>,
) -> LoginWithQrCode<'a> {
LoginWithQrCode { client, registration_data, qr_code_data, state: Default::default() }
}
async fn establish_secure_channel(
&self,
) -> Result<EstablishedSecureChannel, SecureChannelError> {
let http_client = self.client.inner.http_client.inner.clone();
let channel = EstablishedSecureChannel::from_qr_code(
http_client,
self.qr_code_data,
QrCodeIntent::Login,
)
.await?;
Ok(channel)
}
}
/// Named future for logging in by generating a QR code with the
/// [`OAuth::login_with_qr_code()`] method.
#[derive(Debug)]
pub struct LoginWithGeneratedQrCode<'a> {
client: &'a Client,
registration_data: Option<&'a ClientRegistrationData>,
state: SharedObservable<LoginProgress<GeneratedQrProgress>>,
}
impl LoginWithGeneratedQrCode<'_> {
/// Subscribe to the progress of QR code login.
///
/// It's necessary to subscribe to this to show the QR code to the existing
/// device so it can send the check code back to this device.
pub fn subscribe_to_progress(
&self,
) -> impl Stream<Item = LoginProgress<GeneratedQrProgress>> + use<> {
self.state.subscribe()
}
}
impl<'a> IntoFuture for LoginWithGeneratedQrCode<'a> {
type Output = Result<(), QRCodeLoginError>;
boxed_into_future!(extra_bounds: 'a);
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
// Establish and verify the secure channel.
// -- MSC4108 Secure channel setup all steps
let mut channel = self.establish_secure_channel().await?;
trace!("Established the secure channel.");
// Wait for the other device to send us the m.login.protocols message
// so that we can discover the homeserver to use for logging in.
// -- MSC4108 OAuth 2.0 login step 1
let message = channel.receive_json().await?;
// Verify that the device authorization grant is supported and extract
// the homeserver URL.
let homeserver = match message {
QrAuthMessage::LoginProtocols { protocols, homeserver } => {
if !protocols.contains(&LoginProtocolType::DeviceAuthorizationGrant) {
channel
.send_json(QrAuthMessage::LoginFailure {
reason: LoginFailureReason::UnsupportedProtocol,
homeserver: None,
})
.await?;
return Err(QRCodeLoginError::LoginFailure {
reason: LoginFailureReason::UnsupportedProtocol,
homeserver: None,
});
}
homeserver
}
_ => {
send_unexpected_message_error(&mut channel).await?;
return Err(QRCodeLoginError::UnexpectedMessage {
expected: "m.login.protocols",
received: message,
});
}
};
// Change the login homeserver if it is different from the server hosting the
// secure channel.
if self.client.homeserver() != homeserver {
self.client
.switch_homeserver_and_re_resolve_well_known(homeserver)
.await
.map_err(QRCodeLoginError::ServerReset)?;
}
// Proceed with logging in.
// -- MSC4108 OAuth 2.0 login remaining steps
finish_login(self.client, channel, self.registration_data, self.state).await
})
}
}
impl<'a> LoginWithGeneratedQrCode<'a> {
pub(crate) fn new(
client: &'a Client,
registration_data: Option<&'a ClientRegistrationData>,
) -> Self {
Self { client, registration_data, state: Default::default() }
}
async fn establish_secure_channel(
&self,
) -> Result<EstablishedSecureChannel, SecureChannelError> {
let http_client = self.client.inner.http_client.clone();
// Create a new ephemeral key pair and a rendezvous session to request a login
// with.
// -- MSC4108 Secure channel setup steps 1 & 2
let secure_channel = SecureChannel::login(http_client, &self.client.homeserver()).await?;
// Extract the QR code data and emit a progress update so that the caller can
// present the QR code for scanning by the other device.
// -- MSC4108 Secure channel setup step 3
let qr_code_data = secure_channel.qr_code_data().clone();
trace!("Generated QR code.");
self.state.set(LoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrReady(
qr_code_data,
)));
// Wait for the secure channel to connect. The other device now needs to scan
// the QR code and send us the LoginInitiateMessage which we respond to
// with the LoginOkMessage. -- MSC4108 step 4 & 5
let channel = secure_channel.connect().await?;
// The other device now verifies our message, computes the checkcode and
// displays it. We emit a progress update to let the caller prompt the
// user to enter the checkcode and feed it back to us.
// -- MSC4108 Secure channel setup step 6
trace!("Waiting for checkcode.");
let (tx, rx) = tokio::sync::oneshot::channel();
self.state.set(LoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrScanned(
CheckCodeSender::new(tx),
)));
// Retrieve the entered checkcode and verify it to confirm that the channel is
// actually secure.
// -- MSC4108 Secure channel setup step 7
let check_code = rx.await.map_err(|_| SecureChannelError::CannotReceiveCheckCode)?;
trace!("Received check code.");
channel.confirm(check_code)
}
}
#[cfg(all(test, not(target_family = "wasm")))]
mod test {
use std::time::Duration;
use assert_matches2::{assert_let, assert_matches};
use futures_util::StreamExt;
use matrix_sdk_base::crypto::types::{
SecretsBundle,
qr_login::{Msc4108IntentData, QrCodeIntentData},
};
use matrix_sdk_common::executor::spawn;
use matrix_sdk_test::async_test;
use serde_json::json;
use vodozemac::ecies::CheckCode;
use super::*;
use crate::{
authentication::oauth::qrcode::{
messages::LoginProtocolType,
secure_channel::{SecureChannel, test::MockedRendezvousServer},
},
config::RequestConfig,
http_client::HttpClient,
test_utils::{client::oauth::mock_client_metadata, mocks::MatrixMockServer},
};
enum AliceBehaviour {
HappyPath,
DeclinedProtocol,
UnexpectedMessage,
UnexpectedMessageInsteadOfSecrets,
RefuseSecrets,
LetSessionExpire,
}
/// The possible token responses.
enum TokenResponse {
Ok,
AccessDenied,
ExpiredToken,
}
fn secrets_bundle() -> SecretsBundle {
let json = json!({
"cross_signing": {
"master_key": "rTtSv67XGS6k/rg6/yTG/m573cyFTPFRqluFhQY+hSw",
"self_signing_key": "4jbPt7jh5D2iyM4U+3IDa+WthgJB87IQN1ATdkau+xk",
"user_signing_key": "YkFKtkjcsTxF6UAzIIG/l6Nog/G2RigCRfWj3cjNWeM",
},
});
serde_json::from_value(json).expect("We should be able to deserialize a secrets bundle")
}
/// This is most of the code that is required to be the other side, the
/// existing device, of the QR login dance.
async fn grant_login(
alice: SecureChannel,
check_code_receiver: tokio::sync::oneshot::Receiver<CheckCode>,
behavior: AliceBehaviour,
) {
let alice = alice.connect().await.expect("Alice should be able to connect the channel");
let check_code =
check_code_receiver.await.expect("We should receive the check code from bob");
let mut alice = alice
.confirm(check_code.to_digit())
.expect("Alice should be able to confirm the secure channel");
let message = alice
.receive_json()
.await
.expect("Alice should be able to receive the initial message from Bob");
assert_let!(QrAuthMessage::LoginProtocol { protocol, .. } = message);
assert_eq!(protocol, LoginProtocolType::DeviceAuthorizationGrant);
let message = match behavior {
AliceBehaviour::DeclinedProtocol => QrAuthMessage::LoginFailure {
reason: LoginFailureReason::UnsupportedProtocol,
homeserver: None,
},
AliceBehaviour::UnexpectedMessage => QrAuthMessage::LoginDeclined,
_ => QrAuthMessage::LoginProtocolAccepted,
};
alice.send_json(message).await.unwrap();
let message: QrAuthMessage = alice.receive_json().await.unwrap();
assert_let!(QrAuthMessage::LoginSuccess = message);
let message = match behavior {
AliceBehaviour::UnexpectedMessageInsteadOfSecrets => QrAuthMessage::LoginDeclined,
AliceBehaviour::RefuseSecrets => QrAuthMessage::LoginFailure {
reason: LoginFailureReason::DeviceNotFound,
homeserver: None,
},
_ => QrAuthMessage::LoginSecrets(secrets_bundle()),
};
alice.send_json(message).await.unwrap();
}
#[async_test]
async fn test_qr_login() {
let server = MatrixMockServer::new().await;
let rendezvous_server =
MockedRendezvousServer::new(server.server(), "abcdEFG12345", Duration::MAX).await;
let (sender, receiver) = tokio::sync::oneshot::channel();
let oauth_server = server.oauth();
oauth_server.mock_server_metadata().ok().expect(1).named("server_metadata").mount().await;
oauth_server.mock_registration().ok().expect(1).named("registration").mount().await;
oauth_server
.mock_device_authorization()
.ok()
.expect(1)
.named("device_authorization")
.mount()
.await;
oauth_server.mock_token().ok().expect(1).named("token").mount().await;
server.mock_versions().ok().expect(1..).named("versions").mount().await;
server.mock_who_am_i().ok().expect(1).named("whoami").mount().await;
server.mock_upload_keys().ok().expect(1).named("upload_keys").mount().await;
server.mock_query_keys().ok().expect(1).named("query_keys").mount().await;
let client = HttpClient::new(reqwest::Client::new(), Default::default());
let alice = SecureChannel::reciprocate(client, &rendezvous_server.homeserver_url)
.await
.expect("Alice should be able to create a secure channel.");
assert_let!(
QrCodeIntentData::Msc4108 {
data: Msc4108IntentData::Reciprocate { server_name },
..
} = &alice.qr_code_data().intent_data()
);
let bob = Client::builder()
.server_name_or_homeserver_url(server_name)
.request_config(RequestConfig::new().disable_retry())
.build()
.await
.expect("We should be able to build the Client object from the URL in the QR code");
let qr_code = alice.qr_code_data().clone();
let oauth = bob.oauth();
let registration_data = mock_client_metadata().into();
let login_bob = oauth.login_with_qr_code(Some(®istration_data)).scan(&qr_code);
let mut updates = login_bob.subscribe_to_progress();
let updates_task = spawn(async move {
let mut sender = Some(sender);
while let Some(update) = updates.next().await {
match update {
LoginProgress::EstablishingSecureChannel(QrProgress { check_code }) => {
sender
.take()
.expect("The establishing secure channel update should be received only once")
.send(check_code)
.expect("Bob should be able to send the check code to Alice");
}
LoginProgress::Done => break,
_ => (),
}
}
});
let alice_task =
spawn(async { grant_login(alice, receiver, AliceBehaviour::HappyPath).await });
// Wait for all tasks to finish.
login_bob.await.expect("Bob should be able to login");
alice_task.await.expect("Alice should have completed it's task successfully");
updates_task.await.unwrap();
assert!(bob.encryption().cross_signing_status().await.unwrap().is_complete());
let own_identity =
bob.encryption().get_user_identity(bob.user_id().unwrap()).await.unwrap().unwrap();
assert!(own_identity.is_verified());
}
async fn grant_login_with_generated_qr(
alice: &Client,
qr_receiver: tokio::sync::oneshot::Receiver<QrCodeData>,
cctx_receiver: tokio::sync::oneshot::Receiver<CheckCodeSender>,
behavior: AliceBehaviour,
) {
let qr_code_data = qr_receiver.await.expect("Alice should receive the QR code");
let mut channel = EstablishedSecureChannel::from_qr_code(
alice.inner.http_client.inner.clone(),
&qr_code_data,
QrCodeIntent::Reciprocate,
)
.await
.expect("Alice should be able to establish the secure channel");
trace!("Established the secure channel.");
// The other side isn't yet sure that it's talking to the right device, show
// a check code so they can confirm.
let check_code = channel.check_code().to_digit();
let check_code_sender =
cctx_receiver.await.expect("Alice should receive the CheckCodeSender");
check_code_sender
.send(check_code)
.await
.expect("Alice should be able to send the check code to Bob");
// Alice sends m.login.protocols message
let message = QrAuthMessage::LoginProtocols {
protocols: vec![LoginProtocolType::DeviceAuthorizationGrant],
homeserver: alice.homeserver(),
};
channel
.send_json(message)
.await
.expect("Alice should be able to send the `m.login.protocols` message to Bob");
// Alice receives m.login.protocol message
let message: QrAuthMessage = channel
.receive_json()
.await
.expect("Alice should be able to receive the `m.login.protocol` message from Bob");
assert_let!(QrAuthMessage::LoginProtocol { protocol, .. } = message);
assert_eq!(protocol, LoginProtocolType::DeviceAuthorizationGrant);
// Alice sends m.login.protocol_accepted message
let message = match behavior {
AliceBehaviour::DeclinedProtocol => QrAuthMessage::LoginFailure {
reason: LoginFailureReason::UnsupportedProtocol,
homeserver: None,
},
AliceBehaviour::UnexpectedMessage => QrAuthMessage::LoginDeclined,
_ => QrAuthMessage::LoginProtocolAccepted,
};
channel
.send_json(message)
.await
.expect("Alice should be able to send the `m.login.protocol_accepted` message to Bob");
let message: QrAuthMessage = channel
.receive_json()
.await
.expect("Alice should be able to receive the `m.login.success` message from Bob");
assert_let!(QrAuthMessage::LoginSuccess = message);
// Alice sends m.login.secrets message
let message = match behavior {
AliceBehaviour::UnexpectedMessageInsteadOfSecrets => QrAuthMessage::LoginDeclined,
AliceBehaviour::RefuseSecrets => QrAuthMessage::LoginFailure {
reason: LoginFailureReason::DeviceNotFound,
homeserver: None,
},
_ => QrAuthMessage::LoginSecrets(secrets_bundle()),
};
channel
.send_json(message)
.await
.expect("Alice should be able to send the `m.login.secrets` message to Bob");
}
#[async_test]
async fn test_generated_qr_login() {
let server = MatrixMockServer::new().await;
let rendezvous_server =
MockedRendezvousServer::new(server.server(), "abcdEFG12345", Duration::MAX).await;
let (qr_sender, qr_receiver) = tokio::sync::oneshot::channel();
let (cctx_sender, cctx_receiver) = tokio::sync::oneshot::channel();
let oauth_server = server.oauth();
oauth_server.mock_server_metadata().ok().expect(1).named("server_metadata").mount().await;
oauth_server.mock_registration().ok().expect(1).named("registration").mount().await;
oauth_server
.mock_device_authorization()
.ok()
.expect(1)
.named("device_authorization")
.mount()
.await;
oauth_server.mock_token().ok().expect(1).named("token").mount().await;
server.mock_versions().ok().expect(1..).named("versions").mount().await;
server.mock_who_am_i().ok().expect(1).named("whoami").mount().await;
server.mock_upload_keys().ok().expect(1).named("upload_keys").mount().await;
server.mock_query_keys().ok().expect(1).named("query_keys").mount().await;
let homeserver_url = rendezvous_server.homeserver_url.clone();
// Create Alice, the existing client, as a logged-in client. They will scan the
// QR code generated by Bob.
let alice = server.client_builder().logged_in_with_oauth().build().await;
assert!(alice.session_meta().is_some(), "Alice should be logged in");
// Create Bob, the new client. They will generate the QR code.
let bob = Client::builder()
.server_name_or_homeserver_url(&homeserver_url)
.request_config(RequestConfig::new().disable_retry())
.build()
.await
.expect("Should be able to create a client for Bob");
let secure_channel = SecureChannel::login(bob.inner.http_client.clone(), &homeserver_url)
.await
.expect("Bob should be able to create a secure channel");
assert_matches!(
secure_channel.qr_code_data().intent_data(),
QrCodeIntentData::Msc4108 { data: Msc4108IntentData::Login, .. }
);
let registration_data = mock_client_metadata().into();
let bob_oauth = bob.oauth();
let bob_login = bob_oauth.login_with_qr_code(Some(®istration_data)).generate();
let mut bob_updates = bob_login.subscribe_to_progress();
let updates_task = spawn(async move {
let mut qr_sender = Some(qr_sender);
let mut cctx_sender = Some(cctx_sender);
while let Some(update) = bob_updates.next().await {
match update {
LoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrReady(qr)) => {
qr_sender
.take()
.expect("The establishing secure channel update with a qr code should be received only once")
.send(qr)
.expect("Bob should be able to send the qr code code to Alice");
}
LoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrScanned(
cctx,
)) => {
cctx_sender
.take()
.expect("The establishing secure channel update with a CheckCodeSender should be received only once")
.send(cctx)
.expect("Bob should be able to send the qr code code to Alice");
}
LoginProgress::Done => break,
_ => (),
}
}
});
let alice_task = spawn(async move {
grant_login_with_generated_qr(
&alice,
qr_receiver,
cctx_receiver,
AliceBehaviour::HappyPath,
)
.await
});
// Wait for all tasks to finish.
bob_login.await.expect("Bob should be able to login");
alice_task.await.expect("Alice should have completed it's task successfully");
updates_task.await.unwrap();
assert!(bob.encryption().cross_signing_status().await.unwrap().is_complete());
let own_identity =
bob.encryption().get_user_identity(bob.user_id().unwrap()).await.unwrap().unwrap();
assert!(own_identity.is_verified());
}
#[async_test]
async fn test_generated_qr_login_with_homeserver_swap() {
let server = MatrixMockServer::new().await;
let rendezvous_server =
MockedRendezvousServer::new(server.server(), "abcdEFG12345", Duration::MAX).await;
let (qr_sender, qr_receiver) = tokio::sync::oneshot::channel();
let (cctx_sender, cctx_receiver) = tokio::sync::oneshot::channel();
let login_server = MatrixMockServer::new().await;
let oauth_server = login_server.oauth();
oauth_server.mock_server_metadata().ok().expect(1).named("server_metadata").mount().await;
oauth_server.mock_registration().ok().expect(1).named("registration").mount().await;
oauth_server
.mock_device_authorization()
.ok()
.expect(1)
.named("device_authorization")
.mount()
.await;
oauth_server.mock_token().ok().expect(1).named("token").mount().await;
server.mock_versions().ok().expect(1..).named("versions").mount().await;
login_server.mock_well_known().ok().expect(1).named("well_known").mount().await;
login_server.mock_versions().ok().expect(1..).named("versions").mount().await;
login_server.mock_who_am_i().ok().expect(1).named("whoami").mount().await;
login_server.mock_upload_keys().ok().expect(1).named("upload_keys").mount().await;
login_server.mock_query_keys().ok().expect(1).named("query_keys").mount().await;
let homeserver_url = rendezvous_server.homeserver_url.clone();
// Create Alice, the existing client, as a logged-in client. They will scan the
// QR code generated by Bob.
let alice = login_server.client_builder().logged_in_with_oauth().build().await;
assert!(alice.session_meta().is_some(), "Alice should be logged in");
// Create Bob, the new client. They will generate the QR code.
let bob = Client::builder()
.server_name_or_homeserver_url(&homeserver_url)
.request_config(RequestConfig::new().disable_retry())
.build()
.await
.expect("Should be able to create a client for Bob");
let secure_channel = SecureChannel::login(bob.inner.http_client.clone(), &homeserver_url)
.await
.expect("Bob should be able to create a secure channel");
assert_matches!(
secure_channel.qr_code_data().intent_data(),
QrCodeIntentData::Msc4108 { data: Msc4108IntentData::Login, .. }
);
let registration_data = mock_client_metadata().into();
let bob_oauth = bob.oauth();
let bob_login = bob_oauth.login_with_qr_code(Some(®istration_data)).generate();
let mut bob_updates = bob_login.subscribe_to_progress();
let updates_task = spawn(async move {
let mut qr_sender = Some(qr_sender);
let mut cctx_sender = Some(cctx_sender);
while let Some(update) = bob_updates.next().await {
match update {
LoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrReady(qr)) => {
qr_sender
.take()
.expect("The establishing secure channel update with a qr code should be received only once")
.send(qr)
.expect("Bob should be able to send the qr code code to Alice");
}
LoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrScanned(
cctx,
)) => {
cctx_sender
.take()
.expect("The establishing secure channel update with a CheckCodeSender should be received only once")
.send(cctx)
.expect("Bob should be able to send the qr code code to Alice");
}
LoginProgress::Done => break,
_ => (),
}
}
});
let alice_task = spawn(async move {
grant_login_with_generated_qr(
&alice,
qr_receiver,
cctx_receiver,
AliceBehaviour::HappyPath,
)
.await
});
// Wait for all tasks to finish.
bob_login.await.expect("Bob should be able to login");
alice_task.await.expect("Alice should have completed it's task successfully");
updates_task.await.unwrap();
assert!(bob.encryption().cross_signing_status().await.unwrap().is_complete());
let own_identity =
bob.encryption().get_user_identity(bob.user_id().unwrap()).await.unwrap().unwrap();
assert!(own_identity.is_verified());
}
async fn test_failure(
token_response: TokenResponse,
alice_behavior: AliceBehaviour,
) -> Result<(), QRCodeLoginError> {
let server = MatrixMockServer::new().await;
let expiration = match alice_behavior {
AliceBehaviour::LetSessionExpire => Duration::from_secs(2),
_ => Duration::MAX,
};
let rendezvous_server =
MockedRendezvousServer::new(server.server(), "abcdEFG12345", expiration).await;
let (sender, receiver) = tokio::sync::oneshot::channel();
let oauth_server = server.oauth();
let expected_calls = match alice_behavior {
AliceBehaviour::LetSessionExpire => 0,
_ => 1,
};
oauth_server
.mock_server_metadata()
.ok()
.expect(expected_calls)
.named("server_metadata")
.mount()
.await;
oauth_server
.mock_registration()
.ok()
.expect(expected_calls)
.named("registration")
.mount()
.await;
oauth_server
.mock_device_authorization()
.ok()