Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Add an API method that allows to update password
Browse files Browse the repository at this point in the history
  • Loading branch information
tdemin committed Sep 16, 2019
1 parent 2083cc1 commit 3f90b1e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion project_amber/app.py
Expand Up @@ -12,7 +12,7 @@
handle_session_id_req
from project_amber.handlers.task import handle_task_id_request, \
handle_task_request
from project_amber.handlers.users import signup
from project_amber.handlers.users import signup, update_user_data

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = config["database"]
Expand All @@ -33,6 +33,7 @@ def middleware():
methods=["GET", "POST"])
app.add_url_rule("/api/task/<task_id>", "task_id", handle_task_id_request, \
methods=["GET", "PATCH", "DELETE"])
app.add_url_rule("/api/user", "user", update_user_data, methods=["PATCH"])
app.add_url_rule("/api/session", "session", handle_session_req, methods=["GET"])
app.add_url_rule("/api/session/<session_id>", "session_id", \
handle_session_id_req, methods=["GET", "DELETE"])
Expand Down
16 changes: 15 additions & 1 deletion project_amber/handlers/users.py
Expand Up @@ -2,7 +2,21 @@

from project_amber.const import EMPTY_RESP
from project_amber.errors import BadRequest
from project_amber.helpers.auth import addUser
from project_amber.helpers.auth import addUser, updateUser

def update_user_data():
"""
User data PATCH request handler. Accepts JSON with these parameters:
```
{
"password": "my_new_password"
}
```
Returns HTTP 200 on success.
"""
if "password" in request.json:
updateUser(password=request.json.get("password"))
return EMPTY_RESP

def signup():
"""
Expand Down
17 changes: 16 additions & 1 deletion project_amber/helpers/auth.py
Expand Up @@ -6,7 +6,7 @@

from project_amber.const import MSG_USER_NOT_FOUND, MSG_USER_EXISTS
from project_amber.db import db
from project_amber.helpers import time
from project_amber.helpers import time, LoginUser
from project_amber.errors import Unauthorized, NotFound, Conflict
from project_amber.logging import log
from project_amber.models.auth import User, Session
Expand All @@ -33,6 +33,21 @@ def addUser(name: str, password: str) -> int:
db.session.commit()
return user.id

def updateUser(**kwargs) -> int:
"""
Updates user data in the database. Returns their ID on success.
"""
user: LoginUser = request.user
user_record = db.session.query(User).filter_by(id=user.id).one()
for attribute in kwargs:
if attribute == "password":
user_record.password = hashpw(
prehash(kwargs["password"]),
gensalt()
).decode()
db.session.commit()
return user.id

def removeUser(uid: int) -> int:
"""
Removes a user given their ID. Returns their ID on success.
Expand Down

0 comments on commit 3f90b1e

Please sign in to comment.