init
This commit is contained in:
commit
fe636432d0
154 files changed
+30381
No files matched your search
@@ -0,0 +1,74 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from jose import JWTError, jwt
|
||||
from passlib.context import CryptContext
|
||||
|
||||
from app.config import get_settings
|
||||
|
||||
|
||||
pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto")
|
||||
|
||||
|
||||
def verify_password(plain_password: str, password_hash: str) -> bool:
|
||||
return pwd_context.verify(plain_password, password_hash)
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return pwd_context.hash(password)
|
||||
|
||||
|
||||
def create_access_token(subject: str) -> str:
|
||||
settings = get_settings()
|
||||
expire = datetime.now(timezone.utc) + timedelta(minutes=settings.access_token_expire_minutes)
|
||||
payload = {"sub": subject, "exp": expire}
|
||||
return jwt.encode(payload, settings.jwt_secret, algorithm=settings.jwt_algorithm)
|
||||
|
||||
|
||||
def decode_access_token(token: str) -> str | None:
|
||||
settings = get_settings()
|
||||
try:
|
||||
payload = jwt.decode(token, settings.jwt_secret, algorithms=[settings.jwt_algorithm])
|
||||
except JWTError:
|
||||
return None
|
||||
return payload.get("sub")
|
||||
|
||||
|
||||
def _integration_download_secret() -> str:
|
||||
settings = get_settings()
|
||||
if settings.integration_download_secret:
|
||||
return settings.integration_download_secret
|
||||
raise RuntimeError("Integration download secret is not configured.")
|
||||
|
||||
|
||||
def create_integration_download_token(
|
||||
*,
|
||||
file_id: int | None = None,
|
||||
version_id: int | None = None,
|
||||
kind: str,
|
||||
) -> tuple[str, datetime]:
|
||||
settings = get_settings()
|
||||
expire = datetime.now(timezone.utc) + timedelta(minutes=settings.integration_download_expire_minutes)
|
||||
payload = {
|
||||
"typ": "integration_download",
|
||||
"kind": kind,
|
||||
"exp": expire,
|
||||
}
|
||||
if file_id is not None:
|
||||
payload["file_id"] = file_id
|
||||
if version_id is not None:
|
||||
payload["version_id"] = version_id
|
||||
token = jwt.encode(payload, _integration_download_secret(), algorithm=settings.jwt_algorithm)
|
||||
return token, expire
|
||||
|
||||
|
||||
def decode_integration_download_token(token: str) -> dict | None:
|
||||
settings = get_settings()
|
||||
try:
|
||||
payload = jwt.decode(token, _integration_download_secret(), algorithms=[settings.jwt_algorithm])
|
||||
except JWTError:
|
||||
return None
|
||||
if payload.get("typ") != "integration_download":
|
||||
return None
|
||||
return payload
|
||||
Reference in new issue
Block a user