feat(auth): implement JWT authentication with secure password hashing
This commit is contained in:
parent
6656a8782c
commit
56df3228a4
7 changed files with 146 additions and 74 deletions
29
backend/hermes/app/core/security.py
Normal file
29
backend/hermes/app/core/security.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from jose import jwt
|
||||
from pwdlib import PasswordHash
|
||||
|
||||
password_hash = PasswordHash.recommended()
|
||||
|
||||
SECRET_KEY = "CHANGE_ME"
|
||||
ALGORITHM = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 60
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return password_hash.hash(password)
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
return password_hash.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def create_access_token(subject: str) -> str:
|
||||
expire = datetime.now(UTC) + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
|
||||
payload = {
|
||||
"sub": subject,
|
||||
"exp": expire,
|
||||
}
|
||||
|
||||
return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)
|
||||
Loading…
Add table
Add a link
Reference in a new issue