From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 8DDDC6AF57 for ; Mon, 25 Jan 2021 14:43:44 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8BA52AF3B for ; Mon, 25 Jan 2021 14:43:44 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id A8E0BAF30 for ; Mon, 25 Jan 2021 14:43:43 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 7585C460E9 for ; Mon, 25 Jan 2021 14:43:43 +0100 (CET) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Mon, 25 Jan 2021 14:42:56 +0100 Message-Id: <20210125134302.3394328-12-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210125134302.3394328-1-f.gruenbichler@proxmox.com> References: <20210125134302.3394328-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.026 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup 11/15] authid: make Tokenname(Ref) derive Eq X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jan 2021 13:43:44 -0000 it's needed to derive Hash, and we always compare Authids or their Userid components, never just the Tokenname part anyway.. Signed-off-by: Fabian Grünbichler --- src/api2/types/userid.rs | 50 +++++----------------------------------- 1 file changed, 6 insertions(+), 44 deletions(-) diff --git a/src/api2/types/userid.rs b/src/api2/types/userid.rs index 20ce9370..7c73e69f 100644 --- a/src/api2/types/userid.rs +++ b/src/api2/types/userid.rs @@ -16,10 +16,10 @@ //! * [`Authid`]: an owned Authentication ID (a `Userid` with an optional `Tokenname`). //! Note that `Userid` and `Authid` do not have a separate borrowed type. //! -//! Note that `Username`s and `Tokenname`s are not unique, therefore they do not implement `Eq` and cannot be +//! Note that `Username`s are not unique, therefore they do not implement `Eq` and cannot be //! compared directly. If a direct comparison is really required, they can be compared as strings -//! via the `as_str()` method. [`Realm`]s, [`Userid`]s and [`Authid`]s on the other -//! hand can be compared with each other, as in those cases the comparison has meaning. +//! via the `as_str()` method. [`Realm`]s, [`Userid`]s and [`Authid`]s on the other hand can be +//! compared with each other, as in those cases the comparison has meaning. use std::borrow::Borrow; use std::convert::TryFrom; @@ -299,16 +299,8 @@ impl PartialEq for &RealmRef { )] /// The token ID part of an API token authentication id. /// -/// This alone does NOT uniquely identify the API token and therefore does not implement `Eq`. In -/// order to compare token IDs directly, they need to be explicitly compared as strings by calling -/// `.as_str()`. -/// -/// ```compile_fail -/// fn test(a: Tokenname, b: Tokenname) -> bool { -/// a == b // illegal and does not compile -/// } -/// ``` -#[derive(Clone, Debug, Hash, Deserialize, Serialize)] +/// This alone does NOT uniquely identify the API token - use a full `Authid` for such use cases. +#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)] pub struct Tokenname(String); /// A reference to a token name part of an authentication id. This alone does NOT uniquely identify @@ -336,24 +328,6 @@ pub struct TokennameRef(str); /// let b: &UsernameRef = unsafe { std::mem::zeroed() }; /// let _ = <&UsernameRef as PartialEq>::eq(&a, &b); /// ``` -/// -/// ```compile_fail -/// let a: Tokenname = unsafe { std::mem::zeroed() }; -/// let b: Tokenname = unsafe { std::mem::zeroed() }; -/// let _ = ::eq(&a, &b); -/// ``` -/// -/// ```compile_fail -/// let a: &TokennameRef = unsafe { std::mem::zeroed() }; -/// let b: &TokennameRef = unsafe { std::mem::zeroed() }; -/// let _ = <&TokennameRef as PartialEq>::eq(a, b); -/// ``` -/// -/// ```compile_fail -/// let a: &TokennameRef = unsafe { std::mem::zeroed() }; -/// let b: &TokennameRef = unsafe { std::mem::zeroed() }; -/// let _ = <&TokennameRef as PartialEq>::eq(&a, &b); -/// ``` struct _AssertNoEqImpl; impl TokennameRef { @@ -548,7 +522,7 @@ impl PartialEq for Userid { } /// A complete authentication id consisting of a user id and an optional token name. -#[derive(Clone, Debug, Hash)] +#[derive(Clone, Debug, Eq, PartialEq, Hash)] pub struct Authid { user: Userid, tokenname: Option @@ -590,18 +564,6 @@ lazy_static! { pub static ref ROOT_AUTHID: Authid = Authid::from(Userid::new("root@pam".to_string(), 4)); } -impl Eq for Authid {} - -impl PartialEq for Authid { - fn eq(&self, rhs: &Self) -> bool { - self.user == rhs.user && match (&self.tokenname, &rhs.tokenname) { - (Some(ours), Some(theirs)) => ours.as_str() == theirs.as_str(), - (None, None) => true, - _ => false, - } - } -} - impl From for Authid { fn from(parts: Userid) -> Self { Self::new(parts, None) -- 2.20.1