Skip to content

Commit 8883e08

Browse files
zecakehstefanceriu
authored andcommitted
refactor(oauth): Remove OAuthRegistrationStore
MSC2966 was updated, clients should re-register for every log in, so we don't need to store the client IDs between logins. Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
1 parent c4d9ec9 commit 8883e08

File tree

11 files changed

+134
-613
lines changed

11 files changed

+134
-613
lines changed

bindings/matrix-sdk-ffi/src/authentication.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use std::{
22
collections::HashMap,
33
fmt::{self, Debug},
4-
path::PathBuf,
54
sync::Arc,
65
};
76

87
use matrix_sdk::{
98
authentication::oauth::{
10-
error::{OAuthAuthorizationCodeError, OAuthRegistrationStoreError},
9+
error::OAuthAuthorizationCodeError,
1110
registration::{ApplicationType, ClientMetadata, Localized, OAuthGrantType},
12-
ClientId, OAuthError as SdkOAuthError, OAuthRegistrationStore,
11+
ClientId, ClientRegistrationData, OAuthError as SdkOAuthError,
1312
},
1413
Error,
1514
};
@@ -123,14 +122,12 @@ pub struct OidcConfiguration {
123122
/// An array of e-mail addresses of people responsible for this client.
124123
pub contacts: Option<Vec<String>>,
125124

126-
/// Pre-configured registrations for use with issuers that don't support
125+
/// Pre-configured registrations for use with homeservers that don't support
127126
/// dynamic client registration.
128-
pub static_registrations: HashMap<String, String>,
129-
130-
/// A file path where any dynamic registrations should be stored.
131127
///
132-
/// Suggested value: `{base_path}/oidc/registrations.json`
133-
pub dynamic_registrations_file: String,
128+
/// The keys of the map should be the URLs of the homeservers, but keys
129+
/// using `issuer` URLs are also supported.
130+
pub static_registrations: HashMap<String, String>,
134131
}
135132

136133
impl OidcConfiguration {
@@ -165,12 +162,10 @@ impl OidcConfiguration {
165162
Raw::new(&metadata).map_err(|_| OidcError::MetadataInvalid)
166163
}
167164

168-
pub async fn registrations(&self) -> Result<OAuthRegistrationStore, OidcError> {
165+
pub(crate) fn registration_data(&self) -> Result<ClientRegistrationData, OidcError> {
169166
let client_metadata = self.client_metadata()?;
170167

171-
let registrations_file = PathBuf::from(&self.dynamic_registrations_file);
172-
let mut registrations =
173-
OAuthRegistrationStore::new(registrations_file, client_metadata).await?;
168+
let mut registration_data = ClientRegistrationData::new(client_metadata);
174169

175170
if !self.static_registrations.is_empty() {
176171
let static_registrations = self
@@ -185,10 +180,10 @@ impl OidcConfiguration {
185180
})
186181
.collect();
187182

188-
registrations = registrations.with_static_registrations(static_registrations);
183+
registration_data.static_registrations = Some(static_registrations);
189184
}
190185

191-
Ok(registrations)
186+
Ok(registration_data)
192187
}
193188
}
194189

@@ -201,8 +196,6 @@ pub enum OidcError {
201196
NotSupported,
202197
#[error("Unable to use OIDC as the supplied client metadata is invalid.")]
203198
MetadataInvalid,
204-
#[error("Failed to use the supplied registrations file path.")]
205-
RegistrationsPathInvalid,
206199
#[error("The supplied callback URL used to complete OIDC is invalid.")]
207200
CallbackUrlInvalid,
208201
#[error("The OIDC login was cancelled by the user.")]
@@ -228,17 +221,6 @@ impl From<SdkOAuthError> for OidcError {
228221
}
229222
}
230223

231-
impl From<OAuthRegistrationStoreError> for OidcError {
232-
fn from(e: OAuthRegistrationStoreError) -> OidcError {
233-
match e {
234-
OAuthRegistrationStoreError::NotAFilePath | OAuthRegistrationStoreError::File(_) => {
235-
OidcError::RegistrationsPathInvalid
236-
}
237-
_ => OidcError::Generic { message: e.to_string() },
238-
}
239-
}
240-
}
241-
242224
impl From<Error> for OidcError {
243225
fn from(e: Error) -> OidcError {
244226
match e {

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,10 @@ impl Client {
408408
oidc_configuration: &OidcConfiguration,
409409
prompt: Option<OidcPrompt>,
410410
) -> Result<Arc<OAuthAuthorizationData>, OidcError> {
411-
let registrations = oidc_configuration.registrations().await?;
411+
let registration_data = oidc_configuration.registration_data()?;
412412
let redirect_uri = oidc_configuration.redirect_uri()?;
413413

414-
let mut url_builder = self.inner.oauth().login(registrations.into(), redirect_uri, None);
414+
let mut url_builder = self.inner.oauth().login(redirect_uri, None, Some(registration_data));
415415

416416
if let Some(prompt) = prompt {
417417
url_builder = url_builder.prompt(vec![prompt.into()]);

bindings/matrix-sdk-ffi/src/client_builder.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -755,13 +755,12 @@ impl ClientBuilder {
755755
}
756756
})?;
757757

758-
let registrations = oidc_configuration
759-
.registrations()
760-
.await
758+
let registration_data = oidc_configuration
759+
.registration_data()
761760
.map_err(|_| HumanQrLoginError::OidcMetadataInvalid)?;
762761

763762
let oauth = client.inner.oauth();
764-
let login = oauth.login_with_qr_code(&qr_code_data.inner, registrations.into());
763+
let login = oauth.login_with_qr_code(&qr_code_data.inner, Some(&registration_data));
765764

766765
let mut progress = login.subscribe_to_progress();
767766

crates/matrix-sdk/src/authentication/oauth/auth_code_builder.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use ruma::{
2424
use tracing::{info, instrument};
2525
use url::Url;
2626

27-
use super::{ClientRegistrationMethod, OAuth, OAuthError};
27+
use super::{ClientRegistrationData, OAuth, OAuthError};
2828
use crate::{authentication::oauth::AuthorizationValidationData, Result};
2929

3030
/// Builder type used to configure optional settings for authorization with an
@@ -34,7 +34,7 @@ use crate::{authentication::oauth::AuthorizationValidationData, Result};
3434
#[allow(missing_debug_implementations)]
3535
pub struct OAuthAuthCodeUrlBuilder {
3636
oauth: OAuth,
37-
registration_method: ClientRegistrationMethod,
37+
registration_data: Option<ClientRegistrationData>,
3838
scopes: Vec<Scope>,
3939
device_id: OwnedDeviceId,
4040
redirect_uri: Url,
@@ -45,14 +45,14 @@ pub struct OAuthAuthCodeUrlBuilder {
4545
impl OAuthAuthCodeUrlBuilder {
4646
pub(super) fn new(
4747
oauth: OAuth,
48-
registration_method: ClientRegistrationMethod,
4948
scopes: Vec<Scope>,
5049
device_id: OwnedDeviceId,
5150
redirect_uri: Url,
51+
registration_data: Option<ClientRegistrationData>,
5252
) -> Self {
5353
Self {
5454
oauth,
55-
registration_method,
55+
registration_data,
5656
scopes,
5757
device_id,
5858
redirect_uri,
@@ -93,19 +93,12 @@ impl OAuthAuthCodeUrlBuilder {
9393
/// request fails.
9494
#[instrument(target = "matrix_sdk::client", skip_all)]
9595
pub async fn build(self) -> Result<OAuthAuthorizationData, OAuthError> {
96-
let Self {
97-
oauth,
98-
registration_method,
99-
scopes,
100-
device_id,
101-
redirect_uri,
102-
prompt,
103-
login_hint,
104-
} = self;
96+
let Self { oauth, registration_data, scopes, device_id, redirect_uri, prompt, login_hint } =
97+
self;
10598

10699
let server_metadata = oauth.server_metadata().await?;
107100

108-
oauth.use_registration_method(&server_metadata, &registration_method).await?;
101+
oauth.use_registration_data(&server_metadata, registration_data.as_ref()).await?;
109102

110103
let data = oauth.data().expect("OAuth 2.0 data should be set after registration");
111104
info!(

crates/matrix-sdk/src/authentication/oauth/error.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ use ruma::{
3131

3232
#[cfg(feature = "e2e-encryption")]
3333
pub use super::cross_process::CrossProcessRefreshLockError;
34-
#[cfg(not(target_arch = "wasm32"))]
35-
pub use super::registration_store::OAuthRegistrationStoreError;
3634

3735
/// An error when interacting with the OAuth 2.0 authorization server.
3836
pub type OAuthRequestError<T> =
@@ -258,11 +256,6 @@ pub enum OAuthClientRegistrationError {
258256
/// Deserialization of the registration response failed.
259257
#[error("failed to deserialize registration response: {0}")]
260258
FromJson(serde_json::Error),
261-
262-
/// Failed to access or store the registration in the store.
263-
#[cfg(not(target_arch = "wasm32"))]
264-
#[error("failed to use registration store: {0}")]
265-
Store(#[from] OAuthRegistrationStoreError),
266259
}
267260

268261
/// Error response returned by server after requesting an authorization code.

0 commit comments

Comments
 (0)