Skip to content
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
9 changes: 6 additions & 3 deletions bank-service/bank/database/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import psycopg
import hashlib
import bcrypt

from flask import g

Expand Down Expand Up @@ -29,7 +29,7 @@ def authenticate(username:int, password:str):
WHERE uuid = (%s);
""", (username,))
data = cursor.fetchone()
if data and hashlib.md5(password.encode()).hexdigest() == data['phash']:
if data and bcrypt.checkpw(password.encode('utf-8'), data['phash'].encode('utf-8')):
return {'id': data['id'], 'uuid': data['uuid']}
return None
except psycopg.Error as e:
Expand All @@ -51,7 +51,10 @@ def get_user_balance(user_id:int):
raise ValueError('No user with this id')

def add_account(uuid:str, password:str):
phash = hashlib.md5(password.encode()).hexdigest()
phash = bcrypt.hashpw(
password.encode('utf-8'),
bcrypt.gensalt(rounds = 12)
).hexdigest()
conn = get_db()
try:
cursor = conn.cursor()
Expand Down
1 change: 1 addition & 0 deletions bank-service/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
psycopg[binary,pool]
flask
bcrypt
uwsgi
1 change: 1 addition & 0 deletions store-service/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ flask
uwsgi
bleach
pillow
bcrypt
psycopg[binary,pool]
7 changes: 4 additions & 3 deletions store-service/store/database/db_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import psycopg
import bcrypt
import os
import csv
import base64
Expand Down Expand Up @@ -117,20 +118,20 @@ def get_game_info(game_id:int)->dict:
print(f'execute game query with id={game_id}:{e}')
return None

def check_user(user:User)->int:
def check_user(username:str, password:str)->int:
db = get_db()
cursor = db.cursor()
try:
cursor.execute(
'SELECT * FROM store.users WHERE name = %s;', (user.name,)
'SELECT * FROM store.users WHERE name = %s;', (username,)
)
user_data = cursor.fetchone()

if not user_data:
raise ValueError('Unknown username')

user_data = dict(user_data)
if user_data['password_hash'] == user.phash:
if bcrypt.checkpw(password.encode('utf-8'), user_data['password_hash'].encode('utf-8')):
return user_data['id'], user_data['balance']
raise ValueError('Wrong password')

Expand Down
3 changes: 1 addition & 2 deletions store-service/store/routes/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ def login_register():
username = request.form.get('login_username')
password = request.form.get('login_password')
try:
user = User(username, password)
user_id, balance = check_user(user)
user_id, balance = check_user(username, password)

session['user_id'] = user_id
session['balance'] = balance
Expand Down
2 changes: 1 addition & 1 deletion store-service/store/static/javascripts/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function loadMoreGames() {
}

function checkScroll() {
const threshold = 200;
const threshold = 70;
const { scrollTop, scrollHeight, clientHeight } = document.documentElement;

if (scrollTop + clientHeight >= scrollHeight - threshold) {
Expand Down
13 changes: 7 additions & 6 deletions store-service/store/utility/User.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import hashlib
import bcrypt
import re

class User():
Expand All @@ -24,9 +24,10 @@ def __init__(self, name:str, password:str, repassword:str = None):
raise ValueError('Username must have only letters, numbers or _ symbol')

self.name = name
self.phash = hashlib.md5(password.encode()).hexdigest()
self.phash = bcrypt.hashpw(
password.encode('utf-8'),
bcrypt.gensalt(rounds=12)
).decode('utf-8')

if repassword:
rphash = hashlib.md5(repassword.encode()).hexdigest()
if rphash != self.phash:
raise ValueError('Passwords does not matching')
if repassword and not bcrypt.checkpw(repassword.encode('utf-8'), self.phash.encode('utf-8')):
raise ValueError('Passwords does not matching')
File renamed without changes.
7 changes: 5 additions & 2 deletions tools/deGenerator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import bcrypt
import csv
import hashlib
import random
import io
import os
Expand Down Expand Up @@ -40,7 +40,10 @@
def generate_user(idx:int):
return [
idx,
hashlib.md5(f'username{idx}'.encode()).hexdigest(),
bcrypt.hashpw(
f'username{idx}'.encode('utf-8'),
bcrypt.gensalt(rounds=12)
).decode('utf-8'),
f'username{idx}',
round(random.uniform(0, 10000))
]
Expand Down
7 changes: 0 additions & 7 deletions tools/prepare.sh

This file was deleted.