60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
# ==============================================================================
|
|
# Formeleon - Multi-tenant Form Generator
|
|
# Author: Björn Runden <br@runden.net>
|
|
# Module: services/tenant_service.py
|
|
# Description: Business logic for tenant management and validation.
|
|
# ==============================================================================
|
|
|
|
from typing import Optional
|
|
from models import Tenant
|
|
|
|
class TenantService:
|
|
"""Service layer for handling tenant-related business logic."""
|
|
|
|
@staticmethod
|
|
def get_tenant_by_public_key(public_key: str) -> Optional[Tenant]:
|
|
"""
|
|
Get tenant by public key.
|
|
|
|
:param public_key: Public API key
|
|
:return: Tenant object or None
|
|
"""
|
|
if not public_key:
|
|
return None
|
|
return Tenant.query.filter_by(public_key=public_key).first()
|
|
|
|
@staticmethod
|
|
def validate_origin(origin: str, tenant: Tenant) -> bool:
|
|
"""
|
|
Validate that the request origin matches allowed origins for the tenant.
|
|
|
|
:param origin: Request origin/referer
|
|
:param tenant: Tenant object to check against
|
|
:return: True if origin is allowed, False otherwise
|
|
"""
|
|
if not origin:
|
|
return False
|
|
|
|
# Remove trailing slash and protocol
|
|
origin = origin.rstrip('/')
|
|
|
|
allowed_origins = tenant.get_allowed_origins_list()
|
|
|
|
# Check for wildcard
|
|
if '*' in allowed_origins:
|
|
return True
|
|
|
|
# Exact match
|
|
if origin in allowed_origins:
|
|
return True
|
|
|
|
# Check if origin matches any allowed domain (with subdomains)
|
|
for allowed in allowed_origins:
|
|
if allowed.startswith('*'):
|
|
# Wildcard subdomain match
|
|
domain = allowed[1:] # Remove leading *
|
|
if origin.endswith(domain):
|
|
return True
|
|
|
|
return False
|