@@ -22,13 +22,13 @@ use matrix_sdk_base::{
22
22
crypto:: types:: qr_login:: { QrCodeData , QrCodeMode } ,
23
23
SessionMeta ,
24
24
} ;
25
- use openidconnect :: DeviceCodeErrorResponseType ;
25
+ use oauth2 :: DeviceCodeErrorResponseType ;
26
26
use ruma:: OwnedDeviceId ;
27
27
use tracing:: trace;
28
28
use vodozemac:: ecies:: CheckCode ;
29
29
30
30
use super :: {
31
- messages:: LoginFailureReason , oidc_client :: OidcClient , DeviceAuhorizationOidcError ,
31
+ messages:: LoginFailureReason , oauth_client :: OauthClient , DeviceAuthorizationOauthError ,
32
32
SecureChannelError ,
33
33
} ;
34
34
#[ cfg( doc) ]
@@ -64,11 +64,13 @@ pub enum LoginProgress {
64
64
/// The check code we need to, out of band, send to the other device.
65
65
check_code : CheckCode ,
66
66
} ,
67
- /// We're waiting for the OIDC provider to give us the access token. This
68
- /// will only happen if the other device allows the OIDC provider to so.
67
+ /// We're waiting for the OAuth 2.0 authorization server to give us the
68
+ /// access token. This will only happen if the other device allows the
69
+ /// OAuth 2.0 authorization server to do so.
69
70
WaitingForToken {
70
- /// The user code the OIDC provider has given us, the OIDC provider
71
- /// might ask the other device to enter this code.
71
+ /// The user code the OAuth 2.0 authorization server has given us, the
72
+ /// OAuth 2.0 authorization server might ask the other device to
73
+ /// enter this code.
72
74
user_code : String ,
73
75
} ,
74
76
/// The login process has completed.
@@ -113,20 +115,20 @@ impl<'a> IntoFuture for LoginWithQrCode<'a> {
113
115
let check_code = channel. check_code ( ) . to_owned ( ) ;
114
116
self . state . set ( LoginProgress :: EstablishingSecureChannel { check_code } ) ;
115
117
116
- // Register the client with the OIDC provider .
117
- trace ! ( "Registering the client with the OIDC provider ." ) ;
118
- let oidc_client = self . register_client ( ) . await ?;
118
+ // Register the client with the OAuth 2.0 authorization server .
119
+ trace ! ( "Registering the client with the OAuth 2.0 authorization server ." ) ;
120
+ let oauth_client = self . register_client ( ) . await ?;
119
121
120
122
// We want to use the Curve25519 public key for the device ID, so let's generate
121
123
// a new vodozemac `Account` now.
122
124
let account = vodozemac:: olm:: Account :: new ( ) ;
123
125
let public_key = account. identity_keys ( ) . curve25519 ;
124
126
let device_id = public_key;
125
127
126
- // Let's tell the OIDC provider that we want to log in using the device
127
- // authorization grant described in [RFC8628](https://datatracker.ietf.org/doc/html/rfc8628).
128
+ // Let's tell the OAuth 2.0 authorization server that we want to log in using
129
+ // the device authorization grant described in [RFC8628](https://datatracker.ietf.org/doc/html/rfc8628).
128
130
trace ! ( "Requesting device authorization." ) ;
129
- let auth_grant_response = oidc_client . request_device_authorization ( device_id) . await ?;
131
+ let auth_grant_response = oauth_client . request_device_authorization ( device_id) . await ?;
130
132
131
133
// Now we need to inform the other device of the login protocols we picked and
132
134
// the URL they should use to log us in.
@@ -153,17 +155,17 @@ impl<'a> IntoFuture for LoginWithQrCode<'a> {
153
155
}
154
156
}
155
157
156
- // The OIDC provider may or may not show this user code to double check that
157
- // we're talking to the right OIDC provider . Let us display this, so
158
+ // The OAuth 2.0 authorization server may or may not show this user code to
159
+ // double check that we're talking to the right server . Let us display this, so
158
160
// the other device can double check this as well.
159
161
let user_code = auth_grant_response. user_code ( ) ;
160
162
self . state
161
163
. set ( LoginProgress :: WaitingForToken { user_code : user_code. secret ( ) . to_owned ( ) } ) ;
162
164
163
- // Let's now wait for the access token to be provided to use by the OIDC
164
- // provider .
165
- trace ! ( "Waiting for the OIDC provider to give us the access token." ) ;
166
- let session_tokens = match oidc_client . wait_for_tokens ( & auth_grant_response) . await {
165
+ // Let's now wait for the access token to be provided to use by the OAuth 2.0
166
+ // authorization server .
167
+ trace ! ( "Waiting for the OAuth 2.0 authorization server to give us the access token." ) ;
168
+ let session_tokens = match oauth_client . wait_for_tokens ( & auth_grant_response) . await {
167
169
Ok ( t) => t,
168
170
Err ( e) => {
169
171
// If we received an error, and it's one of the ones we should report to the
@@ -190,11 +192,11 @@ impl<'a> IntoFuture for LoginWithQrCode<'a> {
190
192
} ;
191
193
self . client . oidc ( ) . set_session_tokens ( session_tokens) ;
192
194
193
- // We only received an access token from the OIDC provider, we have no clue who
194
- // we are, so we need to figure out our user ID now.
195
- // TODO: This snippet is almost the same as the Oidc::finish_login_method(), why
196
- // is that method even a public method and not called as part of the set session
197
- // tokens method.
195
+ // We only received an access token from the OAuth 2.0 authorization server, we
196
+ // have no clue who we are, so we need to figure out our user ID
197
+ // now. TODO: This snippet is almost the same as the
198
+ // Oidc::finish_login_method(), why is that method even a public
199
+ // method and not called as part of the set session tokens method.
198
200
trace ! ( "Discovering our own user id." ) ;
199
201
let whoami_response =
200
202
self . client . whoami ( ) . await . map_err ( QRCodeLoginError :: UserIdDiscovery ) ?;
@@ -288,30 +290,26 @@ impl<'a> LoginWithQrCode<'a> {
288
290
Ok ( channel)
289
291
}
290
292
291
- async fn register_client ( & self ) -> Result < OidcClient , DeviceAuhorizationOidcError > {
292
- // Let's figure out the OIDC issuer, this fetches the info from the homeserver.
293
- let issuer = self
294
- . client
295
- . oidc ( )
293
+ async fn register_client ( & self ) -> Result < OauthClient , DeviceAuthorizationOauthError > {
294
+ let oidc = self . client . oidc ( ) ;
295
+
296
+ // Let's figure out the OAuth 2.0 issuer, this fetches the info from the
297
+ // homeserver.
298
+ let issuer = oidc
296
299
. fetch_authentication_issuer ( )
297
300
. await
298
- . map_err ( DeviceAuhorizationOidcError :: AuthenticationIssuer ) ?;
301
+ . map_err ( DeviceAuthorizationOauthError :: AuthenticationIssuer ) ?;
299
302
300
- // Now we register the client with the OIDC provider .
303
+ // Now we register the client with the OAuth 2.0 authorization server .
301
304
let registration_response =
302
- self . client . oidc ( ) . register_client ( & issuer, self . client_metadata . clone ( ) , None ) . await ?;
305
+ oidc. register_client ( & issuer, self . client_metadata . clone ( ) , None ) . await ?;
303
306
304
307
// We're now switching to the oauth2 crate, it has a bit of a strange API
305
308
// where you need to provide the HTTP client in every call you make.
306
309
let http_client = self . client . inner . http_client . clone ( ) ;
310
+ let server_metadata = oidc. provider_metadata ( ) . await ?;
307
311
308
- OidcClient :: new (
309
- registration_response. client_id ,
310
- issuer,
311
- http_client,
312
- registration_response. client_secret . as_deref ( ) ,
313
- )
314
- . await
312
+ OauthClient :: new ( registration_response. client_id , & server_metadata, http_client)
315
313
}
316
314
}
317
315
@@ -614,21 +612,26 @@ mod test {
614
612
alice. send_json ( message) . await . unwrap ( ) ;
615
613
}
616
614
617
- async fn mock_oidc_provider ( server : & MockServer , token_response : ResponseTemplate ) {
615
+ async fn mock_oauth_authorization_server (
616
+ server : & MockServer ,
617
+ token_response : ResponseTemplate ,
618
+ ) {
618
619
Mock :: given ( method ( "GET" ) )
619
620
. and ( path ( "/_matrix/client/unstable/org.matrix.msc2965/auth_issuer" ) )
620
621
. respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
621
622
"issuer" : server. uri( ) ,
622
623
623
624
} ) ) )
624
625
. expect ( 1 )
626
+ . named ( "auth_issuer" )
625
627
. mount ( server)
626
628
. await ;
627
629
628
630
Mock :: given ( method ( "GET" ) )
629
631
. and ( path ( "/.well-known/openid-configuration" ) )
630
632
. respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( open_id_configuration ( server) ) )
631
633
. expect ( 1 ..)
634
+ . named ( "server_metadata" )
632
635
. mount ( server)
633
636
. await ;
634
637
@@ -639,26 +642,29 @@ mod test {
639
642
"client_id_issued_at" : 1716375696
640
643
} ) ) )
641
644
. expect ( 1 )
645
+ . named ( "registration_endpoint" )
642
646
. mount ( server)
643
647
. await ;
644
648
645
649
Mock :: given ( method ( "GET" ) )
646
650
. and ( path ( "/oauth2/keys.json" ) )
647
651
. respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( keys_json ( ) ) )
648
- . expect ( 1 )
652
+ . named ( "jwks" )
649
653
. mount ( server)
650
654
. await ;
651
655
652
656
Mock :: given ( method ( "POST" ) )
653
657
. and ( path ( "/oauth2/device" ) )
654
658
. respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( device_code ( server) ) )
655
659
. expect ( 1 )
660
+ . named ( "device_authorization_endpoint" )
656
661
. mount ( server)
657
662
. await ;
658
663
659
664
Mock :: given ( method ( "POST" ) )
660
665
. and ( path ( "/oauth2/token" ) )
661
666
. respond_with ( token_response)
667
+ . named ( "token_endpoint" )
662
668
. mount ( server)
663
669
. await ;
664
670
}
@@ -669,7 +675,8 @@ mod test {
669
675
let rendezvous_server = MockedRendezvousServer :: new ( & server, "abcdEFG12345" ) . await ;
670
676
let ( sender, receiver) = tokio:: sync:: oneshot:: channel ( ) ;
671
677
672
- mock_oidc_provider ( & server, ResponseTemplate :: new ( 200 ) . set_body_json ( token ( ) ) ) . await ;
678
+ mock_oauth_authorization_server ( & server, ResponseTemplate :: new ( 200 ) . set_body_json ( token ( ) ) )
679
+ . await ;
673
680
674
681
Mock :: given ( method ( "GET" ) )
675
682
. and ( path ( "/_matrix/client/r0/account/whoami" ) )
@@ -764,7 +771,7 @@ mod test {
764
771
let rendezvous_server = MockedRendezvousServer :: new ( & server, "abcdEFG12345" ) . await ;
765
772
let ( sender, receiver) = tokio:: sync:: oneshot:: channel ( ) ;
766
773
767
- mock_oidc_provider ( & server, token_response) . await ;
774
+ mock_oauth_authorization_server ( & server, token_response) . await ;
768
775
769
776
Mock :: given ( method ( "GET" ) )
770
777
. and ( path ( "/_matrix/client/r0/account/whoami" ) )
@@ -830,7 +837,7 @@ mod test {
830
837
)
831
838
. await ;
832
839
833
- assert_let ! ( Err ( QRCodeLoginError :: Oidc ( e) ) = result) ;
840
+ assert_let ! ( Err ( QRCodeLoginError :: Oauth ( e) ) = result) ;
834
841
assert_eq ! (
835
842
e. as_request_token_error( ) ,
836
843
Some ( & DeviceCodeErrorResponseType :: AccessDenied ) ,
@@ -848,7 +855,7 @@ mod test {
848
855
)
849
856
. await ;
850
857
851
- assert_let ! ( Err ( QRCodeLoginError :: Oidc ( e) ) = result) ;
858
+ assert_let ! ( Err ( QRCodeLoginError :: Oauth ( e) ) = result) ;
852
859
assert_eq ! (
853
860
e. as_request_token_error( ) ,
854
861
Some ( & DeviceCodeErrorResponseType :: ExpiredToken ) ,
0 commit comments