lizenzserver
Dieser Commit ist enthalten in:
32
v2_lizenzserver/app/core/config.py
Normale Datei
32
v2_lizenzserver/app/core/config.py
Normale Datei
@@ -0,0 +1,32 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
from typing import List
|
||||
|
||||
class Settings(BaseSettings):
|
||||
PROJECT_NAME: str = "License Server"
|
||||
VERSION: str = "1.0.0"
|
||||
API_PREFIX: str = "/api"
|
||||
|
||||
SECRET_KEY: str = "your-secret-key-change-this-in-production"
|
||||
ALGORITHM: str = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
||||
|
||||
DATABASE_URL: str = "postgresql://license_user:license_password@db:5432/license_db"
|
||||
|
||||
REDIS_URL: str = "redis://redis:6379"
|
||||
|
||||
ALLOWED_ORIGINS: List[str] = [
|
||||
"https://api-software-undso.z5m7q9dk3ah2v1plx6ju.com",
|
||||
"https://admin-panel-undso.z5m7q9dk3ah2v1plx6ju.com"
|
||||
]
|
||||
|
||||
DEBUG: bool = False
|
||||
|
||||
MAX_ACTIVATIONS_PER_LICENSE: int = 5
|
||||
HEARTBEAT_INTERVAL_MINUTES: int = 15
|
||||
OFFLINE_GRACE_PERIOD_DAYS: int = 7
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
case_sensitive = True
|
||||
|
||||
settings = Settings()
|
||||
52
v2_lizenzserver/app/core/security.py
Normale Datei
52
v2_lizenzserver/app/core/security.py
Normale Datei
@@ -0,0 +1,52 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
from jose import JWTError, jwt
|
||||
from passlib.context import CryptContext
|
||||
from fastapi import HTTPException, Security, Depends
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from sqlalchemy.orm import Session
|
||||
from app.db.database import get_db
|
||||
from app.models.models import ApiKey
|
||||
from app.core.config import settings
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
security = HTTPBearer()
|
||||
|
||||
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
||||
to_encode = data.copy()
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(minutes=15)
|
||||
to_encode.update({"exp": expire})
|
||||
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
def verify_token(credentials: HTTPAuthorizationCredentials = Security(security)):
|
||||
token = credentials.credentials
|
||||
try:
|
||||
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|
||||
return payload
|
||||
except JWTError:
|
||||
raise HTTPException(status_code=403, detail="Invalid token")
|
||||
|
||||
def verify_api_key(api_key: str, db: Session):
|
||||
key = db.query(ApiKey).filter(
|
||||
ApiKey.key == api_key,
|
||||
ApiKey.is_active == True
|
||||
).first()
|
||||
|
||||
if not key:
|
||||
raise HTTPException(status_code=401, detail="Invalid API key")
|
||||
|
||||
key.last_used = datetime.utcnow()
|
||||
db.commit()
|
||||
|
||||
return key
|
||||
|
||||
def get_api_key(
|
||||
credentials: HTTPAuthorizationCredentials = Security(security),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
api_key = credentials.credentials
|
||||
return verify_api_key(api_key, db)
|
||||
In neuem Issue referenzieren
Einen Benutzer sperren