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

Feat/hashed passwords #58

Merged
merged 2 commits into from
Sep 13, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Backend API/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ librosa==0.10.0.post2
httpx==0.24.1
pytest==7.4.0
autopep8==2.0.2
bcrypt==4.0.1
10 changes: 9 additions & 1 deletion Backend API/src/__tests__/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from main import app as app
import json
import pytest
import bcrypt



def test_get_user_correct(clear_test_data_db):
Expand All @@ -21,7 +23,13 @@ def test_get_user_correct(clear_test_data_db):
assert res_get_user.status_code == 200
assert res_get_user.json()["name"]==name
assert res_get_user.json()["photo"]==foto
assert res_get_user.json()["password"]==password

# check password

utf8_password = res_get_user.json()["password"].encode('utf-8')
assert bcrypt.checkpw(password.encode('utf-8'),utf8_password)==True



try:
fecha = res_get_user.json()["register_date"]
Expand Down
4 changes: 3 additions & 1 deletion Backend API/src/model/User.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class User:
photo: str
register_date: str
password: str
""" Stores the last 5 songs that the user played """
playback_history: list
""" Stores the last 5 songs that the user played """
playlists: list
saved_playlists: list

def get_json(self) -> json:

self.password= self.password.decode('utf-8')
user_json = json.dumps(self.__dict__)
return user_json
13 changes: 9 additions & 4 deletions Backend API/src/services/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from model.User import User
from fastapi import HTTPException
from services.utils import checkValidParameterString
import bcrypt


user_collection = Database().connection["usuario"]

Expand Down Expand Up @@ -70,13 +72,16 @@ def create_user(name: str, photo: str, password: str) -> None:
if result_user_exists:
raise HTTPException(status_code=400, detail="La playlist ya existe")

utf8_password = password.encode('utf-8')
hashed_password = bcrypt.hashpw(utf8_password, bcrypt.gensalt())

result = user_collection.insert_one(
{'name': name, 'photo': photo if 'http' in photo else '', 'register_date': date_iso8601, 'password': password,'saved_playlists': [], 'playlists': [], 'playback_history': []})
{'name': name, 'photo': photo if 'http' in photo else '', 'register_date': date_iso8601, 'password': hashed_password, 'saved_playlists': [], 'playlists': [], 'playback_history': []})

return True if result.acknowledged else False


def update_user(name: str, photo: str, playlists:list,saved_playlists:list,playback_history:list) -> None:
def update_user(name: str, photo: str, playlists: list, saved_playlists: list, playback_history: list) -> None:
""" Updates a user , duplicated playlists and songs wont be added

Parameters
Expand Down Expand Up @@ -105,8 +110,8 @@ def update_user(name: str, photo: str, playlists:list,saved_playlists:list,playb
if not result_user_exists:
raise HTTPException(status_code=404, detail="El usuario no existe")

result = user_collection.update_one( {'name': name} ,
{ "$set": {'photo': photo if 'http' in photo else '','saved_playlists': list(set(saved_playlists)), 'playlists': list(set(playlists)), 'playback_history': list(set(playback_history))} })
result = user_collection.update_one({'name': name},
{"$set": {'photo': photo if 'http' in photo else '', 'saved_playlists': list(set(saved_playlists)), 'playlists': list(set(playlists)), 'playback_history': list(set(playback_history))}})


def delete_user(name: str) -> None:
Expand Down