You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

48 lines
2.0 KiB

from rest_framework.authentication import TokenAuthentication
from rest_framework.exceptions import AuthenticationFailed
import logging
logger = logging.getLogger(__name__)
class DebugTokenAuthentication(TokenAuthentication):
"""
Extended TokenAuthentication with detailed logging for debugging
"""
def authenticate(self, request):
auth_header = request.META.get('HTTP_AUTHORIZATION', '')
logger.info(f"🔍 AUTH DEBUG - Header: {auth_header}")
# Check if header exists
if not auth_header:
logger.warning("🔴 AUTH DEBUG - No Authorization header found")
return None
# Extract token
parts = auth_header.split()
if len(parts) != 2 or parts[0].lower() != 'token':
logger.warning(f"🔴 AUTH DEBUG - Invalid header format: {parts}")
return None
token_key = parts[1]
logger.info(f"🔍 AUTH DEBUG - Token key extracted: {token_key[:10]}...")
try:
# Try to get token from database
from rest_framework.authtoken.models import Token
token = Token.objects.select_related('user').get(key=token_key)
logger.info(f"✅ AUTH DEBUG - Token found in DB")
logger.info(f"✅ AUTH DEBUG - User: {token.user}")
logger.info(f"✅ AUTH DEBUG - User ID: {token.user.id}")
logger.info(f"✅ AUTH DEBUG - User is_active: {token.user.is_active}")
logger.info(f"✅ AUTH DEBUG - User is_authenticated: {token.user.is_authenticated}")
if not token.user.is_active:
logger.error("🔴 AUTH DEBUG - User is not active")
raise AuthenticationFailed('User inactive or deleted.')
logger.info("✅ AUTH DEBUG - Authentication SUCCESSFUL")
return (token.user, token)
except Exception as e:
logger.error(f"🔴 AUTH DEBUG - Token lookup failed: {str(e)}")
raise AuthenticationFailed('Invalid token.')