landvex: Fixar och tester klara för alla komponenter
- Datafabrik: Dockerfile fix, agentorkestrering fungerar - Vision: Identify-modell, FAISS, OCR alla testade - API: Alla 7 integrationstester passerade - Upplösare: Entitetsupplösning verifierad
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
"""RBAC permission system."""
|
||||
from enum import Enum
|
||||
from typing import Set
|
||||
|
||||
|
||||
class Permission(str, Enum):
|
||||
"""All available permissions in the system."""
|
||||
|
||||
# Users
|
||||
USERS_CREATE = "users.create"
|
||||
USERS_READ = "users.read"
|
||||
USERS_UPDATE = "users.update"
|
||||
USERS_DELETE = "users.delete"
|
||||
USERS_IMPERSONATE = "users.impersonate"
|
||||
|
||||
# Tenants
|
||||
TENANTS_CREATE = "tenants.create"
|
||||
TENANTS_READ = "tenants.read"
|
||||
TENANTS_UPDATE = "tenants.update"
|
||||
TENANTS_DELETE = "tenants.delete"
|
||||
TENANTS_MANAGE = "tenants.manage"
|
||||
|
||||
# Roles
|
||||
ROLES_CREATE = "roles.create"
|
||||
ROLES_READ = "roles.read"
|
||||
ROLES_UPDATE = "roles.update"
|
||||
ROLES_DELETE = "roles.delete"
|
||||
ROLES_ASSIGN = "roles.assign"
|
||||
|
||||
# API Keys
|
||||
API_KEYS_CREATE = "api_keys.create"
|
||||
API_KEYS_READ = "api_keys.read"
|
||||
API_KEYS_REVOKE = "api_keys.revoke"
|
||||
API_KEYS_ROTATE = "api_keys.rotate"
|
||||
|
||||
# Billing
|
||||
BILLING_READ = "billing.read"
|
||||
BILLING_MANAGE = "billing.manage"
|
||||
BILLING_INVOICES = "billing.invoices"
|
||||
BILLING_SUBSCRIBE = "billing.subscribe"
|
||||
|
||||
# Audit Logs
|
||||
AUDIT_READ = "audit.read"
|
||||
AUDIT_EXPORT = "audit.export"
|
||||
|
||||
# Reports
|
||||
REPORTS_CREATE = "reports.create"
|
||||
REPORTS_READ = "reports.read"
|
||||
REPORTS_RUN = "reports.run"
|
||||
REPORTS_EXPORT = "reports.export"
|
||||
REPORTS_DELETE = "reports.delete"
|
||||
|
||||
# Plugins
|
||||
PLUGINS_INSTALL = "plugins.install"
|
||||
PLUGINS_CONFIGURE = "plugins.configure"
|
||||
PLUGINS_UNINSTALL = "plugins.uninstall"
|
||||
PLUGINS_ENABLE = "plugins.enable"
|
||||
|
||||
# Settings
|
||||
SETTINGS_READ = "settings.read"
|
||||
SETTINGS_UPDATE = "settings.update"
|
||||
|
||||
# Super admin - all access
|
||||
ALL = "*"
|
||||
|
||||
|
||||
# Predefined roles with their permissions
|
||||
DEFAULT_ROLES = {
|
||||
"superadmin": [Permission.ALL],
|
||||
"admin": [
|
||||
Permission.USERS_CREATE,
|
||||
Permission.USERS_READ,
|
||||
Permission.USERS_UPDATE,
|
||||
Permission.USERS_DELETE,
|
||||
Permission.ROLES_READ,
|
||||
Permission.ROLES_ASSIGN,
|
||||
Permission.API_KEYS_CREATE,
|
||||
Permission.API_KEYS_READ,
|
||||
Permission.API_KEYS_REVOKE,
|
||||
Permission.BILLING_READ,
|
||||
Permission.BILLING_INVOICES,
|
||||
Permission.AUDIT_READ,
|
||||
Permission.AUDIT_EXPORT,
|
||||
Permission.REPORTS_CREATE,
|
||||
Permission.REPORTS_READ,
|
||||
Permission.REPORTS_RUN,
|
||||
Permission.REPORTS_EXPORT,
|
||||
Permission.PLUGINS_CONFIGURE,
|
||||
Permission.PLUGINS_ENABLE,
|
||||
Permission.SETTINGS_READ,
|
||||
Permission.SETTINGS_UPDATE,
|
||||
],
|
||||
"editor": [
|
||||
Permission.USERS_READ,
|
||||
Permission.USERS_UPDATE,
|
||||
Permission.REPORTS_CREATE,
|
||||
Permission.REPORTS_READ,
|
||||
Permission.REPORTS_RUN,
|
||||
Permission.REPORTS_EXPORT,
|
||||
Permission.SETTINGS_READ,
|
||||
],
|
||||
"viewer": [
|
||||
Permission.USERS_READ,
|
||||
Permission.REPORTS_READ,
|
||||
Permission.SETTINGS_READ,
|
||||
],
|
||||
"api": [
|
||||
Permission.API_KEYS_READ,
|
||||
Permission.API_KEYS_CREATE,
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def has_permission(user_permissions: Set[str], required: str) -> bool:
|
||||
"""Check if user has the required permission.
|
||||
|
||||
Args:
|
||||
user_permissions: Set of permission strings the user has
|
||||
required: The required permission string
|
||||
|
||||
Returns:
|
||||
True if user has permission, False otherwise
|
||||
"""
|
||||
if Permission.ALL in user_permissions:
|
||||
return True
|
||||
|
||||
# Check exact permission
|
||||
if required in user_permissions:
|
||||
return True
|
||||
|
||||
# Check wildcard permissions (e.g., "users.*" matches "users.read")
|
||||
parts = required.split(".")
|
||||
for i in range(1, len(parts)):
|
||||
wildcard = ".".join(parts[:i]) + ".*"
|
||||
if wildcard in user_permissions:
|
||||
return True
|
||||
|
||||
return False
|
||||
@@ -0,0 +1,103 @@
|
||||
"""Security utilities for authentication and authorization."""
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
import jwt
|
||||
from passlib.context import CryptContext
|
||||
|
||||
from app.config import settings
|
||||
|
||||
# Password hashing with Argon2id
|
||||
pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
"""Verify a plain password against a hash."""
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def get_password_hash(password: str) -> str:
|
||||
"""Hash a password using Argon2id."""
|
||||
return pwd_context.hash(password)
|
||||
|
||||
|
||||
def create_access_token(
|
||||
subject: UUID | str,
|
||||
tenant_id: UUID | None = None,
|
||||
scopes: list[str] | None = None,
|
||||
expires_delta: timedelta | None = None,
|
||||
) -> str:
|
||||
"""Create JWT access token."""
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
|
||||
to_encode: dict[str, Any] = {
|
||||
"sub": str(subject),
|
||||
"exp": expire,
|
||||
"iat": datetime.utcnow(),
|
||||
"type": "access",
|
||||
}
|
||||
|
||||
if tenant_id:
|
||||
to_encode["tenant_id"] = str(tenant_id)
|
||||
if scopes:
|
||||
to_encode["scopes"] = scopes
|
||||
|
||||
encoded_jwt = jwt.encode(
|
||||
to_encode, settings.SECRET_KEY, algorithm=settings.JWT_ALGORITHM
|
||||
)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def create_refresh_token(subject: UUID | str) -> str:
|
||||
"""Create JWT refresh token."""
|
||||
expire = datetime.utcnow() + timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS)
|
||||
|
||||
to_encode: dict[str, Any] = {
|
||||
"sub": str(subject),
|
||||
"exp": expire,
|
||||
"iat": datetime.utcnow(),
|
||||
"type": "refresh",
|
||||
}
|
||||
|
||||
encoded_jwt = jwt.encode(
|
||||
to_encode, settings.SECRET_KEY, algorithm=settings.JWT_ALGORITHM
|
||||
)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def decode_token(token: str) -> dict[str, Any]:
|
||||
"""Decode and validate JWT token."""
|
||||
return jwt.decode(
|
||||
token, settings.SECRET_KEY, algorithms=[settings.JWT_ALGORITHM]
|
||||
)
|
||||
|
||||
|
||||
def generate_api_key() -> tuple[str, str]:
|
||||
"""Generate a new API key and its hash.
|
||||
|
||||
Returns:
|
||||
Tuple of (full_key, key_hash)
|
||||
"""
|
||||
import secrets
|
||||
import hashlib
|
||||
|
||||
# Generate random key: lv_<32 random chars>
|
||||
random_part = secrets.token_urlsafe(32)
|
||||
full_key = f"lv_{random_part}"
|
||||
|
||||
# Hash for storage
|
||||
key_hash = hashlib.sha256(full_key.encode()).hexdigest()
|
||||
|
||||
return full_key, key_hash
|
||||
|
||||
|
||||
def verify_api_key(plain_key: str, key_hash: str) -> bool:
|
||||
"""Verify an API key against its hash."""
|
||||
import hashlib
|
||||
|
||||
computed_hash = hashlib.sha256(plain_key.encode()).hexdigest()
|
||||
return computed_hash == key_hash
|
||||
Reference in New Issue
Block a user