mirror of
https://github.com/dograh-hq/dograh.git
synced 2026-06-10 08:05:22 +02:00
feat: add authentication for OSS (#167)
* feat: add authentication for OSS Fixes #157 and #156 * fix: fix token generation * fix: limit fastapi workers to 1
This commit is contained in:
parent
0791975864
commit
642cc34e8c
48 changed files with 994 additions and 303 deletions
28
api/utils/auth.py
Normal file
28
api/utils/auth.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import bcrypt
|
||||
import jwt
|
||||
|
||||
from api.constants import OSS_JWT_EXPIRY_HOURS, OSS_JWT_SECRET
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
|
||||
|
||||
|
||||
def verify_password(password: str, password_hash: str) -> bool:
|
||||
return bcrypt.checkpw(password.encode("utf-8"), password_hash.encode("utf-8"))
|
||||
|
||||
|
||||
def create_jwt_token(user_id: int, email: str) -> str:
|
||||
payload = {
|
||||
"sub": str(user_id),
|
||||
"email": email,
|
||||
"exp": datetime.now(UTC) + timedelta(hours=OSS_JWT_EXPIRY_HOURS),
|
||||
"iat": datetime.now(UTC),
|
||||
}
|
||||
return jwt.encode(payload, OSS_JWT_SECRET, algorithm="HS256")
|
||||
|
||||
|
||||
def decode_jwt_token(token: str) -> dict:
|
||||
return jwt.decode(token, OSS_JWT_SECRET, algorithms=["HS256"])
|
||||
Loading…
Add table
Add a link
Reference in a new issue