all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Shannon Sterz <s.sterz@proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH proxmox 2/5] access: define shared `User`, `UserWithTokens` and `ApiTokens types
Date: Tue, 11 Jun 2024 14:51:46 +0200	[thread overview]
Message-ID: <zzvwz2ugjgrbhnoib6snawmzn7w7ezrdhi3htlouhdntyo4uk7@wfdh72xzlpe6> (raw)
In-Reply-To: <20240610154214.356689-3-s.sterz@proxmox.com>

On Mon, Jun 10, 2024 at 05:42:11PM GMT, Shannon Sterz wrote:
> +#[api(
> +    properties: {
> +        userid: {
> +            type: Userid,
> +        },
> +        comment: {
> +            optional: true,
> +            schema: COMMENT_SCHEMA,
> +        },
> +        enable: {
> +            optional: true,
> +            schema: ENABLE_USER_SCHEMA,
> +        },
> +        expire: {
> +            optional: true,
> +            schema: EXPIRE_USER_SCHEMA,
> +        },
> +        firstname: {
> +            optional: true,
> +            schema: FIRST_NAME_SCHEMA,
> +        },
> +        lastname: {
> +            schema: LAST_NAME_SCHEMA,
> +            optional: true,
> +         },
> +        email: {
> +            schema: EMAIL_SCHEMA,
> +            optional: true,
> +        },
> +        tokens: {
> +            type: Array,
> +            optional: true,
> +            description: "List of user's API tokens.",
> +            items: {
> +                type: ApiToken
> +            },
> +        },
> +        "totp-locked": {
> +            type: bool,
> +            optional: true,
> +            default: false,
> +            description: "True if the user is currently locked out of TOTP factors",
> +        },
> +        "tfa-locked-until": {
> +            optional: true,
> +            description: "Contains a timestamp until when a user is locked out of 2nd factors",
> +        },
> +    }
> +)]
> +#[derive(Serialize, Deserialize, Clone, PartialEq)]
> +#[serde(rename_all = "kebab-case")]
> +/// User properties with added list of ApiTokens
> +pub struct UserWithTokens {

While already moving things around, can we make this just contain a
nested `User` with `#[serde(flatten)]`?

> +    pub userid: Userid,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub comment: Option<String>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub enable: Option<bool>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub expire: Option<i64>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub firstname: Option<String>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub lastname: Option<String>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub email: Option<String>,
> +    #[serde(skip_serializing_if = "Vec::is_empty", default)]
> +    pub tokens: Vec<ApiToken>,
> +    #[serde(skip_serializing_if = "bool_is_false", default)]
> +    pub totp_locked: bool,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub tfa_locked_until: Option<i64>,
> +}
> +
> +fn bool_is_false(b: &bool) -> bool {
> +    !b
> +}
> +
> +#[api(
> +    properties: {
> +        tokenid: {
> +            schema: PROXMOX_TOKEN_ID_SCHEMA,
> +        },
> +        comment: {
> +            optional: true,
> +            schema: COMMENT_SCHEMA,
> +        },
> +        enable: {
> +            optional: true,
> +            schema: ENABLE_USER_SCHEMA,
> +        },
> +        expire: {
> +            optional: true,
> +            schema: EXPIRE_USER_SCHEMA,
> +        },
> +    }
> +)]
> +#[derive(Serialize, Deserialize, Clone, PartialEq)]
> +/// ApiToken properties.
> +pub struct ApiToken {
> +    pub tokenid: Authid,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub comment: Option<String>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub enable: Option<bool>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub expire: Option<i64>,
> +}
> +
> +impl ApiToken {
> +    pub fn is_active(&self) -> bool {
> +        if !self.enable.unwrap_or(true) {
> +            return false;
> +        }
> +        if let Some(expire) = self.expire {
> +            let now = proxmox_time::epoch_i64();
> +            if expire > 0 && expire <= now {
> +                return false;
> +            }
> +        }
> +        true
> +    }
> +}
> +
> +#[api(
> +    properties: {
> +        userid: {
> +            type: Userid,
> +        },
> +        comment: {
> +            optional: true,
> +            schema: COMMENT_SCHEMA,
> +        },
> +        enable: {
> +            optional: true,
> +            schema: ENABLE_USER_SCHEMA,
> +        },
> +        expire: {
> +            optional: true,
> +            schema: EXPIRE_USER_SCHEMA,
> +        },
> +        firstname: {
> +            optional: true,
> +            schema: FIRST_NAME_SCHEMA,
> +        },
> +        lastname: {
> +            schema: LAST_NAME_SCHEMA,
> +            optional: true,
> +         },
> +        email: {
> +            schema: EMAIL_SCHEMA,
> +            optional: true,
> +        },
> +    }
> +)]
> +#[derive(Serialize, Deserialize, Updater, PartialEq, Eq)]
> +/// User properties.
> +pub struct User {
> +    #[updater(skip)]
> +    pub userid: Userid,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub comment: Option<String>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub enable: Option<bool>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub expire: Option<i64>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub firstname: Option<String>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub lastname: Option<String>,
> +    #[serde(skip_serializing_if = "Option::is_none")]
> +    pub email: Option<String>,
> +}
> +
> +impl User {
> +    pub fn is_active(&self) -> bool {
> +        if !self.enable.unwrap_or(true) {
> +            return false;
> +        }
> +        if let Some(expire) = self.expire {
> +            let now = proxmox_time::epoch_i64();
> +            if expire > 0 && expire <= now {
> +                return false;
> +            }
> +        }
> +        true
> +    }
> +}
> -- 
> 2.39.2


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


  reply	other threads:[~2024-06-11 12:51 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-10 15:42 [pbs-devel] [PATCH proxmox 0/5] add proxmox-access crate Shannon Sterz
2024-06-10 15:42 ` [pbs-devel] [PATCH proxmox 1/5] access: add the proxmox-access crate to reuse acl trees Shannon Sterz
2024-06-11 12:53   ` Wolfgang Bumiller
2024-06-10 15:42 ` [pbs-devel] [PATCH proxmox 2/5] access: define shared `User`, `UserWithTokens` and `ApiTokens types Shannon Sterz
2024-06-11 12:51   ` Wolfgang Bumiller [this message]
2024-06-10 15:42 ` [pbs-devel] [PATCH proxmox 3/5] access: make token shadow implementation re-usable Shannon Sterz
2024-06-10 15:42 ` [pbs-devel] [PATCH proxmox 4/5] access: factor out user config and cache handling Shannon Sterz
2024-06-11 13:07   ` Wolfgang Bumiller
2024-06-11 14:30     ` Shannon Sterz
2024-06-12 12:49       ` Wolfgang Bumiller
2024-06-10 15:42 ` [pbs-devel] [PATCH proxmox 5/5] access: increment user cache generation when saving acl config Shannon Sterz
2024-06-11 17:28 ` [pbs-devel] [PATCH proxmox 0/5] add proxmox-access crate Thomas Lamprecht
2024-06-13 12:54 ` Shannon Sterz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=zzvwz2ugjgrbhnoib6snawmzn7w7ezrdhi3htlouhdntyo4uk7@wfdh72xzlpe6 \
    --to=w.bumiller@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    --cc=s.sterz@proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal