@@ -2,11 +2,7 @@ use std::{num::NonZeroUsize, sync::Arc, time::Duration};
2
2
3
3
use futures_util:: StreamExt ;
4
4
use matrix_sdk:: {
5
- authentication:: oauth:: qrcode:: { self , DeviceCodeErrorResponseType , LoginFailureReason } ,
6
- crypto:: {
7
- types:: qr_login:: { LoginQrCodeDecodeError , QrCodeModeData } ,
8
- CollectStrategy , TrustRequirement ,
9
- } ,
5
+ crypto:: { types:: qr_login:: QrCodeModeData , CollectStrategy , TrustRequirement } ,
10
6
encryption:: { BackupDownloadStrategy , EncryptionSettings } ,
11
7
event_cache:: EventCacheError ,
12
8
reqwest:: Certificate ,
@@ -18,7 +14,6 @@ use matrix_sdk::{
18
14
Client as MatrixClient , ClientBuildError as MatrixClientBuildError , HttpError , IdParseError ,
19
15
RumaApiError ,
20
16
} ;
21
- use matrix_sdk_common:: { SendOutsideWasm , SyncOutsideWasm } ;
22
17
use ruma:: api:: error:: { DeserializationError , FromHttpResponseError } ;
23
18
use tracing:: { debug, error} ;
24
19
@@ -28,6 +23,7 @@ use crate::{
28
23
client:: ClientSessionDelegate ,
29
24
error:: ClientError ,
30
25
helpers:: unwrap_or_clone_arc,
26
+ qr_code:: { HumanQrLoginError , QrCodeData , QrLoginProgressListener } ,
31
27
runtime:: get_runtime_handle,
32
28
session_store:: { SessionStoreConfig , SessionStoreResult } ,
33
29
task_handle:: TaskHandle ,
@@ -43,164 +39,6 @@ enum HomeserverConfig {
43
39
ServerNameOrUrl ( String ) ,
44
40
}
45
41
46
- /// Data for the QR code login mechanism.
47
- ///
48
- /// The [`QrCodeData`] can be serialized and encoded as a QR code or it can be
49
- /// decoded from a QR code.
50
- #[ derive( Debug , uniffi:: Object ) ]
51
- pub struct QrCodeData {
52
- inner : qrcode:: QrCodeData ,
53
- }
54
-
55
- #[ matrix_sdk_ffi_macros:: export]
56
- impl QrCodeData {
57
- /// Attempt to decode a slice of bytes into a [`QrCodeData`] object.
58
- ///
59
- /// The slice of bytes would generally be returned by a QR code decoder.
60
- #[ uniffi:: constructor]
61
- pub fn from_bytes ( bytes : Vec < u8 > ) -> Result < Arc < Self > , QrCodeDecodeError > {
62
- Ok ( Self { inner : qrcode:: QrCodeData :: from_bytes ( & bytes) ? } . into ( ) )
63
- }
64
-
65
- /// The server name contained within the scanned QR code data.
66
- ///
67
- /// Note: This value is only present when scanning a QR code the belongs to
68
- /// a logged in client. The mode where the new client shows the QR code
69
- /// will return `None`.
70
- pub fn server_name ( & self ) -> Option < String > {
71
- match & self . inner . mode_data {
72
- QrCodeModeData :: Reciprocate { server_name } => Some ( server_name. to_owned ( ) ) ,
73
- QrCodeModeData :: Login => None ,
74
- }
75
- }
76
- }
77
-
78
- /// Error type for the decoding of the [`QrCodeData`].
79
- #[ derive( Debug , thiserror:: Error , uniffi:: Error ) ]
80
- #[ uniffi( flat_error) ]
81
- pub enum QrCodeDecodeError {
82
- #[ error( "Error decoding QR code: {error:?}" ) ]
83
- Crypto {
84
- #[ from]
85
- error : LoginQrCodeDecodeError ,
86
- } ,
87
- }
88
-
89
- #[ derive( Debug , thiserror:: Error , uniffi:: Error ) ]
90
- pub enum HumanQrLoginError {
91
- #[ error( "Linking with this device is not supported." ) ]
92
- LinkingNotSupported ,
93
- #[ error( "The sign in was cancelled." ) ]
94
- Cancelled ,
95
- #[ error( "The sign in was not completed in the required time." ) ]
96
- Expired ,
97
- #[ error( "A secure connection could not have been established between the two devices." ) ]
98
- ConnectionInsecure ,
99
- #[ error( "The sign in was declined." ) ]
100
- Declined ,
101
- #[ error( "An unknown error has happened." ) ]
102
- Unknown ,
103
- #[ error( "The homeserver doesn't provide sliding sync in its configuration." ) ]
104
- SlidingSyncNotAvailable ,
105
- #[ error( "Unable to use OIDC as the supplied client metadata is invalid." ) ]
106
- OidcMetadataInvalid ,
107
- #[ error( "The other device is not signed in and as such can't sign in other devices." ) ]
108
- OtherDeviceNotSignedIn ,
109
- }
110
-
111
- impl From < qrcode:: QRCodeLoginError > for HumanQrLoginError {
112
- fn from ( value : qrcode:: QRCodeLoginError ) -> Self {
113
- use qrcode:: { QRCodeLoginError , SecureChannelError } ;
114
-
115
- match value {
116
- QRCodeLoginError :: LoginFailure { reason, .. } => match reason {
117
- LoginFailureReason :: UnsupportedProtocol => HumanQrLoginError :: LinkingNotSupported ,
118
- LoginFailureReason :: AuthorizationExpired => HumanQrLoginError :: Expired ,
119
- LoginFailureReason :: UserCancelled => HumanQrLoginError :: Cancelled ,
120
- _ => HumanQrLoginError :: Unknown ,
121
- } ,
122
-
123
- QRCodeLoginError :: OAuth ( e) => {
124
- if let Some ( e) = e. as_request_token_error ( ) {
125
- match e {
126
- DeviceCodeErrorResponseType :: AccessDenied => HumanQrLoginError :: Declined ,
127
- DeviceCodeErrorResponseType :: ExpiredToken => HumanQrLoginError :: Expired ,
128
- _ => HumanQrLoginError :: Unknown ,
129
- }
130
- } else {
131
- HumanQrLoginError :: Unknown
132
- }
133
- }
134
-
135
- QRCodeLoginError :: SecureChannel ( e) => match e {
136
- SecureChannelError :: Utf8 ( _)
137
- | SecureChannelError :: MessageDecode ( _)
138
- | SecureChannelError :: Json ( _)
139
- | SecureChannelError :: RendezvousChannel ( _) => HumanQrLoginError :: Unknown ,
140
- SecureChannelError :: SecureChannelMessage { .. }
141
- | SecureChannelError :: Ecies ( _)
142
- | SecureChannelError :: InvalidCheckCode => HumanQrLoginError :: ConnectionInsecure ,
143
- SecureChannelError :: InvalidIntent => HumanQrLoginError :: OtherDeviceNotSignedIn ,
144
- } ,
145
-
146
- QRCodeLoginError :: UnexpectedMessage { .. }
147
- | QRCodeLoginError :: CrossProcessRefreshLock ( _)
148
- | QRCodeLoginError :: DeviceKeyUpload ( _)
149
- | QRCodeLoginError :: SessionTokens ( _)
150
- | QRCodeLoginError :: UserIdDiscovery ( _)
151
- | QRCodeLoginError :: SecretImport ( _) => HumanQrLoginError :: Unknown ,
152
- }
153
- }
154
- }
155
-
156
- /// Enum describing the progress of the QR-code login.
157
- #[ derive( Debug , Default , Clone , uniffi:: Enum ) ]
158
- pub enum QrLoginProgress {
159
- /// The login process is starting.
160
- #[ default]
161
- Starting ,
162
- /// We established a secure channel with the other device.
163
- EstablishingSecureChannel {
164
- /// The check code that the device should display so the other device
165
- /// can confirm that the channel is secure as well.
166
- check_code : u8 ,
167
- /// The string representation of the check code, will be guaranteed to
168
- /// be 2 characters long, preserving the leading zero if the
169
- /// first digit is a zero.
170
- check_code_string : String ,
171
- } ,
172
- /// We are waiting for the login and for the OAuth 2.0 authorization server
173
- /// to give us an access token.
174
- WaitingForToken { user_code : String } ,
175
- /// The login has successfully finished.
176
- Done ,
177
- }
178
-
179
- #[ matrix_sdk_ffi_macros:: export( callback_interface) ]
180
- pub trait QrLoginProgressListener : SyncOutsideWasm + SendOutsideWasm {
181
- fn on_update ( & self , state : QrLoginProgress ) ;
182
- }
183
-
184
- impl From < qrcode:: LoginProgress > for QrLoginProgress {
185
- fn from ( value : qrcode:: LoginProgress ) -> Self {
186
- use qrcode:: LoginProgress ;
187
-
188
- match value {
189
- LoginProgress :: Starting => Self :: Starting ,
190
- LoginProgress :: EstablishingSecureChannel { check_code } => {
191
- let check_code = check_code. to_digit ( ) ;
192
-
193
- Self :: EstablishingSecureChannel {
194
- check_code,
195
- check_code_string : format ! ( "{check_code:02}" ) ,
196
- }
197
- }
198
- LoginProgress :: WaitingForToken { user_code } => Self :: WaitingForToken { user_code } ,
199
- LoginProgress :: Done => Self :: Done ,
200
- }
201
- }
202
- }
203
-
204
42
#[ derive( Debug , thiserror:: Error , uniffi:: Error ) ]
205
43
#[ uniffi( flat_error) ]
206
44
pub enum ClientBuildError {
0 commit comments