matrix_sdk/authentication/oauth/mod.rs
1// Copyright 2022 Kévin Commaille
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//! High-level OAuth 2.0 API.
16//!
17//! The OAuth 2.0 interactions with the Matrix API are currently a
18//! work-in-progress and are defined by [MSC3861] and its sub-proposals. And
19//! more documentation is available at [areweoidcyet.com].
20//!
21//! This authentication API is available with [`Client::oauth()`].
22//!
23//! # Homeserver support
24//!
25//! After building the client, you can check that the homeserver supports
26//! logging in via OAuth 2.0 when [`OAuth::server_metadata()`] succeeds.
27//!
28//! # Registration
29//!
30//! Clients must register with the homeserver before being able to interact with
31//! an OAuth 2.0 server.
32//!
33//! The registration consists in providing client metadata to the authorization
34//! server, to declare the interactions that the client supports with the
35//! homeserver. This step is important because the client cannot use a feature
36//! that is not declared during registration. In return, the server assigns an
37//! ID and eventually credentials to the client, which will allow to identify
38//! the client when authorization requests are made.
39//!
40//! Note that only public clients are supported by this API, i.e. clients
41//! without credentials.
42//!
43//! The registration step can be done automatically by providing a
44//! [`ClientRegistrationData`] to the login method.
45//!
46//! If the server supports dynamic registration, registration can be performed
47//! manually by using [`OAuth::register_client()`]. If dynamic registration is
48//! not available, the homeserver should document how to obtain a client ID. The
49//! client ID can then be provided with [`OAuth::restore_registered_client()`].
50//!
51//! # Login
52//!
53//! Currently, two login methods are supported by this API.
54//!
55//! ## Login with the Authorization Code flow
56//!
57//! The use of the Authorization Code flow is defined in [MSC2964] and [RFC
58//! 6749][rfc6749-auth-code].
59//!
60//! This method requires to open a URL in the end-user's browser where
61//! they will be able to log into their account in the server's web UI and grant
62//! access to their Matrix account.
63//!
64//! [`OAuth::login()`] constructs an [`OAuthAuthCodeUrlBuilder`] that can be
65//! configured, and then calling [`OAuthAuthCodeUrlBuilder::build()`] will
66//! provide the URL to present to the user in a web browser.
67//!
68//! After authenticating with the server, the user will be redirected to the
69//! provided redirect URI, with a code in the query that will allow to finish
70//! the login process by calling [`OAuth::finish_login()`].
71//!
72//! If the login needs to be cancelled before its completion,
73//! [`OAuth::abort_login()`] should be called to clean up the local data.
74//!
75//! ## Login by scanning a QR Code
76//!
77//! Logging in via a QR code is defined in [MSC4108]. It uses the Device
78//! authorization flow specified in [RFC 8628].
79//!
80//! This method requires to have another logged-in Matrix device that can
81//! display a QR Code.
82//!
83//! This login method is only available if the `e2e-encryption` cargo feature is
84//! enabled. It is not available on WASM.
85//!
86//! After scanning the QR Code, [`OAuth::login_with_qr_code()`] can be called
87//! with the QR Code's data. Then the different steps of the process need to be
88//! followed with [`LoginWithQrCode::subscribe_to_progress()`].
89//!
90//! A successful login using this method will automatically mark the device as
91//! verified and transfer all end-to-end encryption related secrets, like the
92//! private cross-signing keys and the backup key from the existing device to
93//! the new device.
94//!
95//! # Persisting/restoring a session
96//!
97//! The full session to persist can be obtained with [`OAuth::full_session()`].
98//! The different parts can also be retrieved with [`Client::session_meta()`],
99//! [`Client::session_tokens()`] and [`OAuth::client_id()`].
100//!
101//! To restore a previous session, use [`OAuth::restore_session()`].
102//!
103//! # Refresh tokens
104//!
105//! The use of refresh tokens with OAuth 2.0 servers is more common than in the
106//! Matrix specification. For this reason, it is recommended to configure the
107//! client with [`ClientBuilder::handle_refresh_tokens()`], to handle refreshing
108//! tokens automatically.
109//!
110//! Applications should then listen to session tokens changes after logging in
111//! with [`Client::subscribe_to_session_changes()`] to persist them on every
112//! change. If they are not persisted properly, the end-user will need to login
113//! again.
114//!
115//! # Unknown token error
116//!
117//! A request to the Matrix API can return an [`Error`] with an
118//! [`ErrorKind::UnknownToken`].
119//!
120//! The first step is to try to refresh the token with
121//! [`OAuth::refresh_access_token()`]. This step is done automatically if the
122//! client was built with [`ClientBuilder::handle_refresh_tokens()`].
123//!
124//! If refreshing the access token fails, the next step is to try to request a
125//! new login authorization with [`OAuth::login()`], using the device ID from
126//! the session.
127//!
128//! If this fails again, the client should assume to be logged out, and all
129//! local data should be erased.
130//!
131//! # Account management.
132//!
133//! The server might advertise a URL that allows the user to manage their
134//! account. It can be used to replace most of the Matrix APIs requiring
135//! User-Interactive Authentication.
136//!
137//! The account management URL is available as `account_management_uri` on
138//! [`AuthorizationServerMetadata`]. To build a full account management URL that
139//! includes the action that the user wants to perform, use
140//! [`AuthorizationServerMetadata::account_management_url_with_action()`].
141//!
142//! # Logout
143//!
144//! To log the [`Client`] out of the session, simply call [`OAuth::logout()`].
145//!
146//! # Examples
147//!
148//! Most methods have examples, there is also an example CLI application that
149//! supports all the actions described here, in [`examples/oauth_cli`].
150//!
151//! [MSC3861]: https://github.com/matrix-org/matrix-spec-proposals/pull/3861
152//! [areweoidcyet.com]: https://areweoidcyet.com/
153//! [MSC2964]: https://github.com/matrix-org/matrix-spec-proposals/pull/2964
154//! [rfc6749-auth-code]: https://datatracker.ietf.org/doc/html/rfc6749#section-4.1
155//! [MSC4108]: https://github.com/matrix-org/matrix-spec-proposals/pull/4108
156//! [RFC 8628]: https://datatracker.ietf.org/doc/html/rfc8628
157//! [`ClientBuilder::handle_refresh_tokens()`]: crate::ClientBuilder::handle_refresh_tokens()
158//! [`Error`]: ruma::api::error::Error
159//! [`ErrorKind::UnknownToken`]: ruma::api::error::ErrorKind::UnknownToken
160//! [`examples/oauth_cli`]: https://github.com/matrix-org/matrix-rust-sdk/tree/main/examples/oauth_cli
161
162#[cfg(feature = "e2e-encryption")]
163use std::sync::OnceLock;
164#[cfg(feature = "e2e-encryption")]
165use std::time::Duration;
166use std::{borrow::Cow, collections::HashMap, fmt, sync::Arc};
167
168use as_variant::as_variant;
169#[cfg(feature = "e2e-encryption")]
170use error::CrossProcessRefreshLockError;
171use error::{
172 OAuthAuthorizationCodeError, OAuthClientRegistrationError, OAuthDiscoveryError,
173 OAuthTokenRevocationError, RedirectUriQueryParseError,
174};
175#[cfg(feature = "e2e-encryption")]
176use matrix_sdk_base::crypto::types::qr_login::QrCodeData;
177use matrix_sdk_base::{SessionMeta, store::RoomLoadSettings, ttl::TtlValue};
178#[cfg(feature = "e2e-encryption")]
179use matrix_sdk_common::cross_process_lock::CrossProcessLockConfig;
180use oauth2::{
181 AccessToken, PkceCodeVerifier, RedirectUrl, RefreshToken, RevocationUrl, Scope,
182 StandardErrorResponse, StandardRevocableToken, TokenResponse, TokenUrl,
183 basic::BasicClient as OAuthClient,
184};
185pub use oauth2::{ClientId, CsrfToken};
186use oauth2_reqwest::ReqwestClient;
187use ruma::{
188 DeviceId, OwnedDeviceId,
189 api::client::discovery::get_authorization_server_metadata::{
190 self, v1::AuthorizationServerMetadata,
191 },
192 serde::Raw,
193};
194use serde::{Deserialize, Serialize};
195use sha2::Digest as _;
196use tokio::sync::Mutex;
197use tracing::{debug, error, instrument, trace, warn};
198use url::Url;
199
200mod auth_code_builder;
201#[cfg(feature = "e2e-encryption")]
202mod cross_process;
203pub mod error;
204mod http_client;
205#[cfg(feature = "e2e-encryption")]
206pub mod qrcode;
207pub mod registration;
208#[cfg(all(test, not(target_family = "wasm")))]
209mod tests;
210
211#[cfg(feature = "e2e-encryption")]
212use self::cross_process::{CrossProcessRefreshLockGuard, CrossProcessRefreshManager};
213#[cfg(feature = "e2e-encryption")]
214use self::qrcode::{
215 GrantLoginWithGeneratedQrCode, GrantLoginWithScannedQrCode, LoginWithGeneratedQrCode,
216 LoginWithQrCode,
217};
218pub use self::{
219 auth_code_builder::{OAuthAuthCodeUrlBuilder, OAuthAuthorizationData},
220 error::OAuthError,
221};
222use self::{
223 http_client::OAuthHttpClient,
224 registration::{ClientMetadata, ClientRegistrationResponse, register_client},
225};
226use super::{AuthData, SessionTokens};
227use crate::{
228 Client, HttpError, RefreshTokenError, Result,
229 client::{SessionChange, caches::CachedValue},
230 executor::spawn,
231 utils::UrlOrQuery,
232};
233
234pub(crate) struct OAuthCtx {
235 /// Lock and state when multiple processes may refresh an OAuth 2.0 session.
236 #[cfg(feature = "e2e-encryption")]
237 cross_process_token_refresh_manager: OnceLock<CrossProcessRefreshManager>,
238
239 /// Deferred cross-process lock initializer.
240 ///
241 /// Note: only required because we're using the crypto store that might not
242 /// be present before reloading a session.
243 #[cfg(feature = "e2e-encryption")]
244 deferred_cross_process_lock_init: Mutex<Option<String>>,
245
246 /// Whether to allow HTTP issuer URLs.
247 insecure_discover: bool,
248}
249
250impl OAuthCtx {
251 pub(crate) fn new(insecure_discover: bool) -> Self {
252 Self {
253 insecure_discover,
254 #[cfg(feature = "e2e-encryption")]
255 cross_process_token_refresh_manager: Default::default(),
256 #[cfg(feature = "e2e-encryption")]
257 deferred_cross_process_lock_init: Default::default(),
258 }
259 }
260}
261
262pub(crate) struct OAuthAuthData {
263 pub(crate) client_id: ClientId,
264 /// The data necessary to validate authorization responses.
265 authorization_data: Mutex<HashMap<CsrfToken, AuthorizationValidationData>>,
266}
267
268#[cfg(not(tarpaulin_include))]
269impl fmt::Debug for OAuthAuthData {
270 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
271 f.debug_struct("OAuthAuthData").finish_non_exhaustive()
272 }
273}
274
275/// A high-level authentication API to interact with an OAuth 2.0 authorization
276/// server.
277#[derive(Debug, Clone)]
278pub struct OAuth {
279 /// The underlying Matrix API client.
280 client: Client,
281 /// The HTTP client used for making OAuth 2.0 request.
282 http_client: OAuthHttpClient,
283}
284
285impl OAuth {
286 pub(crate) fn new(client: Client) -> Self {
287 let http_client = OAuthHttpClient {
288 inner: ReqwestClient::from(client.inner.http_client.inner.clone()),
289 #[cfg(test)]
290 insecure_rewrite_https_to_http: false,
291 };
292 Self { client, http_client }
293 }
294
295 /// Rewrite HTTPS requests to use HTTP instead.
296 ///
297 /// This is a workaround to bypass some checks that require an HTTPS URL,
298 /// but we can only mock HTTP URLs.
299 #[cfg(test)]
300 pub(crate) fn insecure_rewrite_https_to_http(mut self) -> Self {
301 self.http_client.insecure_rewrite_https_to_http = true;
302 self
303 }
304
305 fn ctx(&self) -> &OAuthCtx {
306 &self.client.auth_ctx().oauth
307 }
308
309 fn http_client(&self) -> &OAuthHttpClient {
310 &self.http_client
311 }
312
313 /// Enable a cross-process store lock on the state store, to coordinate
314 /// refreshes across different processes.
315 #[cfg(feature = "e2e-encryption")]
316 pub async fn enable_cross_process_refresh_lock(
317 &self,
318 lock_value: String,
319 ) -> Result<(), OAuthError> {
320 // FIXME: it must be deferred only because we're using the crypto store and it's
321 // initialized only in `set_or_reload_session`, not if we use a dedicated store.
322 let mut lock = self.ctx().deferred_cross_process_lock_init.lock().await;
323 if lock.is_some() {
324 return Err(CrossProcessRefreshLockError::DuplicatedLock.into());
325 }
326 *lock = Some(lock_value);
327
328 Ok(())
329 }
330
331 /// Performs a deferred cross-process refresh-lock, if needs be, after an
332 /// olm machine has been initialized.
333 ///
334 /// Must be called after [`BaseClient::set_or_reload_session`].
335 #[cfg(feature = "e2e-encryption")]
336 async fn deferred_enable_cross_process_refresh_lock(&self) {
337 let deferred_init_lock = self.ctx().deferred_cross_process_lock_init.lock().await;
338
339 // Don't `take()` the value, so that subsequent calls to
340 // `enable_cross_process_refresh_lock` will keep on failing if we've enabled the
341 // lock at least once.
342 let Some(lock_value) = deferred_init_lock.as_ref() else {
343 return;
344 };
345
346 // FIXME: We shouldn't be using the crypto store for that! see also https://github.com/matrix-org/matrix-rust-sdk/issues/2472
347 let olm_machine_lock = self.client.olm_machine().await;
348 let olm_machine =
349 olm_machine_lock.as_ref().expect("there has to be an olm machine, hopefully?");
350 let store = olm_machine.store();
351 let lock = store.create_store_lock(
352 "oidc_session_refresh_lock".to_owned(),
353 CrossProcessLockConfig::multi_process(lock_value.to_owned()),
354 );
355
356 let manager = CrossProcessRefreshManager::new(store.clone(), lock);
357
358 // This method is guarded with the `deferred_cross_process_lock_init` lock held,
359 // so this `set` can't be an error.
360 let _ = self.ctx().cross_process_token_refresh_manager.set(manager);
361 }
362
363 /// The OAuth 2.0 authentication data.
364 ///
365 /// Returns `None` if the client was not registered or if the registration
366 /// was not restored with [`OAuth::restore_registered_client()`] or
367 /// [`OAuth::restore_session()`].
368 fn data(&self) -> Option<&OAuthAuthData> {
369 let data = self.client.auth_ctx().auth_data.get()?;
370 as_variant!(data, AuthData::OAuth)
371 }
372
373 /// Log in this device using a QR code.
374 ///
375 /// # Arguments
376 ///
377 /// * `registration_data` - The data to restore or register the client with
378 /// the server. If this is not provided, an error will occur unless
379 /// [`OAuth::register_client()`] or [`OAuth::restore_registered_client()`]
380 /// was called previously.
381 #[cfg(feature = "e2e-encryption")]
382 pub fn login_with_qr_code<'a>(
383 &'a self,
384 registration_data: Option<&'a ClientRegistrationData>,
385 ) -> LoginWithQrCodeBuilder<'a> {
386 LoginWithQrCodeBuilder { client: &self.client, registration_data }
387 }
388
389 /// Grant login to a new device using a QR code.
390 #[cfg(feature = "e2e-encryption")]
391 pub fn grant_login_with_qr_code<'a>(&'a self) -> GrantLoginWithQrCodeBuilder<'a> {
392 GrantLoginWithQrCodeBuilder::new(&self.client)
393 }
394
395 /// Restore or register the OAuth 2.0 client for the server with the given
396 /// metadata, with the given optional [`ClientRegistrationData`].
397 ///
398 /// If we already have a client ID, this is a noop.
399 ///
400 /// Returns an error if there was a problem using the registration method.
401 async fn use_registration_data(
402 &self,
403 server_metadata: &AuthorizationServerMetadata,
404 data: Option<&ClientRegistrationData>,
405 ) -> std::result::Result<(), OAuthError> {
406 if self.client_id().is_some() {
407 tracing::info!("OAuth 2.0 is already configured.");
408 return Ok(());
409 }
410
411 let Some(data) = data else {
412 return Err(OAuthError::NotRegistered);
413 };
414
415 if let Some(static_registrations) = &data.static_registrations {
416 let client_id = static_registrations
417 .get(&self.client.homeserver())
418 .or_else(|| static_registrations.get(&server_metadata.issuer));
419
420 if let Some(client_id) = client_id {
421 self.restore_registered_client(client_id.clone());
422 return Ok(());
423 }
424 }
425
426 self.register_client_inner(server_metadata, &data.metadata).await?;
427
428 Ok(())
429 }
430
431 /// Get the cached OAuth 2.0 authorization server metadata of the
432 /// homeserver.
433 ///
434 /// This method will cache the metadata for a while. If the cache is not
435 /// populated it will request the server metadata, like a call to
436 /// [`OAuth::server_metadata()`], and cache the response before returning
437 /// it.
438 ///
439 /// The cache can be forced to be refreshed by calling
440 /// [`OAuth::server_metadata()`] instead.
441 ///
442 /// In most cases during the authentication process, it is better to always
443 /// fetch the metadata from the server. This is provided for convenience for
444 /// cases where the client doesn't want to incur the extra time necessary to
445 /// make the request.
446 ///
447 /// Returns an error if a problem occurred when fetching or validating the
448 /// metadata.
449 pub async fn cached_server_metadata(
450 &self,
451 ) -> Result<AuthorizationServerMetadata, OAuthDiscoveryError> {
452 let server_metadata_cache = &self.client.inner.caches.server_metadata;
453
454 if let CachedValue::Cached(metadata) = server_metadata_cache.value() {
455 if metadata.has_expired() {
456 debug!("spawning task to refresh OAuth 2.0 server metadata cache");
457
458 let oauth = self.clone();
459 self.client.task_monitor().spawn_finite_task(
460 "refresh OAuth 2.0 server metadata cache",
461 async move {
462 if let Err(error) = oauth.server_metadata().await {
463 warn!("failed to refresh OAuth 2.0 server metadata cache: {error}");
464 }
465 },
466 );
467 }
468
469 return Ok(metadata.into_data());
470 }
471
472 self.server_metadata().await
473 }
474
475 /// Fetch the OAuth 2.0 authorization server metadata of the homeserver.
476 ///
477 /// This will always request the latest server metadata from the homeserver.
478 ///
479 /// To avoid making a request each time, you can use
480 /// [`OAuth::cached_server_metadata()`].
481 ///
482 /// Returns an error if a problem occurred when fetching or validating the
483 /// metadata.
484 pub async fn server_metadata(
485 &self,
486 ) -> Result<AuthorizationServerMetadata, OAuthDiscoveryError> {
487 let server_metadata_cache = &self.client.inner.caches.server_metadata;
488
489 let mut server_metadata_guard = match server_metadata_cache.refresh_lock.try_lock() {
490 Ok(guard) => guard,
491 Err(_) => {
492 // There is already a refresh in progress, wait for it to finish.
493 let guard = server_metadata_cache.refresh_lock.lock().await;
494
495 // Reuse the data if the request was successful.
496 if matches!(*guard, Ok(()))
497 && let CachedValue::Cached(value) = server_metadata_cache.value()
498 {
499 return Ok(value.into_data());
500 }
501
502 // The previous request failed, make another request.
503 guard
504 }
505 };
506
507 match self.server_metadata_inner().await {
508 Ok(metadata) => {
509 // Always refresh the cache.
510 self.client.inner.caches.server_metadata.set_value(TtlValue::new(metadata.clone()));
511 *server_metadata_guard = Ok(());
512 Ok(metadata)
513 }
514 Err(error) => {
515 *server_metadata_guard = Err(());
516 Err(error)
517 }
518 }
519 }
520
521 async fn server_metadata_inner(
522 &self,
523 ) -> Result<AuthorizationServerMetadata, OAuthDiscoveryError> {
524 let is_endpoint_unsupported = |error: &HttpError| {
525 error
526 .as_client_api_error()
527 .is_some_and(|err| err.status_code == http::StatusCode::NOT_FOUND)
528 };
529
530 let response =
531 self.client.send(get_authorization_server_metadata::v1::Request::new()).await.map_err(
532 |error| {
533 // If the endpoint returns a 404, i.e. the server doesn't support the endpoint.
534 if is_endpoint_unsupported(&error) {
535 OAuthDiscoveryError::NotSupported
536 } else {
537 error.into()
538 }
539 },
540 )?;
541
542 let metadata = response.metadata.deserialize()?;
543
544 if self.ctx().insecure_discover {
545 metadata.insecure_validate_urls()?;
546 } else {
547 metadata.validate_urls()?;
548 }
549
550 Ok(metadata)
551 }
552
553 /// The OAuth 2.0 unique identifier of this client obtained after
554 /// registration.
555 ///
556 /// Returns `None` if the client was not registered or if the registration
557 /// was not restored with [`OAuth::restore_registered_client()`] or
558 /// [`OAuth::restore_session()`].
559 pub fn client_id(&self) -> Option<&ClientId> {
560 self.data().map(|data| &data.client_id)
561 }
562
563 /// The OAuth 2.0 user session of this client.
564 ///
565 /// Returns `None` if the client was not logged in.
566 pub fn user_session(&self) -> Option<UserSession> {
567 let meta = self.client.session_meta()?.to_owned();
568 let tokens = self.client.session_tokens()?;
569 Some(UserSession { meta, tokens })
570 }
571
572 /// The full OAuth 2.0 session of this client.
573 ///
574 /// Returns `None` if the client was not logged in with the OAuth 2.0 API.
575 pub fn full_session(&self) -> Option<OAuthSession> {
576 let user = self.user_session()?;
577 let data = self.data()?;
578 Some(OAuthSession { client_id: data.client_id.clone(), user })
579 }
580
581 /// Register a client with the OAuth 2.0 server.
582 ///
583 /// This should be called before any authorization request with an
584 /// authorization server that supports dynamic client registration. If the
585 /// client registered with the server manually, it should use
586 /// [`OAuth::restore_registered_client()`].
587 ///
588 /// Note that this method only supports public clients, i.e. clients without
589 /// a secret.
590 ///
591 /// # Arguments
592 ///
593 /// * `client_metadata` - The serialized client metadata to register.
594 ///
595 /// # Panic
596 ///
597 /// Panics if the authentication data was already set.
598 ///
599 /// # Example
600 ///
601 /// ```no_run
602 /// use matrix_sdk::{Client, ServerName};
603 /// # use matrix_sdk::authentication::oauth::ClientId;
604 /// # use matrix_sdk::authentication::oauth::registration::ClientMetadata;
605 /// # use ruma::serde::Raw;
606 /// # let client_metadata = unimplemented!();
607 /// # fn persist_client_registration (_: url::Url, _: &ClientId) {}
608 /// # _ = async {
609 /// let server_name = ServerName::parse("myhomeserver.org")?;
610 /// let client = Client::builder().server_name(&server_name).build().await?;
611 /// let oauth = client.oauth();
612 ///
613 /// if let Err(error) = oauth.server_metadata().await {
614 /// if error.is_not_supported() {
615 /// println!("OAuth 2.0 is not supported");
616 /// }
617 ///
618 /// return Err(error.into());
619 /// }
620 ///
621 /// let response = oauth
622 /// .register_client(&client_metadata)
623 /// .await?;
624 ///
625 /// println!(
626 /// "Registered with client_id: {}",
627 /// response.client_id.as_str()
628 /// );
629 ///
630 /// // The API only supports clients without secrets.
631 /// let client_id = response.client_id;
632 ///
633 /// persist_client_registration(client.homeserver(), &client_id);
634 /// # anyhow::Ok(()) };
635 /// ```
636 pub async fn register_client(
637 &self,
638 client_metadata: &Raw<ClientMetadata>,
639 ) -> Result<ClientRegistrationResponse, OAuthError> {
640 let server_metadata = self.server_metadata().await?;
641 Ok(self.register_client_inner(&server_metadata, client_metadata).await?)
642 }
643
644 async fn register_client_inner(
645 &self,
646 server_metadata: &AuthorizationServerMetadata,
647 client_metadata: &Raw<ClientMetadata>,
648 ) -> Result<ClientRegistrationResponse, OAuthClientRegistrationError> {
649 let registration_endpoint = server_metadata
650 .registration_endpoint
651 .as_ref()
652 .ok_or(OAuthClientRegistrationError::NotSupported)?;
653
654 let registration_response =
655 register_client(self.http_client(), registration_endpoint, client_metadata).await?;
656
657 // The format of the credentials changes according to the client metadata that
658 // was sent. Public clients only get a client ID.
659 self.restore_registered_client(registration_response.client_id.clone());
660
661 Ok(registration_response)
662 }
663
664 /// Set the data of a client that is registered with an OAuth 2.0
665 /// authorization server.
666 ///
667 /// This should be called when logging in with a server that is already
668 /// known by the client.
669 ///
670 /// Note that this method only supports public clients, i.e. clients with
671 /// no credentials.
672 ///
673 /// # Arguments
674 ///
675 /// * `client_id` - The unique identifier to authenticate the client with
676 /// the server, obtained after registration.
677 ///
678 /// # Panic
679 ///
680 /// Panics if authentication data was already set.
681 pub fn restore_registered_client(&self, client_id: ClientId) {
682 let data = OAuthAuthData { client_id, authorization_data: Default::default() };
683
684 self.client
685 .auth_ctx()
686 .auth_data
687 .set(AuthData::OAuth(data))
688 .expect("Client authentication data was already set");
689 }
690
691 /// Restore a previously logged in session.
692 ///
693 /// This can be used to restore the client to a logged in state, including
694 /// loading the sync state and the encryption keys from the store, if
695 /// one was set up.
696 ///
697 /// # Arguments
698 ///
699 /// * `session` - The session to restore.
700 /// * `room_load_settings` — Specify how many rooms must be restored; use
701 /// `::default()` if you don't know which value to pick.
702 ///
703 /// # Panic
704 ///
705 /// Panics if authentication data was already set.
706 pub async fn restore_session(
707 &self,
708 session: OAuthSession,
709 room_load_settings: RoomLoadSettings,
710 ) -> Result<()> {
711 let OAuthSession { client_id, user: UserSession { meta, tokens } } = session;
712
713 let data = OAuthAuthData { client_id, authorization_data: Default::default() };
714
715 self.client.auth_ctx().set_session_tokens(tokens.clone());
716 self.client
717 .base_client()
718 .activate(
719 meta,
720 room_load_settings,
721 #[cfg(feature = "e2e-encryption")]
722 None,
723 )
724 .await?;
725 #[cfg(feature = "e2e-encryption")]
726 self.deferred_enable_cross_process_refresh_lock().await;
727
728 self.client
729 .inner
730 .auth_ctx
731 .auth_data
732 .set(AuthData::OAuth(data))
733 .expect("Client authentication data was already set");
734
735 // Initialize the cross-process locking by saving our tokens' hash into the
736 // database, if we've enabled the cross-process lock.
737
738 #[cfg(feature = "e2e-encryption")]
739 if let Some(cross_process_lock) = self.ctx().cross_process_token_refresh_manager.get() {
740 cross_process_lock.restore_session(&tokens).await;
741
742 let mut guard = cross_process_lock
743 .spin_lock()
744 .await
745 .map_err(|err| crate::Error::OAuth(Box::new(err.into())))?;
746
747 // After we got the lock, it's possible that our session doesn't match the one
748 // read from the database, because of a race: another process has
749 // refreshed the tokens while we were waiting for the lock.
750 //
751 // In that case, if there's a mismatch, we reload the session and update the
752 // hash. Otherwise, we save our hash into the database.
753
754 if guard.hash_mismatch {
755 Box::pin(self.handle_session_hash_mismatch(&mut guard))
756 .await
757 .map_err(|err| crate::Error::OAuth(Box::new(err.into())))?;
758 } else {
759 guard
760 .save_in_memory_and_db(&tokens)
761 .await
762 .map_err(|err| crate::Error::OAuth(Box::new(err.into())))?;
763 // No need to call the save_session_callback here; it was the
764 // source of the session, so it's already in
765 // sync with what we had.
766 }
767 }
768
769 #[cfg(feature = "e2e-encryption")]
770 self.client.encryption().spawn_initialization_task(None).await;
771
772 Ok(())
773 }
774
775 #[cfg(feature = "e2e-encryption")]
776 async fn handle_session_hash_mismatch(
777 &self,
778 guard: &mut CrossProcessRefreshLockGuard,
779 ) -> Result<(), CrossProcessRefreshLockError> {
780 trace!("Handling hash mismatch.");
781
782 let callback = self
783 .client
784 .auth_ctx()
785 .reload_session_callback
786 .get()
787 .ok_or(CrossProcessRefreshLockError::MissingReloadSession)?;
788
789 match callback(self.client.clone()) {
790 Ok(tokens) => {
791 guard.handle_mismatch(&tokens).await?;
792
793 self.client.auth_ctx().set_session_tokens(tokens.clone());
794 // The app's callback acted as authoritative here, so we're not
795 // saving the data back into the app, as that would have no
796 // effect.
797 }
798 Err(err) => {
799 error!("when reloading OAuth 2.0 session tokens from callback: {err}");
800 }
801 }
802
803 Ok(())
804 }
805
806 /// The scopes to request for logging in and the corresponding device ID.
807 fn login_scopes(
808 device_id: Option<OwnedDeviceId>,
809 additional_scopes: Option<Vec<Scope>>,
810 ) -> (Vec<Scope>, OwnedDeviceId) {
811 /// Scope to grand full access to the client-server API.
812 const SCOPE_MATRIX_CLIENT_SERVER_API_FULL_ACCESS: &str =
813 "urn:matrix:org.matrix.msc2967.client:api:*";
814 /// Prefix of the scope to bind a device ID to an access token.
815 const SCOPE_MATRIX_DEVICE_ID_PREFIX: &str = "urn:matrix:org.matrix.msc2967.client:device:";
816
817 // Generate the device ID if it is not provided.
818 let device_id = device_id.unwrap_or_else(DeviceId::new);
819
820 let mut scopes = vec![
821 Scope::new(SCOPE_MATRIX_CLIENT_SERVER_API_FULL_ACCESS.to_owned()),
822 Scope::new(format!("{SCOPE_MATRIX_DEVICE_ID_PREFIX}{device_id}")),
823 ];
824
825 if let Some(extra_scopes) = additional_scopes {
826 scopes.extend(extra_scopes);
827 }
828
829 (scopes, device_id)
830 }
831
832 /// Log in via OAuth 2.0 with the Authorization Code flow.
833 ///
834 /// This method requires to open a URL in the end-user's browser where they
835 /// will be able to log into their account in the server's web UI and grant
836 /// access to their Matrix account.
837 ///
838 /// The [`OAuthAuthCodeUrlBuilder`] that is returned allows to customize a
839 /// few settings before calling `.build()` to obtain the URL to open in the
840 /// browser of the end-user.
841 ///
842 /// [`OAuth::finish_login()`] must be called once the user has been
843 /// redirected to the `redirect_uri`. [`OAuth::abort_login()`] should be
844 /// called instead if the authorization should be aborted before completion.
845 ///
846 /// # Arguments
847 ///
848 /// * `redirect_uri` - The URI where the end user will be redirected after
849 /// authorizing the login. It must be one of the redirect URIs sent in the
850 /// client metadata during registration.
851 ///
852 /// * `device_id` - The unique ID that will be associated with the session.
853 /// If not set, a random one will be generated. It can be an existing
854 /// device ID from a previous login call. Note that this should be done
855 /// only if the client also holds the corresponding encryption keys.
856 ///
857 /// * `registration_data` - The data to restore or register the client with
858 /// the server. If this is not provided, an error will occur unless
859 /// [`OAuth::register_client()`] or [`OAuth::restore_registered_client()`]
860 /// was called previously.
861 ///
862 /// * `additional_scopes` - Additional scopes to request from the
863 /// authorization server, e.g. "urn:matrix:client:com.example.msc9999.foo".
864 /// The scopes for API access and the device ID according to the
865 /// [specification](https://spec.matrix.org/v1.15/client-server-api/#allocated-scope-tokens)
866 /// are always requested.
867 ///
868 /// # Example
869 ///
870 /// ```no_run
871 /// use matrix_sdk::{
872 /// authentication::oauth::registration::ClientMetadata,
873 /// ruma::serde::Raw,
874 /// };
875 /// use url::Url;
876 /// # use matrix_sdk::Client;
877 /// # let client: Client = unimplemented!();
878 /// # let redirect_uri = unimplemented!();
879 /// # async fn open_uri_and_wait_for_redirect(uri: Url) -> Url { unimplemented!() };
880 /// # fn client_metadata() -> Raw<ClientMetadata> { unimplemented!() };
881 /// # _ = async {
882 /// let oauth = client.oauth();
883 /// let client_metadata: Raw<ClientMetadata> = client_metadata();
884 /// let registration_data = client_metadata.into();
885 ///
886 /// let auth_data = oauth.login(redirect_uri, None, Some(registration_data), None)
887 /// .build()
888 /// .await?;
889 ///
890 /// // Open auth_data.url and wait for response at the redirect URI.
891 /// let redirected_to_uri: Url = open_uri_and_wait_for_redirect(auth_data.url).await;
892 ///
893 /// oauth.finish_login(redirected_to_uri.into()).await?;
894 ///
895 /// // The session tokens can be persisted from the
896 /// // `OAuth::full_session()` method.
897 ///
898 /// // You can now make requests to the Matrix API.
899 /// let _me = client.whoami().await?;
900 /// # anyhow::Ok(()) }
901 /// ```
902 pub fn login(
903 &self,
904 redirect_uri: Url,
905 device_id: Option<OwnedDeviceId>,
906 registration_data: Option<ClientRegistrationData>,
907 additional_scopes: Option<Vec<Scope>>,
908 ) -> OAuthAuthCodeUrlBuilder {
909 let (scopes, device_id) = Self::login_scopes(device_id, additional_scopes);
910
911 OAuthAuthCodeUrlBuilder::new(
912 self.clone(),
913 scopes.to_vec(),
914 device_id,
915 redirect_uri,
916 registration_data,
917 )
918 }
919
920 /// Finish the login process.
921 ///
922 /// This method should be called after the URL returned by
923 /// [`OAuthAuthCodeUrlBuilder::build()`] has been presented and the user has
924 /// been redirected to the redirect URI after completing the authorization.
925 ///
926 /// If the authorization needs to be cancelled before its completion,
927 /// [`OAuth::abort_login()`] should be used instead to clean up the local
928 /// data.
929 ///
930 /// # Arguments
931 ///
932 /// * `url_or_query` - The URI where the user was redirected, or just its
933 /// query part.
934 ///
935 /// Returns an error if the authorization failed, if a request fails, or if
936 /// the client was already logged in with a different session.
937 pub async fn finish_login(&self, url_or_query: UrlOrQuery) -> Result<()> {
938 let response = AuthorizationResponse::parse_url_or_query(&url_or_query)
939 .map_err(|error| OAuthError::from(OAuthAuthorizationCodeError::from(error)))?;
940
941 let auth_code = match response {
942 AuthorizationResponse::Success(code) => code,
943 AuthorizationResponse::Error(error) => {
944 self.abort_login(&error.state).await;
945 return Err(OAuthError::from(OAuthAuthorizationCodeError::from(error.error)).into());
946 }
947 };
948
949 let device_id = self.finish_authorization(auth_code).await?;
950 self.load_session(device_id).await
951 }
952
953 /// Load the session after login.
954 ///
955 /// Returns an error if the request to get the user ID fails, or if the
956 /// client was already logged in with a different session.
957 pub(crate) async fn load_session(&self, device_id: OwnedDeviceId) -> Result<()> {
958 // Get the user ID.
959 let whoami_res = self.client.whoami().await.map_err(crate::Error::from)?;
960
961 let new_session = SessionMeta { user_id: whoami_res.user_id, device_id };
962
963 if let Some(current_session) = self.client.session_meta() {
964 if new_session != *current_session {
965 return Err(OAuthError::SessionMismatch.into());
966 }
967 } else {
968 self.client
969 .base_client()
970 .activate(
971 new_session,
972 RoomLoadSettings::default(),
973 #[cfg(feature = "e2e-encryption")]
974 None,
975 )
976 .await?;
977 // At this point the Olm machine has been set up.
978
979 // Enable the cross-process lock for refreshes, if needs be.
980 #[cfg(feature = "e2e-encryption")]
981 self.enable_cross_process_lock().await.map_err(OAuthError::from)?;
982
983 #[cfg(feature = "e2e-encryption")]
984 self.client.encryption().spawn_initialization_task(None).await;
985 }
986
987 Ok(())
988 }
989
990 #[cfg(feature = "e2e-encryption")]
991 pub(crate) async fn enable_cross_process_lock(
992 &self,
993 ) -> Result<(), CrossProcessRefreshLockError> {
994 // Enable the cross-process lock for refreshes, if needs be.
995 self.deferred_enable_cross_process_refresh_lock().await;
996
997 if let Some(cross_process_manager) = self.ctx().cross_process_token_refresh_manager.get()
998 && let Some(tokens) = self.client.session_tokens()
999 {
1000 let mut cross_process_guard = cross_process_manager.spin_lock().await?;
1001
1002 if cross_process_guard.hash_mismatch {
1003 // At this point, we're finishing a login while another process had written
1004 // something in the database. It's likely the information in the database is
1005 // just outdated and wasn't properly updated, but display a warning, just in
1006 // case this happens frequently.
1007 warn!("unexpected cross-process hash mismatch when finishing login (see comment)");
1008 }
1009
1010 cross_process_guard.save_in_memory_and_db(&tokens).await?;
1011 }
1012
1013 Ok(())
1014 }
1015
1016 /// Finish the authorization process.
1017 ///
1018 /// This method should be called after the URL returned by
1019 /// [`OAuthAuthCodeUrlBuilder::build()`] has been presented and the user has
1020 /// been redirected to the redirect URI after a successful authorization.
1021 ///
1022 /// # Arguments
1023 ///
1024 /// * `auth_code` - The response received as part of the redirect URI when
1025 /// the authorization was successful.
1026 ///
1027 /// Returns the device ID used in the authorized scope if it succeeds.
1028 /// Returns an error if a request fails.
1029 async fn finish_authorization(
1030 &self,
1031 auth_code: AuthorizationCode,
1032 ) -> Result<OwnedDeviceId, OAuthError> {
1033 let data = self.data().ok_or(OAuthError::NotAuthenticated)?;
1034 let client_id = data.client_id.clone();
1035
1036 let validation_data = data
1037 .authorization_data
1038 .lock()
1039 .await
1040 .remove(&auth_code.state)
1041 .ok_or(OAuthAuthorizationCodeError::InvalidState)?;
1042
1043 let token_uri = TokenUrl::from_url(validation_data.server_metadata.token_endpoint.clone());
1044
1045 let response = OAuthClient::new(client_id)
1046 .set_token_uri(token_uri)
1047 .exchange_code(oauth2::AuthorizationCode::new(auth_code.code))
1048 .set_pkce_verifier(validation_data.pkce_verifier)
1049 .set_redirect_uri(Cow::Owned(validation_data.redirect_uri))
1050 .request_async(self.http_client())
1051 .await
1052 .map_err(OAuthAuthorizationCodeError::RequestToken)?;
1053
1054 self.client.auth_ctx().set_session_tokens(SessionTokens {
1055 access_token: response.access_token().secret().clone(),
1056 refresh_token: response.refresh_token().map(RefreshToken::secret).cloned(),
1057 });
1058
1059 Ok(validation_data.device_id)
1060 }
1061
1062 /// Abort the login process.
1063 ///
1064 /// This method should be called if a login should be aborted before it is
1065 /// completed.
1066 ///
1067 /// If the login has been completed, [`OAuth::finish_login()`] should be
1068 /// used instead.
1069 ///
1070 /// # Arguments
1071 ///
1072 /// * `state` - The state provided in [`OAuthAuthorizationData`] after
1073 /// building the authorization URL.
1074 pub async fn abort_login(&self, state: &CsrfToken) {
1075 if let Some(data) = self.data() {
1076 data.authorization_data.lock().await.remove(state);
1077 }
1078 }
1079
1080 /// Request codes from the authorization server for logging in with another
1081 /// device.
1082 #[cfg(feature = "e2e-encryption")]
1083 async fn request_device_authorization(
1084 &self,
1085 server_metadata: &AuthorizationServerMetadata,
1086 device_id: Option<OwnedDeviceId>,
1087 ) -> Result<oauth2::StandardDeviceAuthorizationResponse, qrcode::DeviceAuthorizationOAuthError>
1088 {
1089 let (scopes, _) = Self::login_scopes(device_id, None);
1090
1091 let client_id = self.client_id().ok_or(OAuthError::NotRegistered)?.clone();
1092
1093 let device_authorization_url = server_metadata
1094 .device_authorization_endpoint
1095 .clone()
1096 .map(oauth2::DeviceAuthorizationUrl::from_url)
1097 .ok_or(qrcode::DeviceAuthorizationOAuthError::NoDeviceAuthorizationEndpoint)?;
1098
1099 let response = OAuthClient::new(client_id)
1100 .set_device_authorization_url(device_authorization_url)
1101 .exchange_device_code()
1102 .add_scopes(scopes)
1103 .request_async(self.http_client())
1104 .await?;
1105
1106 Ok(response)
1107 }
1108
1109 /// Exchange the device code against an access token.
1110 #[cfg(feature = "e2e-encryption")]
1111 async fn exchange_device_code(
1112 &self,
1113 server_metadata: &AuthorizationServerMetadata,
1114 device_authorization_response: &oauth2::StandardDeviceAuthorizationResponse,
1115 ) -> Result<(), qrcode::DeviceAuthorizationOAuthError> {
1116 use oauth2::TokenResponse;
1117
1118 let client_id = self.client_id().ok_or(OAuthError::NotRegistered)?.clone();
1119
1120 let token_uri = TokenUrl::from_url(server_metadata.token_endpoint.clone());
1121
1122 let response = OAuthClient::new(client_id)
1123 .set_token_uri(token_uri)
1124 .exchange_device_access_token(device_authorization_response)
1125 .request_async(self.http_client(), tokio::time::sleep, None)
1126 .await?;
1127
1128 self.client.auth_ctx().set_session_tokens(SessionTokens {
1129 access_token: response.access_token().secret().to_owned(),
1130 refresh_token: response.refresh_token().map(|t| t.secret().to_owned()),
1131 });
1132
1133 Ok(())
1134 }
1135
1136 async fn refresh_access_token_inner(
1137 self,
1138 refresh_token: String,
1139 token_endpoint: Url,
1140 client_id: ClientId,
1141 #[cfg(feature = "e2e-encryption")] cross_process_lock: Option<CrossProcessRefreshLockGuard>,
1142 ) -> Result<(), OAuthError> {
1143 trace!(
1144 "Token refresh: attempting to refresh with refresh_token {:x}",
1145 hash_str(&refresh_token)
1146 );
1147
1148 let token = RefreshToken::new(refresh_token.clone());
1149 let token_uri = TokenUrl::from_url(token_endpoint);
1150
1151 let response = OAuthClient::new(client_id)
1152 .set_token_uri(token_uri)
1153 .exchange_refresh_token(&token)
1154 .request_async(self.http_client())
1155 .await
1156 .map_err(OAuthError::RefreshToken)?;
1157
1158 let new_access_token = response.access_token().secret().clone();
1159 let new_refresh_token = response.refresh_token().map(RefreshToken::secret).cloned();
1160
1161 trace!(
1162 "Token refresh: new refresh_token: {} / access_token: {:x}",
1163 new_refresh_token
1164 .as_deref()
1165 .map(|token| format!("{:x}", hash_str(token)))
1166 .unwrap_or_else(|| "<none>".to_owned()),
1167 hash_str(&new_access_token)
1168 );
1169
1170 let tokens = SessionTokens {
1171 access_token: new_access_token,
1172 refresh_token: new_refresh_token.or(Some(refresh_token)),
1173 };
1174
1175 #[cfg(feature = "e2e-encryption")]
1176 let tokens_clone = tokens.clone();
1177
1178 self.client.auth_ctx().set_session_tokens(tokens);
1179
1180 // Call the save_session_callback if set, while the optional lock is being held.
1181 if let Some(save_session_callback) = self.client.auth_ctx().save_session_callback.get() {
1182 // Satisfies the save_session_callback invariant: set_session_tokens has
1183 // been called just above.
1184 tracing::debug!("call save_session_callback");
1185 if let Err(err) = save_session_callback(self.client.clone()) {
1186 error!("when saving session after refresh: {err}");
1187 }
1188 }
1189
1190 #[cfg(feature = "e2e-encryption")]
1191 if let Some(mut lock) = cross_process_lock {
1192 lock.save_in_memory_and_db(&tokens_clone).await?;
1193 }
1194
1195 tracing::debug!("broadcast session changed");
1196 _ = self.client.auth_ctx().session_change_sender.send(SessionChange::TokensRefreshed);
1197
1198 Ok(())
1199 }
1200
1201 /// Refresh the access token.
1202 ///
1203 /// This should be called when the access token has expired. It should not
1204 /// be needed to call this manually if the [`Client`] was constructed with
1205 /// [`ClientBuilder::handle_refresh_tokens()`].
1206 ///
1207 /// This method is protected behind a lock, so calling this method several
1208 /// times at once will only call the endpoint once and all subsequent calls
1209 /// will wait for the result of the first call.
1210 ///
1211 /// [`ClientBuilder::handle_refresh_tokens()`]: crate::ClientBuilder::handle_refresh_tokens()
1212 #[instrument(skip_all)]
1213 pub async fn refresh_access_token(&self) -> Result<(), RefreshTokenError> {
1214 macro_rules! fail {
1215 ($lock:expr, $err:expr) => {
1216 let error = $err;
1217 *$lock = Err(error.clone());
1218 return Err(error);
1219 };
1220 }
1221
1222 let client = &self.client;
1223
1224 let refresh_status_lock = client.auth_ctx().refresh_token_lock.clone().try_lock_owned();
1225
1226 let Ok(mut refresh_status_guard) = refresh_status_lock else {
1227 debug!("another refresh is happening, waiting for result.");
1228 // There's already a request to refresh happening in the same process. Wait for
1229 // it to finish.
1230 let res = client.auth_ctx().refresh_token_lock.lock().await.clone();
1231 debug!("other refresh is a {}", if res.is_ok() { "success" } else { "failure " });
1232 return res;
1233 };
1234
1235 debug!("no other refresh happening in background, starting.");
1236
1237 #[cfg(feature = "e2e-encryption")]
1238 let cross_process_guard =
1239 if let Some(manager) = self.ctx().cross_process_token_refresh_manager.get() {
1240 let mut cross_process_guard = match manager
1241 .spin_lock()
1242 .await
1243 .map_err(|err| RefreshTokenError::OAuth(Arc::new(err.into())))
1244 {
1245 Ok(guard) => guard,
1246 Err(err) => {
1247 warn!("couldn't acquire cross-process lock (timeout)");
1248 fail!(refresh_status_guard, err);
1249 }
1250 };
1251
1252 if cross_process_guard.hash_mismatch {
1253 Box::pin(self.handle_session_hash_mismatch(&mut cross_process_guard))
1254 .await
1255 .map_err(|err| RefreshTokenError::OAuth(Arc::new(err.into())))?;
1256 // Optimistic exit: assume that the underlying process did update fast enough.
1257 // In the worst case, we'll do another refresh Soon™.
1258 tracing::info!("other process handled refresh for us, assuming success");
1259 *refresh_status_guard = Ok(());
1260 return Ok(());
1261 }
1262
1263 Some(cross_process_guard)
1264 } else {
1265 None
1266 };
1267
1268 let Some(session_tokens) = self.client.session_tokens() else {
1269 warn!("invalid state: missing session tokens");
1270 fail!(refresh_status_guard, RefreshTokenError::RefreshTokenRequired);
1271 };
1272
1273 let Some(refresh_token) = session_tokens.refresh_token else {
1274 warn!("invalid state: missing session tokens");
1275 fail!(refresh_status_guard, RefreshTokenError::RefreshTokenRequired);
1276 };
1277
1278 let server_metadata = match self.server_metadata().await {
1279 Ok(metadata) => metadata,
1280 Err(err) => {
1281 warn!("couldn't get authorization server metadata: {err:?}");
1282 fail!(refresh_status_guard, RefreshTokenError::OAuth(Arc::new(err.into())));
1283 }
1284 };
1285
1286 let Some(client_id) = self.client_id().cloned() else {
1287 warn!("invalid state: missing client ID");
1288 fail!(
1289 refresh_status_guard,
1290 RefreshTokenError::OAuth(Arc::new(OAuthError::NotAuthenticated))
1291 );
1292 };
1293
1294 // Do not interrupt refresh access token requests and processing, by detaching
1295 // the request sending and response processing.
1296 // Make sure to keep the `refresh_status_guard` during the entire processing.
1297
1298 let this = self.clone();
1299
1300 spawn(async move {
1301 match this
1302 .refresh_access_token_inner(
1303 refresh_token,
1304 server_metadata.token_endpoint,
1305 client_id,
1306 #[cfg(feature = "e2e-encryption")]
1307 cross_process_guard,
1308 )
1309 .await
1310 {
1311 Ok(()) => {
1312 debug!("success refreshing a token");
1313 *refresh_status_guard = Ok(());
1314 Ok(())
1315 }
1316
1317 Err(err) => {
1318 let err = RefreshTokenError::OAuth(Arc::new(err));
1319 warn!("error refreshing an OAuth 2.0 token: {err}");
1320 fail!(refresh_status_guard, err);
1321 }
1322 }
1323 })
1324 .await
1325 .expect("joining")
1326 }
1327
1328 /// Log out from the currently authenticated session.
1329 pub async fn logout(&self) -> Result<(), OAuthError> {
1330 let client_id = self.client_id().ok_or(OAuthError::NotAuthenticated)?.clone();
1331
1332 let server_metadata = self.server_metadata().await?;
1333 let revocation_url = RevocationUrl::from_url(server_metadata.revocation_endpoint);
1334
1335 let tokens = self.client.session_tokens().ok_or(OAuthError::NotAuthenticated)?;
1336
1337 // Revoke the access token, it should revoke both tokens.
1338 OAuthClient::new(client_id)
1339 .set_revocation_url(revocation_url)
1340 .revoke_token(StandardRevocableToken::AccessToken(AccessToken::new(
1341 tokens.access_token,
1342 )))
1343 .map_err(OAuthTokenRevocationError::Url)?
1344 .request_async(self.http_client())
1345 .await
1346 .map_err(OAuthTokenRevocationError::Revoke)?;
1347
1348 #[cfg(feature = "e2e-encryption")]
1349 if let Some(manager) = self.ctx().cross_process_token_refresh_manager.get() {
1350 manager.on_logout().await?;
1351 }
1352
1353 Ok(())
1354 }
1355}
1356
1357/// Builder for QR login futures.
1358#[cfg(feature = "e2e-encryption")]
1359#[derive(Debug)]
1360pub struct LoginWithQrCodeBuilder<'a> {
1361 /// The underlying Matrix API client.
1362 client: &'a Client,
1363
1364 /// The data to restore or register the client with the server.
1365 registration_data: Option<&'a ClientRegistrationData>,
1366}
1367
1368#[cfg(feature = "e2e-encryption")]
1369impl<'a> LoginWithQrCodeBuilder<'a> {
1370 /// This method allows you to log in with a scanned QR code.
1371 ///
1372 /// The existing device needs to display the QR code which this device can
1373 /// scan and call this method to log in.
1374 ///
1375 /// A successful login using this method will automatically mark the device
1376 /// as verified and transfer all end-to-end encryption related secrets, like
1377 /// the private cross-signing keys and the backup key from the existing
1378 /// device to the new device.
1379 ///
1380 /// For the reverse flow where this device generates the QR code for the
1381 /// existing device to scan, use [`LoginWithQrCodeBuilder::generate`].
1382 ///
1383 /// # Arguments
1384 ///
1385 /// * `data` - The data scanned from a QR code.
1386 ///
1387 /// # Example
1388 ///
1389 /// ```no_run
1390 /// use anyhow::bail;
1391 /// use futures_util::StreamExt;
1392 /// use matrix_sdk::{
1393 /// authentication::oauth::{
1394 /// registration::ClientMetadata,
1395 /// qrcode::{LoginProgress, Msc4108IntentData, QrCodeData, QrCodeIntentData, QrProgress},
1396 /// },
1397 /// ruma::serde::Raw,
1398 /// Client,
1399 /// };
1400 /// # fn client_metadata() -> Raw<ClientMetadata> { unimplemented!() }
1401 /// # _ = async {
1402 /// # let bytes = unimplemented!();
1403 /// // You'll need to use a different library to scan and extract the raw bytes from the QR
1404 /// // code.
1405 /// let qr_code_data = QrCodeData::from_bytes(bytes)?;
1406 ///
1407 /// // Fetch the homeserver out of the parsed QR code data.
1408 /// let QrCodeIntentData::Msc4108 { data: Msc4108IntentData::Reciprocate { server_name }, ..} = qr_code_data.intent_data() else {
1409 /// bail!("The QR code is invalid, we did not receive a homeserver in the QR code.");
1410 /// };
1411 ///
1412 /// // Build the client as usual.
1413 /// let client = Client::builder()
1414 /// .server_name_or_homeserver_url(server_name)
1415 /// .handle_refresh_tokens()
1416 /// .build()
1417 /// .await?;
1418 ///
1419 /// let oauth = client.oauth();
1420 /// let client_metadata: Raw<ClientMetadata> = client_metadata();
1421 /// let registration_data = client_metadata.into();
1422 ///
1423 /// // Subscribing to the progress is necessary since we need to input the check
1424 /// // code on the existing device.
1425 /// let login = oauth.login_with_qr_code(Some(®istration_data)).scan(&qr_code_data);
1426 /// let mut progress = login.subscribe_to_progress();
1427 ///
1428 /// // Create a task which will show us the progress and tell us the check
1429 /// // code to input in the existing device.
1430 /// let task = tokio::spawn(async move {
1431 /// while let Some(state) = progress.next().await {
1432 /// match state {
1433 /// LoginProgress::Starting | LoginProgress::SyncingSecrets => (),
1434 /// LoginProgress::EstablishingSecureChannel(QrProgress { check_code }) => {
1435 /// let code = check_code.to_digit();
1436 /// println!("Please enter the following code into the other device {code:02}");
1437 /// },
1438 /// LoginProgress::WaitingForToken { user_code } => {
1439 /// println!("Please use your other device to confirm the log in {user_code}")
1440 /// },
1441 /// LoginProgress::Done => break,
1442 /// }
1443 /// }
1444 /// });
1445 ///
1446 /// // Now run the future to complete the login.
1447 /// login.await?;
1448 /// task.abort();
1449 ///
1450 /// println!("Successfully logged in: {:?} {:?}", client.user_id(), client.device_id());
1451 /// # anyhow::Ok(()) };
1452 /// ```
1453 pub fn scan(self, data: &'a QrCodeData) -> LoginWithQrCode<'a> {
1454 LoginWithQrCode::new(self.client, data, self.registration_data)
1455 }
1456
1457 /// This method allows you to log in by generating a QR code.
1458 ///
1459 /// This device needs to call this method to generate and display the
1460 /// QR code which the existing device can scan and grant the log in.
1461 ///
1462 /// A successful login using this method will automatically mark the device
1463 /// as verified and transfer all end-to-end encryption related secrets, like
1464 /// the private cross-signing keys and the backup key from the existing
1465 /// device to the new device.
1466 ///
1467 /// For the reverse flow where the existing device generates the QR code
1468 /// for this device to scan, use [`LoginWithQrCodeBuilder::scan`].
1469 ///
1470 /// # Example
1471 ///
1472 /// ```no_run
1473 /// use anyhow::bail;
1474 /// use futures_util::StreamExt;
1475 /// use matrix_sdk::{
1476 /// authentication::oauth::{
1477 /// registration::ClientMetadata,
1478 /// qrcode::{GeneratedQrProgress, LoginProgress, QrCodeData},
1479 /// },
1480 /// ruma::serde::Raw,
1481 /// Client,
1482 /// };
1483 /// use std::{error::Error, io::stdin};
1484 /// # fn client_metadata() -> Raw<ClientMetadata> { unimplemented!() }
1485 /// # _ = async {
1486 /// // Build the client as usual.
1487 /// let client = Client::builder()
1488 /// .server_name_or_homeserver_url("matrix.org")
1489 /// .handle_refresh_tokens()
1490 /// .build()
1491 /// .await?;
1492 ///
1493 /// let oauth = client.oauth();
1494 /// let client_metadata: Raw<ClientMetadata> = client_metadata();
1495 /// let registration_data = client_metadata.into();
1496 ///
1497 /// // Subscribing to the progress is necessary since we need to display the
1498 /// // QR code and prompt for the check code.
1499 /// let login = oauth.login_with_qr_code(Some(®istration_data)).generate();
1500 /// let mut progress = login.subscribe_to_progress();
1501 ///
1502 /// // Create a task which will show us the progress and allows us to display
1503 /// // the QR code and prompt for the check code.
1504 /// let task = tokio::spawn(async move {
1505 /// while let Some(state) = progress.next().await {
1506 /// match state {
1507 /// LoginProgress::Starting | LoginProgress::SyncingSecrets => (),
1508 /// LoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrReady(qr)) => {
1509 /// println!("Please use your other device to scan the QR code {:?}", qr)
1510 /// }
1511 /// LoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrScanned(cctx)) => {
1512 /// println!("Please enter the code displayed on your other device");
1513 /// let mut s = String::new();
1514 /// stdin().read_line(&mut s)?;
1515 /// let check_code = s.trim().parse::<u8>()?;
1516 /// cctx.send(check_code).await?
1517 /// }
1518 /// LoginProgress::WaitingForToken { user_code } => {
1519 /// println!("Please use your other device to confirm the log in {user_code}")
1520 /// },
1521 /// LoginProgress::Done => break,
1522 /// }
1523 /// }
1524 /// Ok::<(), Box<dyn Error + Send + Sync>>(())
1525 /// });
1526 ///
1527 /// // Now run the future to complete the login.
1528 /// login.await?;
1529 /// task.abort();
1530 ///
1531 /// println!("Successfully logged in: {:?} {:?}", client.user_id(), client.device_id());
1532 /// # anyhow::Ok(()) };
1533 /// ```
1534 pub fn generate(self) -> LoginWithGeneratedQrCode<'a> {
1535 LoginWithGeneratedQrCode::new(self.client, self.registration_data)
1536 }
1537}
1538
1539/// Builder for QR login grant handlers.
1540#[cfg(feature = "e2e-encryption")]
1541#[derive(Debug)]
1542pub struct GrantLoginWithQrCodeBuilder<'a> {
1543 /// The underlying Matrix API client.
1544 client: &'a Client,
1545 /// The duration to wait for the homeserver to create the new device after
1546 /// consenting the login before giving up.
1547 device_creation_timeout: Duration,
1548}
1549
1550#[cfg(feature = "e2e-encryption")]
1551impl<'a> GrantLoginWithQrCodeBuilder<'a> {
1552 /// Create a new builder with the default device creation timeout.
1553 fn new(client: &'a Client) -> Self {
1554 Self { client, device_creation_timeout: Duration::from_secs(10) }
1555 }
1556
1557 /// Set the device creation timeout.
1558 ///
1559 /// # Arguments
1560 ///
1561 /// * `device_creation_timeout` - The duration to wait for the homeserver to
1562 /// create the new device after consenting the login before giving up.
1563 pub fn device_creation_timeout(mut self, device_creation_timeout: Duration) -> Self {
1564 self.device_creation_timeout = device_creation_timeout;
1565 self
1566 }
1567
1568 /// This method allows you to grant login to a new device by scanning a
1569 /// QR code generated by the new device.
1570 ///
1571 /// The new device needs to display the QR code which this device can
1572 /// scan and call this method to grant the login.
1573 ///
1574 /// A successful login grant using this method will automatically mark the
1575 /// new device as verified and transfer all end-to-end encryption
1576 /// related secrets, like the private cross-signing keys and the backup
1577 /// key from this device device to the new device.
1578 ///
1579 /// For the reverse flow where this device generates the QR code
1580 /// for the new device to scan, use
1581 /// [`GrantLoginWithQrCodeBuilder::generate`].
1582 ///
1583 /// # Arguments
1584 ///
1585 /// * `data` - The data scanned from a QR code.
1586 ///
1587 /// # Example
1588 ///
1589 /// ```no_run
1590 /// use anyhow::bail;
1591 /// use futures_util::StreamExt;
1592 /// use matrix_sdk::{
1593 /// Client, authentication::oauth::{
1594 /// qrcode::{GrantLoginProgress, QrCodeData, QrProgress},
1595 /// }
1596 /// };
1597 /// use std::{error::Error, io::stdin};
1598 /// # _ = async {
1599 /// # let bytes = unimplemented!();
1600 /// // You'll need to use a different library to scan and extract the raw bytes from the QR
1601 /// // code.
1602 /// let qr_code_data = QrCodeData::from_bytes(bytes)?;
1603 ///
1604 /// // Build the client as usual.
1605 /// let client = Client::builder()
1606 /// .server_name_or_homeserver_url("matrix.org")
1607 /// .handle_refresh_tokens()
1608 /// .build()
1609 /// .await?;
1610 ///
1611 /// let oauth = client.oauth();
1612 ///
1613 /// // Subscribing to the progress is necessary to capture
1614 /// // the checkcode in order to display it to the other device and to obtain the verification URL to
1615 /// // open it in a browser so the user can consent to the new login.
1616 /// let mut grant = oauth.grant_login_with_qr_code().scan(&qr_code_data);
1617 /// let mut progress = grant.subscribe_to_progress();
1618 ///
1619 /// // Create a task which will show us the progress and allows us to receive
1620 /// // and feed back data.
1621 /// let task = tokio::spawn(async move {
1622 /// while let Some(state) = progress.next().await {
1623 /// match state {
1624 /// GrantLoginProgress::Starting | GrantLoginProgress::SyncingSecrets => (),
1625 /// GrantLoginProgress::EstablishingSecureChannel(QrProgress { check_code }) => {
1626 /// println!("Please enter the checkcode on your other device: {:?}", check_code);
1627 /// }
1628 /// GrantLoginProgress::WaitingForAuth { verification_uri } => {
1629 /// println!("Please open {verification_uri} to confirm the new login")
1630 /// },
1631 /// GrantLoginProgress::Done => break,
1632 /// }
1633 /// }
1634 /// Ok::<(), Box<dyn Error + Send + Sync>>(())
1635 /// });
1636 ///
1637 /// // Now run the future to grant the login.
1638 /// grant.await?;
1639 /// task.abort();
1640 ///
1641 /// println!("Successfully granted login");
1642 /// # anyhow::Ok(()) };
1643 /// ```
1644 pub fn scan(self, data: &'a QrCodeData) -> GrantLoginWithScannedQrCode<'a> {
1645 GrantLoginWithScannedQrCode::new(self.client, data, self.device_creation_timeout)
1646 }
1647
1648 /// This method allows you to grant login to a new device by generating a QR
1649 /// code on this device to be scanned by the new device.
1650 ///
1651 /// This device needs to call this method to generate and display the
1652 /// QR code which the new device can scan to initiate the grant process.
1653 ///
1654 /// A successful login grant using this method will automatically mark the
1655 /// new device as verified and transfer all end-to-end encryption
1656 /// related secrets, like the private cross-signing keys and the backup
1657 /// key from this device device to the new device.
1658 ///
1659 /// For the reverse flow where the new device generates the QR code
1660 /// for this device to scan, use [`GrantLoginWithQrCodeBuilder::scan`].
1661 ///
1662 /// # Example
1663 ///
1664 /// ```no_run
1665 /// use anyhow::bail;
1666 /// use futures_util::StreamExt;
1667 /// use matrix_sdk::{
1668 /// Client, authentication::oauth::{
1669 /// qrcode::{GeneratedQrProgress, GrantLoginProgress}
1670 /// }
1671 /// };
1672 /// use std::{error::Error, io::stdin};
1673 /// # _ = async {
1674 /// // Build the client as usual.
1675 /// let client = Client::builder()
1676 /// .server_name_or_homeserver_url("matrix.org")
1677 /// .handle_refresh_tokens()
1678 /// .build()
1679 /// .await?;
1680 ///
1681 /// let oauth = client.oauth();
1682 ///
1683 /// // Subscribing to the progress is necessary since we need to capture the
1684 /// // QR code, feed the checkcode back in and obtain the verification URL to
1685 /// // open it in a browser so the user can consent to the new login.
1686 /// let mut grant = oauth.grant_login_with_qr_code().generate();
1687 /// let mut progress = grant.subscribe_to_progress();
1688 ///
1689 /// // Create a task which will show us the progress and allows us to receive
1690 /// // and feed back data.
1691 /// let task = tokio::spawn(async move {
1692 /// while let Some(state) = progress.next().await {
1693 /// match state {
1694 /// GrantLoginProgress::Starting | GrantLoginProgress::SyncingSecrets => (),
1695 /// GrantLoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrReady(qr_code_data)) => {
1696 /// println!("Please scan the QR code on your other device: {:?}", qr_code_data);
1697 /// }
1698 /// GrantLoginProgress::EstablishingSecureChannel(GeneratedQrProgress::QrScanned(checkcode_sender)) => {
1699 /// println!("Please enter the code displayed on your other device");
1700 /// let mut s = String::new();
1701 /// stdin().read_line(&mut s)?;
1702 /// let check_code = s.trim().parse::<u8>()?;
1703 /// checkcode_sender.send(check_code).await?;
1704 /// }
1705 /// GrantLoginProgress::WaitingForAuth { verification_uri } => {
1706 /// println!("Please open {verification_uri} to confirm the new login")
1707 /// },
1708 /// GrantLoginProgress::Done => break,
1709 /// }
1710 /// }
1711 /// Ok::<(), Box<dyn Error + Send + Sync>>(())
1712 /// });
1713 ///
1714 /// // Now run the future to grant the login.
1715 /// grant.await?;
1716 /// task.abort();
1717 ///
1718 /// println!("Successfully granted login");
1719 /// # anyhow::Ok(()) };
1720 /// ```
1721 pub fn generate(self) -> GrantLoginWithGeneratedQrCode<'a> {
1722 GrantLoginWithGeneratedQrCode::new(self.client, self.device_creation_timeout)
1723 }
1724}
1725/// A full session for the OAuth 2.0 API.
1726#[derive(Debug, Clone)]
1727pub struct OAuthSession {
1728 /// The client ID obtained after registration.
1729 pub client_id: ClientId,
1730
1731 /// The user session.
1732 pub user: UserSession,
1733}
1734
1735/// A user session for the OAuth 2.0 API.
1736#[derive(Debug, Clone, Serialize, Deserialize)]
1737pub struct UserSession {
1738 /// The Matrix user session info.
1739 #[serde(flatten)]
1740 pub meta: SessionMeta,
1741
1742 /// The tokens used for authentication.
1743 #[serde(flatten)]
1744 pub tokens: SessionTokens,
1745}
1746
1747/// The data necessary to validate a response from the Token endpoint in the
1748/// Authorization Code flow.
1749#[derive(Debug)]
1750struct AuthorizationValidationData {
1751 /// The metadata of the server,
1752 server_metadata: AuthorizationServerMetadata,
1753
1754 /// The device ID used in the scope.
1755 device_id: OwnedDeviceId,
1756
1757 /// The URI where the end-user will be redirected after authorization.
1758 redirect_uri: RedirectUrl,
1759
1760 /// A string to correlate the authorization request to the token request.
1761 pkce_verifier: PkceCodeVerifier,
1762}
1763
1764/// The data returned by the server in the redirect URI after a successful
1765/// authorization.
1766#[derive(Debug, Clone)]
1767enum AuthorizationResponse {
1768 /// A successful response.
1769 Success(AuthorizationCode),
1770
1771 /// An error response.
1772 Error(AuthorizationError),
1773}
1774
1775impl AuthorizationResponse {
1776 /// Deserialize an `AuthorizationResponse` from a [`UrlOrQuery`].
1777 ///
1778 /// Returns an error if the URL or query doesn't have the expected format.
1779 fn parse_url_or_query(url_or_query: &UrlOrQuery) -> Result<Self, RedirectUriQueryParseError> {
1780 let query = url_or_query.query().ok_or(RedirectUriQueryParseError::MissingQuery)?;
1781 Self::parse_query(query)
1782 }
1783
1784 /// Deserialize an `AuthorizationResponse` from the query part of a URI.
1785 ///
1786 /// Returns an error if the query doesn't have the expected format.
1787 fn parse_query(query: &str) -> Result<Self, RedirectUriQueryParseError> {
1788 // For some reason deserializing the enum with `serde(untagged)` doesn't work,
1789 // so let's try both variants separately.
1790 if let Ok(code) = serde_html_form::from_str(query) {
1791 return Ok(AuthorizationResponse::Success(code));
1792 }
1793 if let Ok(error) = serde_html_form::from_str(query) {
1794 return Ok(AuthorizationResponse::Error(error));
1795 }
1796
1797 Err(RedirectUriQueryParseError::UnknownFormat)
1798 }
1799}
1800
1801/// The data returned by the server in the redirect URI after a successful
1802/// authorization.
1803#[derive(Debug, Clone, Deserialize)]
1804struct AuthorizationCode {
1805 /// The code to use to retrieve the access token.
1806 code: String,
1807 /// The unique identifier for this transaction.
1808 state: CsrfToken,
1809}
1810
1811/// The data returned by the server in the redirect URI after an authorization
1812/// error.
1813#[derive(Debug, Clone, Deserialize)]
1814struct AuthorizationError {
1815 /// The error.
1816 #[serde(flatten)]
1817 error: StandardErrorResponse<error::AuthorizationCodeErrorResponseType>,
1818 /// The unique identifier for this transaction.
1819 state: CsrfToken,
1820}
1821
1822fn hash_str(x: &str) -> impl fmt::LowerHex {
1823 sha2::Sha256::new().chain_update(x).finalize()
1824}
1825
1826/// Data to register or restore a client.
1827#[derive(Debug, Clone)]
1828pub struct ClientRegistrationData {
1829 /// The metadata to use to register the client when using dynamic client
1830 /// registration.
1831 pub metadata: Raw<ClientMetadata>,
1832
1833 /// Static registrations for servers that don't support dynamic registration
1834 /// but provide a client ID out-of-band.
1835 ///
1836 /// The keys of the map should be the URLs of the homeservers, but keys
1837 /// using `issuer` URLs are also supported.
1838 pub static_registrations: Option<HashMap<Url, ClientId>>,
1839}
1840
1841impl ClientRegistrationData {
1842 /// Construct a [`ClientRegistrationData`] with the given metadata and no
1843 /// static registrations.
1844 pub fn new(metadata: Raw<ClientMetadata>) -> Self {
1845 Self { metadata, static_registrations: None }
1846 }
1847}
1848
1849impl From<Raw<ClientMetadata>> for ClientRegistrationData {
1850 fn from(value: Raw<ClientMetadata>) -> Self {
1851 Self::new(value)
1852 }
1853}