mas_data_model/upstream_oauth2/
provider.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7use chrono::{DateTime, Utc};
8use mas_iana::jose::JsonWebSignatureAlg;
9use oauth2_types::scope::Scope;
10use serde::{Deserialize, Serialize};
11use thiserror::Error;
12use ulid::Ulid;
13use url::Url;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
16#[serde(rename_all = "lowercase")]
17pub enum DiscoveryMode {
18    /// Use OIDC discovery to fetch and verify the provider metadata
19    #[default]
20    Oidc,
21
22    /// Use OIDC discovery to fetch the provider metadata, but don't verify it
23    Insecure,
24
25    /// Don't fetch the provider metadata
26    Disabled,
27}
28
29impl DiscoveryMode {
30    /// Returns `true` if discovery is disabled
31    #[must_use]
32    pub fn is_disabled(&self) -> bool {
33        matches!(self, DiscoveryMode::Disabled)
34    }
35}
36
37#[derive(Debug, Clone, Error)]
38#[error("Invalid discovery mode {0:?}")]
39pub struct InvalidDiscoveryModeError(String);
40
41impl std::str::FromStr for DiscoveryMode {
42    type Err = InvalidDiscoveryModeError;
43
44    fn from_str(s: &str) -> Result<Self, Self::Err> {
45        match s {
46            "oidc" => Ok(Self::Oidc),
47            "insecure" => Ok(Self::Insecure),
48            "disabled" => Ok(Self::Disabled),
49            s => Err(InvalidDiscoveryModeError(s.to_owned())),
50        }
51    }
52}
53
54impl DiscoveryMode {
55    #[must_use]
56    pub fn as_str(self) -> &'static str {
57        match self {
58            Self::Oidc => "oidc",
59            Self::Insecure => "insecure",
60            Self::Disabled => "disabled",
61        }
62    }
63}
64
65impl std::fmt::Display for DiscoveryMode {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        f.write_str(self.as_str())
68    }
69}
70
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
72#[serde(rename_all = "lowercase")]
73pub enum PkceMode {
74    /// Use PKCE if the provider supports it
75    #[default]
76    Auto,
77
78    /// Always use PKCE with the S256 method
79    S256,
80
81    /// Don't use PKCE
82    Disabled,
83}
84
85#[derive(Debug, Clone, Error)]
86#[error("Invalid PKCE mode {0:?}")]
87pub struct InvalidPkceModeError(String);
88
89impl std::str::FromStr for PkceMode {
90    type Err = InvalidPkceModeError;
91
92    fn from_str(s: &str) -> Result<Self, Self::Err> {
93        match s {
94            "auto" => Ok(Self::Auto),
95            "s256" => Ok(Self::S256),
96            "disabled" => Ok(Self::Disabled),
97            s => Err(InvalidPkceModeError(s.to_owned())),
98        }
99    }
100}
101
102impl PkceMode {
103    #[must_use]
104    pub fn as_str(self) -> &'static str {
105        match self {
106            Self::Auto => "auto",
107            Self::S256 => "s256",
108            Self::Disabled => "disabled",
109        }
110    }
111}
112
113impl std::fmt::Display for PkceMode {
114    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115        f.write_str(self.as_str())
116    }
117}
118
119#[derive(Debug, Clone, Error)]
120#[error("Invalid response mode {0:?}")]
121pub struct InvalidResponseModeError(String);
122
123#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
124#[serde(rename_all = "snake_case")]
125pub enum ResponseMode {
126    #[default]
127    Query,
128    FormPost,
129}
130
131impl From<ResponseMode> for oauth2_types::requests::ResponseMode {
132    fn from(value: ResponseMode) -> Self {
133        match value {
134            ResponseMode::Query => oauth2_types::requests::ResponseMode::Query,
135            ResponseMode::FormPost => oauth2_types::requests::ResponseMode::FormPost,
136        }
137    }
138}
139
140impl ResponseMode {
141    #[must_use]
142    pub fn as_str(self) -> &'static str {
143        match self {
144            Self::Query => "query",
145            Self::FormPost => "form_post",
146        }
147    }
148}
149
150impl std::fmt::Display for ResponseMode {
151    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
152        f.write_str(self.as_str())
153    }
154}
155
156impl std::str::FromStr for ResponseMode {
157    type Err = InvalidResponseModeError;
158
159    fn from_str(s: &str) -> Result<Self, Self::Err> {
160        match s {
161            "query" => Ok(ResponseMode::Query),
162            "form_post" => Ok(ResponseMode::FormPost),
163            s => Err(InvalidResponseModeError(s.to_owned())),
164        }
165    }
166}
167
168#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
169#[serde(rename_all = "snake_case")]
170pub enum TokenAuthMethod {
171    None,
172    ClientSecretBasic,
173    ClientSecretPost,
174    ClientSecretJwt,
175    PrivateKeyJwt,
176    SignInWithApple,
177}
178
179impl TokenAuthMethod {
180    #[must_use]
181    pub fn as_str(self) -> &'static str {
182        match self {
183            Self::None => "none",
184            Self::ClientSecretBasic => "client_secret_basic",
185            Self::ClientSecretPost => "client_secret_post",
186            Self::ClientSecretJwt => "client_secret_jwt",
187            Self::PrivateKeyJwt => "private_key_jwt",
188            Self::SignInWithApple => "sign_in_with_apple",
189        }
190    }
191}
192
193impl std::fmt::Display for TokenAuthMethod {
194    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
195        f.write_str(self.as_str())
196    }
197}
198
199impl std::str::FromStr for TokenAuthMethod {
200    type Err = InvalidUpstreamOAuth2TokenAuthMethod;
201
202    fn from_str(s: &str) -> Result<Self, Self::Err> {
203        match s {
204            "none" => Ok(Self::None),
205            "client_secret_post" => Ok(Self::ClientSecretPost),
206            "client_secret_basic" => Ok(Self::ClientSecretBasic),
207            "client_secret_jwt" => Ok(Self::ClientSecretJwt),
208            "private_key_jwt" => Ok(Self::PrivateKeyJwt),
209            "sign_in_with_apple" => Ok(Self::SignInWithApple),
210            s => Err(InvalidUpstreamOAuth2TokenAuthMethod(s.to_owned())),
211        }
212    }
213}
214
215#[derive(Debug, Clone, Error)]
216#[error("Invalid upstream OAuth 2.0 token auth method: {0}")]
217pub struct InvalidUpstreamOAuth2TokenAuthMethod(String);
218
219#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
220pub struct UpstreamOAuthProvider {
221    pub id: Ulid,
222    pub issuer: Option<String>,
223    pub human_name: Option<String>,
224    pub brand_name: Option<String>,
225    pub discovery_mode: DiscoveryMode,
226    pub pkce_mode: PkceMode,
227    pub jwks_uri_override: Option<Url>,
228    pub authorization_endpoint_override: Option<Url>,
229    pub scope: Scope,
230    pub token_endpoint_override: Option<Url>,
231    pub userinfo_endpoint_override: Option<Url>,
232    pub fetch_userinfo: bool,
233    pub userinfo_signed_response_alg: Option<JsonWebSignatureAlg>,
234    pub client_id: String,
235    pub encrypted_client_secret: Option<String>,
236    pub token_endpoint_signing_alg: Option<JsonWebSignatureAlg>,
237    pub token_endpoint_auth_method: TokenAuthMethod,
238    pub id_token_signed_response_alg: JsonWebSignatureAlg,
239    pub response_mode: Option<ResponseMode>,
240    pub created_at: DateTime<Utc>,
241    pub disabled_at: Option<DateTime<Utc>>,
242    pub claims_imports: ClaimsImports,
243    pub additional_authorization_parameters: Vec<(String, String)>,
244    pub forward_login_hint: bool,
245}
246
247impl PartialOrd for UpstreamOAuthProvider {
248    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
249        Some(self.id.cmp(&other.id))
250    }
251}
252
253impl Ord for UpstreamOAuthProvider {
254    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
255        self.id.cmp(&other.id)
256    }
257}
258
259impl UpstreamOAuthProvider {
260    /// Returns `true` if the provider is enabled
261    #[must_use]
262    pub const fn enabled(&self) -> bool {
263        self.disabled_at.is_none()
264    }
265}
266
267#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
268pub struct ClaimsImports {
269    #[serde(default)]
270    pub subject: SubjectPreference,
271
272    #[serde(default)]
273    pub localpart: ImportPreference,
274
275    #[serde(default)]
276    pub displayname: ImportPreference,
277
278    #[serde(default)]
279    pub email: ImportPreference,
280
281    #[serde(default)]
282    pub account_name: SubjectPreference,
283}
284
285// XXX: this should have another name
286#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
287pub struct SubjectPreference {
288    #[serde(default)]
289    pub template: Option<String>,
290}
291
292#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
293pub struct ImportPreference {
294    #[serde(default)]
295    pub action: ImportAction,
296
297    #[serde(default)]
298    pub template: Option<String>,
299}
300
301impl std::ops::Deref for ImportPreference {
302    type Target = ImportAction;
303
304    fn deref(&self) -> &Self::Target {
305        &self.action
306    }
307}
308
309#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
310#[serde(rename_all = "lowercase")]
311pub enum ImportAction {
312    /// Ignore the claim
313    #[default]
314    Ignore,
315
316    /// Suggest the claim value, but allow the user to change it
317    Suggest,
318
319    /// Force the claim value, but don't fail if it is missing
320    Force,
321
322    /// Force the claim value, and fail if it is missing
323    Require,
324}
325
326impl ImportAction {
327    #[must_use]
328    pub fn is_forced(&self) -> bool {
329        matches!(self, Self::Force | Self::Require)
330    }
331
332    #[must_use]
333    pub fn ignore(&self) -> bool {
334        matches!(self, Self::Ignore)
335    }
336
337    #[must_use]
338    pub fn is_required(&self) -> bool {
339        matches!(self, Self::Require)
340    }
341
342    #[must_use]
343    pub fn should_import(&self, user_preference: bool) -> bool {
344        match self {
345            Self::Ignore => false,
346            Self::Suggest => user_preference,
347            Self::Force | Self::Require => true,
348        }
349    }
350}