aiverse / app /core /security.py
junaidte14's picture
aiverse backend to hf
3821102
Raw History Blame Contribute Delete
3.96 kB
"""
Security utilities
JWT token generation, password hashing, and authentication helpers
"""
from datetime import datetime, timedelta
from typing import Optional, Dict, Any
from jose import JWTError, jwt
from pwdlib import PasswordHash
from app.core.config import settings
# Password hashing using pwdlib (modern bcrypt)
password_hash = PasswordHash.recommended()
def hash_password(password: str) -> str:
"""
Hash password using bcrypt
Args:
password: Plain text password
Returns:
Hashed password
"""
return password_hash.hash(password)
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""
Verify password against hash
Args:
plain_password: Plain text password
hashed_password: Hashed password
Returns:
True if password matches, False otherwise
"""
return password_hash.verify(plain_password, hashed_password)
def create_access_token(
data: Dict[str, Any], expires_delta: Optional[timedelta] = None
) -> str:
"""
Create JWT access token
Args:
data: Data to encode in token (usually {"sub": user_id})
expires_delta: Token expiration time
Returns:
Encoded JWT token
"""
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(
minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES
)
jwt_claims = {"exp": expire, "iat": datetime.utcnow()}
if "type" not in to_encode:
jwt_claims["type"] = "access"
to_encode.update(jwt_claims)
encoded_jwt = jwt.encode(
to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM
)
return encoded_jwt
def create_refresh_token(
data: Dict[str, Any], expires_delta: Optional[timedelta] = None
) -> str:
"""
Create JWT refresh token
Args:
data: Data to encode in token
expires_delta: Token expiration time
Returns:
Encoded JWT refresh token
"""
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS)
to_encode.update({"exp": expire, "iat": datetime.utcnow(), "type": "refresh"})
encoded_jwt = jwt.encode(
to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM
)
return encoded_jwt
def decode_token(token: str) -> Optional[Dict[str, Any]]:
"""
Decode and validate JWT token
Args:
token: JWT token to decode
Returns:
Decoded token payload if valid, None otherwise
"""
try:
payload = jwt.decode(
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
)
return payload
except JWTError:
return None
def create_password_reset_token(email: str) -> str:
"""
Create password reset token
Args:
email: User email
Returns:
Password reset token
"""
delta = timedelta(hours=settings.PASSWORD_RESET_TOKEN_EXPIRE_HOURS)
return create_access_token(
data={"sub": email, "type": "password_reset"}, expires_delta=delta
)
def create_email_verification_token(email: str) -> str:
"""
Create email verification token
Args:
email: User email
Returns:
Email verification token
"""
delta = timedelta(hours=settings.EMAIL_VERIFICATION_TOKEN_EXPIRE_HOURS)
return create_access_token(
data={"sub": email, "type": "email_verification"}, expires_delta=delta
)
def verify_token_type(payload: Dict[str, Any], expected_type: str) -> bool:
"""
Verify token type
Args:
payload: Decoded token payload
expected_type: Expected token type
Returns:
True if token type matches, False otherwise
"""
return payload.get("type") == expected_type