openid cleanup: derive and use Updater

This commit is contained in:
Dietmar Maurer 2021-08-11 13:22:45 +02:00
parent 80f950c05d
commit e4a5c072b4
2 changed files with 17 additions and 47 deletions

View File

@ -6,7 +6,7 @@ use ::serde::{Deserialize, Serialize};
use proxmox::api::{api, Permission, Router, RpcEnvironment}; use proxmox::api::{api, Permission, Router, RpcEnvironment};
use crate::config::domains::{self, OpenIdRealmConfig}; use crate::config::domains::{self, OpenIdRealmConfig, OpenIdRealmConfigUpdater};
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_REALM_ALLOCATE}; use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_REALM_ALLOCATE};
use crate::api2::types::*; use crate::api2::types::*;
@ -164,29 +164,9 @@ pub enum DeletableProperty {
realm: { realm: {
schema: REALM_ID_SCHEMA, schema: REALM_ID_SCHEMA,
}, },
"issuer-url": { update: {
description: "OpenID Issuer Url", type: OpenIdRealmConfigUpdater,
type: String, flatten: true,
optional: true,
},
"client-id": {
description: "OpenID Client ID",
type: String,
optional: true,
},
"client-key": {
description: "OpenID Client Key",
type: String,
optional: true,
},
autocreate: {
description: "Automatically create users if they do not exist.",
optional: true,
type: bool,
},
comment: {
schema: SINGLE_LINE_COMMENT_SCHEMA,
optional: true,
}, },
delete: { delete: {
description: "List of properties to delete.", description: "List of properties to delete.",
@ -210,11 +190,7 @@ pub enum DeletableProperty {
/// Update an OpenID realm configuration /// Update an OpenID realm configuration
pub fn update_openid_realm( pub fn update_openid_realm(
realm: String, realm: String,
issuer_url: Option<String>, update: OpenIdRealmConfigUpdater,
client_id: Option<String>,
client_key: Option<String>,
autocreate: Option<bool>,
comment: Option<String>,
delete: Option<Vec<DeletableProperty>>, delete: Option<Vec<DeletableProperty>>,
digest: Option<String>, digest: Option<String>,
_rpcenv: &mut dyn RpcEnvironment, _rpcenv: &mut dyn RpcEnvironment,
@ -241,7 +217,7 @@ pub fn update_openid_realm(
} }
} }
if let Some(comment) = comment { if let Some(comment) = update.comment {
let comment = comment.trim().to_string(); let comment = comment.trim().to_string();
if comment.is_empty() { if comment.is_empty() {
config.comment = None; config.comment = None;
@ -250,11 +226,11 @@ pub fn update_openid_realm(
} }
} }
if let Some(issuer_url) = issuer_url { config.issuer_url = issuer_url; } if let Some(issuer_url) = update.issuer_url { config.issuer_url = issuer_url; }
if let Some(client_id) = client_id { config.client_id = client_id; } if let Some(client_id) = update.client_id { config.client_id = client_id; }
if client_key.is_some() { config.client_key = client_key; } if update.client_key.is_some() { config.client_key = update.client_key; }
if autocreate.is_some() { config.autocreate = autocreate; } if update.autocreate.is_some() { config.autocreate = update.autocreate; }
domains.set_data(&realm, "openid", &config)?; domains.set_data(&realm, "openid", &config)?;

View File

@ -42,17 +42,7 @@ pub enum OpenIdUserAttribute {
realm: { realm: {
schema: REALM_ID_SCHEMA, schema: REALM_ID_SCHEMA,
}, },
"issuer-url": {
description: "OpenID Issuer Url",
type: String,
},
"client-id": {
description: "OpenID Client ID",
type: String,
},
"client-key": { "client-key": {
description: "OpenID Client Key",
type: String,
optional: true, optional: true,
}, },
comment: { comment: {
@ -60,9 +50,7 @@ pub enum OpenIdUserAttribute {
schema: SINGLE_LINE_COMMENT_SCHEMA, schema: SINGLE_LINE_COMMENT_SCHEMA,
}, },
autocreate: { autocreate: {
description: "Automatically create users if they do not exist.",
optional: true, optional: true,
type: bool,
default: false, default: false,
}, },
"username-claim": { "username-claim": {
@ -71,19 +59,25 @@ pub enum OpenIdUserAttribute {
}, },
}, },
)] )]
#[derive(Serialize,Deserialize)] #[derive(Serialize,Deserialize,Updater)]
#[serde(rename_all="kebab-case")] #[serde(rename_all="kebab-case")]
/// OpenID configuration properties. /// OpenID configuration properties.
pub struct OpenIdRealmConfig { pub struct OpenIdRealmConfig {
#[updater(skip)]
pub realm: String, pub realm: String,
/// OpenID Issuer Url
pub issuer_url: String, pub issuer_url: String,
/// OpenID Client ID
pub client_id: String, pub client_id: String,
/// OpenID Client Key
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub client_key: Option<String>, pub client_key: Option<String>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub comment: Option<String>, pub comment: Option<String>,
/// Automatically create users if they do not exist.
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub autocreate: Option<bool>, pub autocreate: Option<bool>,
#[updater(skip)]
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub username_claim: Option<OpenIdUserAttribute>, pub username_claim: Option<OpenIdUserAttribute>,
} }