use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProviderType {
Oidc,
Oauth2,
}
impl ProviderType {
#[must_use]
pub fn as_str(&self) -> &'static str {
match self {
Self::Oidc => "oidc",
Self::Oauth2 => "oauth2",
}
}
#[must_use]
pub fn from_str_lenient(s: &str) -> Self {
match s {
"oauth2" => Self::Oauth2,
_ => Self::Oidc,
}
}
}
impl core::fmt::Display for ProviderType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderMapping {
pub email_field: String,
pub email_verified_field: String,
pub name_field: String,
pub subject_field: String,
pub username_field: Option<String>,
}
impl Default for ProviderMapping {
fn default() -> Self {
Self {
email_field: "email".into(),
email_verified_field: "email_verified".into(),
name_field: "name".into(),
subject_field: "sub".into(),
username_field: Some("preferred_username".into()),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpstreamProvider {
pub id: String,
pub name: String,
pub provider_type: ProviderType,
pub client_id: String,
pub client_secret_ciphertext: Option<Vec<u8>>,
pub encryption_key_id: String,
pub authorization_url: Option<String>,
pub token_url: Option<String>,
pub userinfo_url: Option<String>,
pub discovery_url: Option<String>,
pub scopes: Vec<String>,
pub is_active: bool,
pub mapping_config: ProviderMapping,
pub created_at: i64,
pub updated_at: i64,
}
impl UpstreamProvider {
#[must_use]
pub fn has_endpoints(&self) -> bool {
self.discovery_url.is_some()
|| (self.authorization_url.is_some() && self.token_url.is_some())
}
}
#[derive(Debug, Clone, Default)]
pub struct ProviderUpdate {
pub name: Option<String>,
pub provider_type: Option<ProviderType>,
pub client_id: Option<String>,
pub authorization_url: Option<Option<String>>,
pub token_url: Option<Option<String>>,
pub userinfo_url: Option<Option<String>>,
pub discovery_url: Option<Option<String>>,
pub scopes: Option<Vec<String>>,
pub is_active: Option<bool>,
pub mapping_config: Option<ProviderMapping>,
}