from rest_framework.authentication import TokenAuthentication
from rest_framework.exceptions import AuthenticationFailed


class BearerTokenAuthentication(TokenAuthentication):
    """
    Extends DRF's TokenAuthentication to accept both:
      Authorization: Token <token>
      Authorization: Bearer <token>
    """

    def authenticate(self, request):
        auth_header = request.META.get('HTTP_AUTHORIZATION', '')
        if not auth_header:
            return None
        parts = auth_header.split()
        if len(parts) != 2:
            return None
        prefix, token = parts
        if prefix.lower() not in ('token', 'bearer'):
            return None
        return self.authenticate_credentials(token)
