Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core api: add change-user-password #78

Merged
merged 6 commits into from Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,45 @@
#!/usr/bin/env python3

#
# Copyright (C) 2021 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# NethServer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NethServer. If not, see COPYING.
#

import sys
import json
import agent
import redis

request = json.load(sys.stdin)

# Try to authenticate with user and password using unprivileged connection
auth_db = redis.Redis(host='localhost', db=0, port=6379)
try:
is_authenticated = auth_db.execute_command('AUTH', request['user'], request['current_password'])
except Exception as e:
# validation error
agent.set_status('validation-failed')
json.dump([{'field':'current_password', 'parameter':'current_password','value': 'xxx', 'error':'invalid_user_password'}], fp=sys.stdout)
print(str(e), file=sys.stderr)
sys.exit(2)

if is_authenticated:
# On auhtentication success, switch to privileged connection to change the password
rdb = agent.redis_connect(privileged=True)
rdb.acl_setuser(request['user'], passwords=[f'+{request["new_password"]}'], reset_passwords=True, enabled=True)
json.dump({}, fp=sys.stdout)
gsanchietti marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,33 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "change-user-password-input",
"$id": "http://schema.nethserver.org/cluster/change-user-password-input.json",
"description": "Input schema of the change-user-password action",
"examples": [
{
"user": "admin",
"current_password": "Nethesis,1234",
"new_password": "Nethesis,12345"
}
],
"type": "object",
"properties": {
"user": {
"description": "Target user.",
"type": "string"
},
"current_password": {
"description": "Current password in plain text",
"type": "string"
},
"new_password": {
"description": "New password in plain text. The password will be updated only if the authentication with 'user' and 'current_password' is valid.",
"type": "string"
}
},
"required": [
"user",
"current_password",
"new_password"
gsanchietti marked this conversation as resolved.
Show resolved Hide resolved
]
}
@@ -0,0 +1,11 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "change-user-password output",
"$id": "http://schema.nethserver.org/cluster/change-user-password-output.json",
"description": "Just an empty object, representing a successful response",
"examples": [
{}
],
"type": "object",
"properties": {}
}