-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
39 lines (32 loc) · 1.06 KB
/
auth.py
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
import bcrypt
import jwt
import os
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
# Group 11
## Ge Wang | a1880714
## Yong Yue Beh | a1843874
## Liew Yi Hui | a1907230
## Mustafa Jamale | a1863981
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key")
def generate_rsa_keypair():
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048
)
public_key = private_key.public_key()
return private_key, public_key
def generate_jwt(user_id):
return jwt.encode({"user_id": user_id}, SECRET_KEY, algorithm="HS256")
def verify_jwt(token):
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
return payload.get("user_id")
except jwt.ExpiredSignatureError:
return None
except jwt.InvalidTokenError:
return None
def hash_password(password):
return bcrypt.hashpw(password.encode(), bcrypt.gensalt())
def check_password(stored_hash, password):
return bcrypt.checkpw(password.encode(), stored_hash)