Skip to content

Commit 2a7e5b9

Browse files
committed
Updated password encryption to argon2
1 parent 6f16fc6 commit 2a7e5b9

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

backend/PyMatcha/models/user.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from __future__ import annotations
2020

2121
import datetime
22-
import hashlib
2322
import logging
2423
from typing import Any
2524
from typing import Dict
@@ -64,11 +63,6 @@ class User(Model):
6463
confirmed_on = Field(datetime.datetime, fmt="%Y-%m-%d %H:%M:%S")
6564
previous_reset_token = Field(str)
6665

67-
def check_password(self, password: str) -> bool:
68-
logging.debug("Checking password again {} hashed password".format(self.id))
69-
_hash, salt = self.password.split(":")
70-
return _hash == hashlib.sha3_512(salt.encode() + password.encode()).hexdigest()
71-
7266
@staticmethod
7367
def create(
7468
first_name: str,

backend/PyMatcha/routes/api/auth/login.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
from PyMatcha.utils.decorators import validate_params
3636
from PyMatcha.utils.errors import NotFoundError
3737
from PyMatcha.utils.errors import UnauthorizedError
38+
from PyMatcha.utils.password import check_password
3839
from PyMatcha.utils.success import Success
3940
from PyMatcha.utils.success import SuccessOutput
4041

41-
4242
REQUIRED_KEYS_LOGIN = {"username": str, "password": str}
4343

4444
auth_login_bp = Blueprint("auth_login", __name__)
@@ -56,7 +56,7 @@ def auth_login():
5656
except NotFoundError:
5757
current_app.logger.debug("/auth/login -> User not found")
5858
raise UnauthorizedError("Incorrect username or password")
59-
if not u.check_password(password):
59+
if not check_password(u.password, password):
6060
current_app.logger.debug("/auth/login -> Password invalid")
6161
raise UnauthorizedError("Incorrect username or password")
6262

backend/PyMatcha/routes/api/profile/edit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from PyMatcha.utils.errors import UnauthorizedError
3636
from PyMatcha.utils.mail import send_mail_html
3737
from PyMatcha.utils.mail import send_mail_text
38+
from PyMatcha.utils.password import check_password
3839
from PyMatcha.utils.success import Success
3940

4041
profile_edit_bp = Blueprint("profile_edit", __name__)
@@ -126,7 +127,7 @@ def edit_password():
126127
data = request.get_json()
127128
old_password = data["old_password"]
128129
new_password = data["new_password"]
129-
if not current_user.check_password(old_password):
130+
if not check_password(current_user.password, old_password):
130131
raise UnauthorizedError("Incorrect password")
131132
current_user.password = hash_password(new_password)
132133
current_user.save()

backend/PyMatcha/utils/password.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
"""
19-
import hashlib
20-
import logging
21-
import uuid
19+
from argon2 import PasswordHasher
20+
21+
ph = PasswordHasher()
2222

2323

2424
def hash_password(password: str) -> str:
25-
salt = uuid.uuid4().hex
26-
logging.debug("Hashing password with salt {}".format(salt))
27-
return hashlib.sha3_512(salt.encode() + password.encode()).hexdigest() + ":" + salt
25+
return ph.hash(password)
26+
27+
28+
def check_password(hash: str, password: str) -> bool:
29+
return ph.verify(hash, password)

backend/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ pre-commit==2.5.1
2727
names==0.3.0
2828
lorem==0.1.1
2929
future==0.18.2
30-
ip2geotools==0.1.5
30+
ip2geotools==0.1.5
31+
32+
argon2-cffi==20.1.0

0 commit comments

Comments
 (0)