Skip to content

Commit 65dbe14

Browse files
committed
refactor(oidc): move oidc_client to own module
1 parent e37ad11 commit 65dbe14

File tree

5 files changed

+103
-73
lines changed

5 files changed

+103
-73
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2025 The Matrix.org Foundation C.I.C.
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+
//! Common modules useful when using OIDC as the auththentication mechanism.
16+
17+
pub(crate) mod oidc_client;
18+
19+
use as_variant::as_variant;
20+
use openidconnect::core::CoreErrorResponseType;
21+
pub use openidconnect::{
22+
ConfigurationError, DeviceCodeErrorResponseType, DiscoveryError, HttpClientError,
23+
RequestTokenError, StandardErrorResponse,
24+
};
25+
use thiserror::Error;
26+
27+
use crate::{oidc, HttpError};
28+
29+
/// Error type describing failures in the interaction between the device
30+
/// attempting to log in and the OIDC provider.
31+
#[derive(Debug, Error)]
32+
pub enum DeviceAuhorizationOidcError {
33+
/// A generic OIDC error happened while we were attempting to register the
34+
/// device with the OIDC provider.
35+
#[error(transparent)]
36+
Oidc(#[from] oidc::OidcError),
37+
38+
/// The issuer URL failed to be parsed.
39+
#[error(transparent)]
40+
InvalidIssuerUrl(#[from] url::ParseError),
41+
42+
/// There was an error with our device configuration right before attempting
43+
/// to wait for the access token to be issued by the OIDC provider.
44+
#[error(transparent)]
45+
Configuration(#[from] ConfigurationError),
46+
47+
/// An error happened while we attempted to discover the authentication
48+
/// issuer URL.
49+
#[error(transparent)]
50+
AuthenticationIssuer(HttpError),
51+
52+
/// An error happened while we attempted to request a device authorization
53+
/// from the OIDC provider.
54+
#[error(transparent)]
55+
DeviceAuthorization(
56+
#[from]
57+
RequestTokenError<
58+
HttpClientError<reqwest::Error>,
59+
StandardErrorResponse<CoreErrorResponseType>,
60+
>,
61+
),
62+
63+
/// An error happened while waiting for the access token to be issued and
64+
/// sent to us by the OIDC provider.
65+
#[error(transparent)]
66+
RequestToken(
67+
#[from]
68+
RequestTokenError<
69+
HttpClientError<reqwest::Error>,
70+
StandardErrorResponse<DeviceCodeErrorResponseType>,
71+
>,
72+
),
73+
74+
/// An error happened during the discovery of the OIDC provider metadata.
75+
#[error(transparent)]
76+
Discovery(#[from] DiscoveryError<HttpClientError<reqwest::Error>>),
77+
}
78+
79+
impl DeviceAuhorizationOidcError {
80+
/// If the [`DeviceAuhorizationOidcError`] is of the
81+
/// [`DeviceCodeErrorResponseType`] error variant, return it.
82+
pub fn as_request_token_error(&self) -> Option<&DeviceCodeErrorResponseType> {
83+
let error = as_variant!(self, DeviceAuhorizationOidcError::RequestToken)?;
84+
let request_token_error = as_variant!(error, RequestTokenError::ServerResponse)?;
85+
86+
Some(request_token_error.error())
87+
}
88+
}

crates/matrix-sdk/src/authentication/qrcode/oidc_client.rs renamed to crates/matrix-sdk/src/authentication/common_oidc/oidc_client.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ pub type OidcClientInner<
8787
/// An OIDC specific HTTP client.
8888
///
8989
/// This is used to communicate with the OIDC provider exclusively.
90-
pub(super) struct OidcClient {
90+
#[derive(Debug)]
91+
pub(crate) struct OidcClient {
9192
inner: OidcClientInner,
9293
http_client: HttpClient,
9394
}
9495

9596
impl OidcClient {
96-
pub(super) async fn new(
97+
pub(crate) async fn new(
9798
client_id: String,
9899
issuer_url: String,
99100
http_client: HttpClient,
@@ -120,7 +121,7 @@ impl OidcClient {
120121
Ok(OidcClient { inner: oidc_client, http_client })
121122
}
122123

123-
pub(super) async fn request_device_authorization(
124+
pub(crate) async fn request_device_authorization(
124125
&self,
125126
device_id: Curve25519PublicKey,
126127
) -> Result<CoreDeviceAuthorizationResponse, DeviceAuhorizationOidcError> {
@@ -145,7 +146,7 @@ impl OidcClient {
145146
Ok(details)
146147
}
147148

148-
pub(super) async fn wait_for_tokens(
149+
pub(crate) async fn wait_for_tokens(
149150
&self,
150151
details: &CoreDeviceAuthorizationResponse,
151152
) -> Result<OidcSessionTokens, DeviceAuhorizationOidcError> {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ use crate::{
3030
Client, RefreshTokenError, SessionChange,
3131
};
3232

33+
#[cfg(feature = "experimental-oidc")]
34+
pub mod common_oidc;
35+
3336
#[cfg(all(feature = "experimental-oidc", feature = "e2e-encryption", not(target_arch = "wasm32")))]
3437
pub mod qrcode;
3538

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ use ruma::OwnedDeviceId;
2929
use tracing::trace;
3030
use vodozemac::ecies::CheckCode;
3131

32-
use super::{
33-
messages::LoginFailureReason, oidc_client::OidcClient, DeviceAuhorizationOidcError,
34-
SecureChannelError,
35-
};
32+
use super::{messages::LoginFailureReason, DeviceAuhorizationOidcError, SecureChannelError};
3633
#[cfg(doc)]
3734
use crate::oidc::Oidc;
3835
use crate::{
39-
authentication::qrcode::{
40-
messages::QrAuthMessage, secure_channel::EstablishedSecureChannel, QRCodeLoginError,
36+
authentication::{
37+
common_oidc::oidc_client::OidcClient,
38+
qrcode::{
39+
messages::QrAuthMessage, secure_channel::EstablishedSecureChannel, QRCodeLoginError,
40+
},
4141
},
4242
Client,
4343
};

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

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
//! QR code. To log in using a QR code, please take a look at the
2323
//! [`Oidc::login_with_qr_code()`] method
2424
25-
use as_variant::as_variant;
2625
use matrix_sdk_base::crypto::SecretImportError;
2726
pub use openidconnect::{
2827
core::CoreErrorResponseType, ConfigurationError, DeviceCodeErrorResponseType, DiscoveryError,
@@ -38,7 +37,6 @@ use crate::{oidc::CrossProcessRefreshLockError, HttpError};
3837

3938
mod login;
4039
mod messages;
41-
mod oidc_client;
4240
mod rendezvous_channel;
4341
mod secure_channel;
4442

@@ -50,6 +48,7 @@ pub use self::{
5048
login::{LoginProgress, LoginWithQrCode},
5149
messages::{LoginFailureReason, LoginProtocolType, QrAuthMessage},
5250
};
51+
use crate::authentication::common_oidc::DeviceAuhorizationOidcError;
5352

5453
/// The error type for failures while trying to log in a new device using a QR
5554
/// code.
@@ -106,67 +105,6 @@ pub enum QRCodeLoginError {
106105
SecretImport(#[from] SecretImportError),
107106
}
108107

109-
/// Error type describing failures in the interaction between the device
110-
/// attempting to log in and the OIDC provider.
111-
#[derive(Debug, Error)]
112-
pub enum DeviceAuhorizationOidcError {
113-
/// A generic OIDC error happened while we were attempting to register the
114-
/// device with the OIDC provider.
115-
#[error(transparent)]
116-
Oidc(#[from] crate::oidc::OidcError),
117-
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),
126-
127-
/// An error happened while we attempted to discover the authentication
128-
/// issuer URL.
129-
#[error(transparent)]
130-
AuthenticationIssuer(HttpError),
131-
132-
/// An error happened while we attempted to request a device authorization
133-
/// from the OIDC provider.
134-
#[error(transparent)]
135-
DeviceAuthorization(
136-
#[from]
137-
RequestTokenError<
138-
HttpClientError<reqwest::Error>,
139-
StandardErrorResponse<CoreErrorResponseType>,
140-
>,
141-
),
142-
143-
/// An error happened while waiting for the access token to be issued and
144-
/// sent to us by the OIDC provider.
145-
#[error(transparent)]
146-
RequestToken(
147-
#[from]
148-
RequestTokenError<
149-
HttpClientError<reqwest::Error>,
150-
StandardErrorResponse<DeviceCodeErrorResponseType>,
151-
>,
152-
),
153-
154-
/// An error happened during the discovery of the OIDC provider metadata.
155-
#[error(transparent)]
156-
Discovery(#[from] DiscoveryError<HttpClientError<reqwest::Error>>),
157-
}
158-
159-
impl DeviceAuhorizationOidcError {
160-
/// If the [`DeviceAuhorizationOidcError`] is of the
161-
/// [`DeviceCodeErrorResponseType`] error variant, return it.
162-
pub fn as_request_token_error(&self) -> Option<&DeviceCodeErrorResponseType> {
163-
let error = as_variant!(self, DeviceAuhorizationOidcError::RequestToken)?;
164-
let request_token_error = as_variant!(error, RequestTokenError::ServerResponse)?;
165-
166-
Some(request_token_error.error())
167-
}
168-
}
169-
170108
/// Error type for failures in when receiving or sending messages over the
171109
/// secure channel.
172110
#[derive(Debug, Error)]

0 commit comments

Comments
 (0)