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}
245
246impl PartialOrd for UpstreamOAuthProvider {
247    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
248        Some(self.id.cmp(&other.id))
249    }
250}
251
252impl Ord for UpstreamOAuthProvider {
253    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
254        self.id.cmp(&other.id)
255    }
256}
257
258impl UpstreamOAuthProvider {
259    /// Returns `true` if the provider is enabled
260    #[must_use]
261    pub const fn enabled(&self) -> bool {
262        self.disabled_at.is_none()
263    }
264}
265
266#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
267pub struct ClaimsImports {
268    #[serde(default)]
269    pub subject: SubjectPreference,
270
271    #[serde(default)]
272    pub localpart: ImportPreference,
273
274    #[serde(default)]
275    pub displayname: ImportPreference,
276
277    #[serde(default)]
278    pub email: ImportPreference,
279
280    #[serde(default)]
281    pub account_name: SubjectPreference,
282}
283
284// XXX: this should have another name
285#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
286pub struct SubjectPreference {
287    #[serde(default)]
288    pub template: Option<String>,
289}
290
291#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
292pub struct ImportPreference {
293    #[serde(default)]
294    pub action: ImportAction,
295
296    #[serde(default)]
297    pub template: Option<String>,
298}
299
300impl std::ops::Deref for ImportPreference {
301    type Target = ImportAction;
302
303    fn deref(&self) -> &Self::Target {
304        &self.action
305    }
306}
307
308#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
309#[serde(rename_all = "lowercase")]
310pub enum ImportAction {
311    /// Ignore the claim
312    #[default]
313    Ignore,
314
315    /// Suggest the claim value, but allow the user to change it
316    Suggest,
317
318    /// Force the claim value, but don't fail if it is missing
319    Force,
320
321    /// Force the claim value, and fail if it is missing
322    Require,
323}
324
325impl ImportAction {
326    #[must_use]
327    pub fn is_forced(&self) -> bool {
328        matches!(self, Self::Force | Self::Require)
329    }
330
331    #[must_use]
332    pub fn ignore(&self) -> bool {
333        matches!(self, Self::Ignore)
334    }
335
336    #[must_use]
337    pub fn is_required(&self) -> bool {
338        matches!(self, Self::Require)
339    }
340
341    #[must_use]
342    pub fn should_import(&self, user_preference: bool) -> bool {
343        match self {
344            Self::Ignore => false,
345            Self::Suggest => user_preference,
346            Self::Force | Self::Require => true,
347        }
348    }
349}