From 7780e117ecd79f3d2185565a4b9006c8b9e35bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Wieczorek?= Date: Tue, 8 Aug 2023 12:04:59 +0200 Subject: [PATCH 1/4] kernelci.api.latest: implement method for changing user password MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Wieczorek --- kernelci/api/latest.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/kernelci/api/latest.py b/kernelci/api/latest.py index a3213a7b0a..c0f84d7aac 100644 --- a/kernelci/api/latest.py +++ b/kernelci/api/latest.py @@ -51,6 +51,18 @@ def whoami(self) -> dict: def password_hash(self, password: str) -> dict: return self._post('/hash', {'password': password}).json() + def change_password(self, username: str, current: str, new: str) -> dict: + return self._post( + '/password', + { + 'current_password': {'password': current}, + 'new_password': {'password': new}, + }, + { + 'username': username, + }, + ).json() + def create_token(self, username: str, password: str, scopes: Optional[Sequence[str]] = None) -> str: data = { From e632bc8ab90d513e10f9d0bff3d62ee5def24e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Wieczorek?= Date: Tue, 8 Aug 2023 15:02:47 +0200 Subject: [PATCH 2/4] kernelci.cli.user: add `change_password` command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add command to change user's password. Sample command is as below: `./kci user change_password --username admin` Signed-off-by: Paweł Wieczorek --- kernelci/cli/user.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/kernelci/cli/user.py b/kernelci/cli/user.py index 4e4db5ce63..ab2d88779f 100644 --- a/kernelci/cli/user.py +++ b/kernelci/cli/user.py @@ -50,6 +50,20 @@ def _api_call(self, api, configs, args): return True +class cmd_change_password(APICommand): # pylint: disable=invalid-name + """Change a password for a given user""" + args = APICommand.args + [Args.username] + + def _api_call(self, api, configs, args): + # TODO(pawiecz): Add a prefix to clarify which password is requested + current = getpass.getpass() + # TODO(pawiecz): Request new password twice to ensure it has been + # typed properly + new = getpass.getpass() + api.change_password(args.username, current, new) + return True + + class cmd_get_group(APICommand): # pylint: disable=invalid-name """Get a user group with a given ID""" args = APICommand.args + [Args.group_id] From 3f6003b6edb2b5cb185cfb98108fbad729e4195f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Wieczorek?= Date: Tue, 8 Aug 2023 15:16:32 +0200 Subject: [PATCH 3/4] fixup! kernelci.api.latest: implement method for changing user password MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Wieczorek --- kernelci/api/latest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/kernelci/api/latest.py b/kernelci/api/latest.py index c0f84d7aac..4c5d5a96f5 100644 --- a/kernelci/api/latest.py +++ b/kernelci/api/latest.py @@ -52,6 +52,7 @@ def password_hash(self, password: str) -> dict: return self._post('/hash', {'password': password}).json() def change_password(self, username: str, current: str, new: str) -> dict: + """Change a password for a given user""" return self._post( '/password', { From 7890dbe4e11ac629ca86429c67462178dd97d1f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Wieczorek?= Date: Tue, 8 Aug 2023 17:58:50 +0200 Subject: [PATCH 4/4] fixup! kernelci.cli.user: add `change_password` command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Wieczorek --- kernelci/cli/user.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kernelci/cli/user.py b/kernelci/cli/user.py index ab2d88779f..6f012c1136 100644 --- a/kernelci/cli/user.py +++ b/kernelci/cli/user.py @@ -55,11 +55,12 @@ class cmd_change_password(APICommand): # pylint: disable=invalid-name args = APICommand.args + [Args.username] def _api_call(self, api, configs, args): - # TODO(pawiecz): Add a prefix to clarify which password is requested - current = getpass.getpass() - # TODO(pawiecz): Request new password twice to ensure it has been - # typed properly - new = getpass.getpass() + current = getpass.getpass("Current password: ") + new = getpass.getpass("New password: ") + retyped = getpass.getpass("Retype new password: ") + if new != retyped: + print("Sorry, passwords do not match.") + return False api.change_password(args.username, current, new) return True