-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
executable file
·79 lines (61 loc) · 2.64 KB
/
Copy pathmodels.py
File metadata and controls
executable file
·79 lines (61 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from datetime import datetime
import bcrypt
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
hashed_password = db.Column(db.String(60), nullable=False)
role = db.Column(db.String(10), nullable=False)
def check_password(self, password):
password1 = list(self.hashed_password)
# re sort pw before hashing
if password1[0] != "$" or password1[3] != "$" or password1[6] != "$":
password1[8], password1[0], password1[9], password1[3], password1[11], password1[6] = (
password1[0],
password1[8],
password1[3],
password1[9],
password1[6],
password1[11],
)
if password1[1] != "2" or password1[2] != "b":
password1[1], password1[19], password1[2], password1[18] = (
password1[19],
password1[1],
password1[18],
password1[2],
)
if password1[4] != "1" or password1[5] != "2":
password1[4], password1[22], password1[5], password1[29] = (
password1[22],
password1[4],
password1[29],
password1[5],
)
password1 = "".join(password1)
return bcrypt.checkpw(password.encode("utf-8"), password1.encode("utf-8"))
@classmethod
def get_user_by_username(cls, username):
return cls.query.filter_by(username=username).first()
@classmethod
def create_user(cls, username, password, role):
hashed_password1 = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
hashed_password2 = hashed_password1.decode("utf-8")
hashed_password = list(hashed_password2)
indices_to_swap = [(0, 8), (3, 9), (6, 11), (1, 19), (2, 18), (4, 22), (5, 29)]
for i, j in indices_to_swap:
hashed_password[i], hashed_password[j] = hashed_password[j], hashed_password[i]
hashed_password = ''.join(hashed_password)
user = cls(username=username, hashed_password=hashed_password, role=role)
db.session.add(user)
db.session.commit()
return user
class Logs(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False)
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now)
action = db.Column(db.String(255), nullable=False)
def __init__(self, username, action):
self.username = username
self.action = action