Repository navigation
Expand file tree
/
Copy pathsecurity.py
More file actions
107 lines (81 loc) Β· 3.16 KB
/
Copy pathsecurity.py
File metadata and controls
107 lines (81 loc) Β· 3.16 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""Security utilities β JWT, Fernet encryption, password hashing."""
from datetime import datetime, timedelta, timezone
from typing import Any, Optional
from uuid import UUID
import bcrypt
from cryptography.fernet import Fernet, InvalidToken
from jose import JWTError, jwt
from app.core.config import get_settings
settings = get_settings()
# bcrypt truncates / rejects beyond 72 bytes; enforce explicitly.
_BCRYPT_MAX_PASSWORD_BYTES = 72
def _derive_fernet_key(secret: str) -> bytes:
"""Derive a url-safe Fernet key from the application secret."""
import base64
import hashlib
digest = hashlib.sha256(secret.encode("utf-8")).digest()
return base64.urlsafe_b64encode(digest)
_fernet = Fernet(_derive_fernet_key(settings.secret_key))
def encrypt_token(plaintext: str) -> str:
return _fernet.encrypt(plaintext.encode("utf-8")).decode("utf-8")
def decrypt_token(ciphertext: str) -> str:
try:
return _fernet.decrypt(ciphertext.encode("utf-8")).decode("utf-8")
except InvalidToken as exc:
raise ValueError("Failed to decrypt token") from exc
def create_access_token(
subject: str | UUID,
extra_claims: Optional[dict[str, Any]] = None,
expires_delta: Optional[timedelta] = None,
) -> str:
expire = datetime.now(timezone.utc) + (
expires_delta
if expires_delta
else timedelta(minutes=settings.jwt_access_token_expire_minutes)
)
payload: dict[str, Any] = {
"sub": str(subject),
"exp": expire,
"iat": datetime.now(timezone.utc),
"type": "access",
}
if extra_claims:
payload.update(extra_claims)
return jwt.encode(payload, settings.secret_key, algorithm=settings.jwt_algorithm)
def create_refresh_token(
subject: str | UUID,
expires_delta: Optional[timedelta] = None,
) -> str:
expire = datetime.now(timezone.utc) + (
expires_delta
if expires_delta
else timedelta(days=settings.jwt_refresh_token_expire_days)
)
payload = {
"sub": str(subject),
"exp": expire,
"iat": datetime.now(timezone.utc),
"type": "refresh",
}
return jwt.encode(payload, settings.secret_key, algorithm=settings.jwt_algorithm)
def decode_token(token: str) -> dict[str, Any]:
try:
return jwt.decode(token, settings.secret_key, algorithms=[settings.jwt_algorithm])
except JWTError as exc:
raise ValueError("Invalid or expired token") from exc
def _password_bytes(password: str) -> bytes:
raw = password.encode("utf-8")
if len(raw) > _BCRYPT_MAX_PASSWORD_BYTES:
raise ValueError(
f"Password cannot be longer than {_BCRYPT_MAX_PASSWORD_BYTES} bytes"
)
return raw
def verify_password(plain: str, hashed: str) -> bool:
"""Verify a password against a bcrypt hash (passlib or native $2b$ form)."""
try:
return bcrypt.checkpw(plain.encode("utf-8"), hashed.encode("utf-8"))
except (ValueError, TypeError):
return False
def hash_password(password: str) -> str:
"""Hash with bcrypt 2b. Compatible with hashes previously produced via passlib."""
return bcrypt.hashpw(_password_bytes(password), bcrypt.gensalt()).decode("utf-8")