From a4b6c357843065dce3e80246e98f2f9a3df78c54 Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Mon, 11 Nov 2024 08:27:38 +0200 Subject: [PATCH] Check if password exists with different name but same properties --- nc_password_client/nc_password_client.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/nc_password_client/nc_password_client.py b/nc_password_client/nc_password_client.py index bc8c4f0..77754bd 100755 --- a/nc_password_client/nc_password_client.py +++ b/nc_password_client/nc_password_client.py @@ -554,15 +554,18 @@ class NextcloudHandler: else: self.exit.status_code_error(r.status_code) - def exists_password(self, name): + def exists_password(self, obj): + '''Test if a password exist with the same name''' for password in self.list_passwords(): - if password['label'] == name: + if self.is_same_password(obj, password): + return True + if password['label'] == obj['label']: return True return False def create_password(self, post_obj): '''Create/add a password''' - if not self.exists_password(post_obj['label']): + if not self.exists_password(post_obj): r = requests.post( f'{self.http}://{self.host}/index.php/apps/passwords/api/1.0/password/create', data=post_obj, @@ -612,6 +615,16 @@ class NextcloudHandler: else: self.exit.status_code_error(r.status_code) + def is_same_password(self, obj1, obj2): + '''Test if two password objects are the same or similar''' + if obj1 == obj2: + return True + if (obj1['username'] == obj2['username'] and + obj1['password'] == obj2['password'] and + obj1['url'] == obj2['url']): + return True + return False + class NcPasswordClient: '''Nextcloud Password Client'''