Fix authentication: Follow unified API model - token account_id is authoritative

- Simplified authentication logic to match unified API documentation
- Token's account_id is now the sole source of truth for account context
- Removed validation against user.account (no longer valid per unified API model)
- Middleware now simply extracts account_id from JWT and sets request.account
- Matches documented flow: Extract Account ID → Load Account Object → Set request.account
This commit is contained in:
IGNY8 VPS (Salman)
2025-11-16 19:36:18 +00:00
parent 46b5b5f1b2
commit 8171014a7e
2 changed files with 11 additions and 32 deletions

View File

@@ -62,32 +62,18 @@ class JWTAuthentication(BaseAuthentication):
# User not found - return None to allow other auth classes to try # User not found - return None to allow other auth classes to try
return None return None
# Get account from token (token's account_id is authoritative for current context) # Get account from token (token's account_id is authoritative per unified API model)
# Unified API Standard: Token contains account_id, middleware extracts and sets request.account
account_id = payload.get('account_id') account_id = payload.get('account_id')
account = None account = None
if account_id: if account_id:
try: try:
account = Account.objects.get(id=account_id) account = Account.objects.get(id=account_id)
# Verify user has access to this account
# For developers/admins, they can access any account
# For regular users, verify they belong to this account
if not user.is_admin_or_developer() and not user.is_system_account_user():
# Regular user - must belong to this account
if user.account and user.account.id != account_id:
# User doesn't belong to token's account - use user's account instead
account = user.account
except Account.DoesNotExist: except Account.DoesNotExist:
# Account from token doesn't exist - use user's account instead # Account from token doesn't exist - set to None
pass
if not account:
try:
account = getattr(user, 'account', None)
except (AttributeError, Exception):
# If account access fails, set to None
account = None account = None
# Set account on request # Set account on request (unified API model: token's account_id is authoritative)
request.account = account request.account = account
return (user, token) return (user, token)

View File

@@ -99,21 +99,14 @@ class AccountContextMiddleware(MiddlewareMixin):
user = User.objects.select_related('account', 'account__plan').get(id=user_id) user = User.objects.select_related('account', 'account__plan').get(id=user_id)
request.user = user request.user = user
if account_id: if account_id:
# Verify account still exists # Unified API Standard: Extract account_id from JWT, load Account object, set request.account
account = Account.objects.get(id=account_id) # Token's account_id is authoritative - no validation against user.account
# Token's account_id is authoritative for current context try:
# For developers/admins, they can access any account account = Account.objects.get(id=account_id)
# For regular users, verify they belong to this account
if not user.is_admin_or_developer() and not user.is_system_account_user():
# Regular user - must belong to this account
if user.account and user.account.id != account_id:
# User doesn't belong to token's account - use user's account instead
request.account = user.account
else:
request.account = account
else:
# Developer/admin/system user - use token's account (they can access any)
request.account = account request.account = account
except Account.DoesNotExist:
# Account from token doesn't exist - set to None
request.account = None
else: else:
try: try:
user_account = getattr(user, 'account', None) user_account = getattr(user, 'account', None)