Skip to content

Commit d7dc1c9

Browse files
committed
refactor(auth-qrcode): Use oauth2 crate instead of openidconnect
The MSCs are now only based on OAuth 2.0, which is simpler than OpenID Connect. Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
1 parent 57919f5 commit d7dc1c9

File tree

6 files changed

+68
-200
lines changed

6 files changed

+68
-200
lines changed

Cargo.lock

Lines changed: 1 addition & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/matrix-sdk/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ experimental-oidc = [
5353
"dep:rand",
5454
"dep:sha2",
5555
"dep:tower",
56-
"dep:openidconnect",
56+
"dep:oauth2",
5757
]
5858
experimental-widgets = ["dep:language-tags", "dep:uuid"]
5959

@@ -129,7 +129,7 @@ tokio = { workspace = true, features = ["macros"] }
129129

130130
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
131131
backoff = { version = "0.4.0", features = ["tokio"] }
132-
openidconnect = { version = "4.0.0", optional = true }
132+
oauth2 = { version = "5.0.0", default-features = false, features = ["reqwest"], optional = true }
133133
# only activate reqwest's stream feature on non-wasm, the wasm part seems to not
134134
# support *sending* streams, which makes it useless for us.
135135
reqwest = { workspace = true, features = ["stream", "gzip", "http2"] }

crates/matrix-sdk/src/authentication/qrcode/login.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use matrix_sdk_base::{
2424
crypto::types::qr_login::{QrCodeData, QrCodeMode},
2525
SessionMeta,
2626
};
27-
use openidconnect::DeviceCodeErrorResponseType;
27+
use oauth2::DeviceCodeErrorResponseType;
2828
use ruma::OwnedDeviceId;
2929
use tracing::trace;
3030
use vodozemac::ecies::CheckCode;
@@ -291,38 +291,33 @@ impl<'a> LoginWithQrCode<'a> {
291291
}
292292

293293
async fn register_client(&self) -> Result<OidcClient, DeviceAuhorizationOidcError> {
294+
let oidc = self.client.oidc();
295+
294296
// Let's figure out the OIDC issuer, this fetches the info from the homeserver.
295-
let issuer = self
296-
.client
297-
.oidc()
297+
let issuer = oidc
298298
.fetch_authentication_issuer()
299299
.await
300300
.map_err(DeviceAuhorizationOidcError::AuthenticationIssuer)?;
301301

302302
// Now we register the client with the OIDC provider.
303303
let registration_response =
304-
self.client.oidc().register_client(&issuer, self.client_metadata.clone(), None).await?;
304+
oidc.register_client(&issuer, self.client_metadata.clone(), None).await?;
305305

306-
// Now we need to put the relevant data we got from the regustration response
306+
// Now we need to put the relevant data we got from the registration response
307307
// into the `Client`.
308308
// TODO: Why isn't `oidc().register_client()` doing this automatically?
309-
self.client.oidc().restore_registered_client(
309+
oidc.restore_registered_client(
310310
issuer.clone(),
311311
self.client_metadata.clone(),
312312
ClientCredentials::None { client_id: registration_response.client_id.clone() },
313313
);
314314

315-
// We're now switching to the openidconnect crate, it has a bit of a strange API
315+
// We're now switching to the oauth2 crate, it has a bit of a strange API
316316
// where you need to provide the HTTP client in every call you make.
317317
let http_client = self.client.inner.http_client.clone();
318+
let server_metadata = oidc.provider_metadata().await?;
318319

319-
OidcClient::new(
320-
registration_response.client_id,
321-
issuer,
322-
http_client,
323-
registration_response.client_secret.as_deref(),
324-
)
325-
.await
320+
OidcClient::new(registration_response.client_id, &server_metadata, http_client)
326321
}
327322
}
328323

@@ -633,13 +628,15 @@ mod test {
633628

634629
})))
635630
.expect(1)
631+
.named("auth_issuer")
636632
.mount(server)
637633
.await;
638634

639635
Mock::given(method("GET"))
640636
.and(path("/.well-known/openid-configuration"))
641637
.respond_with(ResponseTemplate::new(200).set_body_json(open_id_configuration(server)))
642638
.expect(1..)
639+
.named("server_metadata")
643640
.mount(server)
644641
.await;
645642

@@ -650,26 +647,29 @@ mod test {
650647
"client_id_issued_at": 1716375696
651648
})))
652649
.expect(1)
650+
.named("registration_endpoint")
653651
.mount(server)
654652
.await;
655653

656654
Mock::given(method("GET"))
657655
.and(path("/oauth2/keys.json"))
658656
.respond_with(ResponseTemplate::new(200).set_body_json(keys_json()))
659-
.expect(1)
657+
.named("jwks")
660658
.mount(server)
661659
.await;
662660

663661
Mock::given(method("POST"))
664662
.and(path("/oauth2/device"))
665663
.respond_with(ResponseTemplate::new(200).set_body_json(device_code(server)))
666664
.expect(1)
665+
.named("device_authorization_endpoint")
667666
.mount(server)
668667
.await;
669668

670669
Mock::given(method("POST"))
671670
.and(path("/oauth2/token"))
672671
.respond_with(token_response)
672+
.named("token_endpoint")
673673
.mount(server)
674674
.await;
675675
}

crates/matrix-sdk/src/authentication/qrcode/messages.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
use matrix_sdk_base::crypto::types::SecretsBundle;
1616
use matrix_sdk_common::deserialized_responses::PrivOwnedStr;
17-
use openidconnect::{
18-
core::CoreDeviceAuthorizationResponse, EndUserVerificationUrl, VerificationUriComplete,
17+
use oauth2::{
18+
EndUserVerificationUrl, StandardDeviceAuthorizationResponse, VerificationUriComplete,
1919
};
2020
use ruma::serde::StringEnum;
2121
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -107,8 +107,8 @@ impl QrAuthMessage {
107107
}
108108
}
109109

110-
impl From<&CoreDeviceAuthorizationResponse> for AuthorizationGrant {
111-
fn from(value: &CoreDeviceAuthorizationResponse) -> Self {
110+
impl From<&StandardDeviceAuthorizationResponse> for AuthorizationGrant {
111+
fn from(value: &StandardDeviceAuthorizationResponse) -> Self {
112112
Self {
113113
verification_uri: value.verification_uri().clone(),
114114
verification_uri_complete: value.verification_uri_complete().cloned(),

crates/matrix-sdk/src/authentication/qrcode/mod.rs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
2525
use as_variant::as_variant;
2626
use matrix_sdk_base::crypto::SecretImportError;
27-
pub use openidconnect::{
28-
core::CoreErrorResponseType, ConfigurationError, DeviceCodeErrorResponseType, DiscoveryError,
29-
HttpClientError, RequestTokenError, StandardErrorResponse,
27+
pub use oauth2::{
28+
basic::{BasicErrorResponse, BasicRequestTokenError},
29+
ConfigurationError, DeviceCodeErrorResponse, DeviceCodeErrorResponseType, HttpClientError,
30+
RequestTokenError, StandardErrorResponse,
3031
};
3132
use thiserror::Error;
3233
use url::Url;
@@ -115,14 +116,9 @@ pub enum DeviceAuhorizationOidcError {
115116
#[error(transparent)]
116117
Oidc(#[from] crate::authentication::oidc::OidcError),
117118

118-
/// The issuer URL failed to be parsed.
119-
#[error(transparent)]
120-
InvalidIssuerUrl(#[from] url::ParseError),
121-
122-
/// There was an error with our device configuration right before attempting
123-
/// to wait for the access token to be issued by the OIDC provider.
124-
#[error(transparent)]
125-
Configuration(#[from] ConfigurationError),
119+
/// The OAuth 2.0 server doesn't support the device authorization grant.
120+
#[error("OAuth 2.0 server doesn't support the device authorization grant")]
121+
NoDeviceAuthorizationEndpoint,
126122

127123
/// An error happened while we attempted to discover the authentication
128124
/// issuer URL.
@@ -132,28 +128,14 @@ pub enum DeviceAuhorizationOidcError {
132128
/// An error happened while we attempted to request a device authorization
133129
/// from the OIDC provider.
134130
#[error(transparent)]
135-
DeviceAuthorization(
136-
#[from]
137-
RequestTokenError<
138-
HttpClientError<reqwest::Error>,
139-
StandardErrorResponse<CoreErrorResponseType>,
140-
>,
141-
),
131+
DeviceAuthorization(#[from] BasicRequestTokenError<HttpClientError<reqwest::Error>>),
142132

143133
/// An error happened while waiting for the access token to be issued and
144134
/// sent to us by the OIDC provider.
145135
#[error(transparent)]
146136
RequestToken(
147-
#[from]
148-
RequestTokenError<
149-
HttpClientError<reqwest::Error>,
150-
StandardErrorResponse<DeviceCodeErrorResponseType>,
151-
>,
137+
#[from] RequestTokenError<HttpClientError<reqwest::Error>, DeviceCodeErrorResponse>,
152138
),
153-
154-
/// An error happened during the discovery of the OIDC provider metadata.
155-
#[error(transparent)]
156-
Discovery(#[from] DiscoveryError<HttpClientError<reqwest::Error>>),
157139
}
158140

159141
impl DeviceAuhorizationOidcError {

0 commit comments

Comments
 (0)