Skip to content

decorators

django_spire.auth.decorators

bearer_token_auth_required

Source code in django_spire/auth/decorators.py
def bearer_token_auth_required(token: str) -> Callable[[Callable], Callable]:
    def decorator_wrapper(view_func: Callable) -> Callable:
        def view_wrapper(request: WSGIRequest, *args, **kwargs) -> HttpResponse:
            if request.user.is_authenticated:
                return view_func(request, *args, **kwargs)

            auth_header_parts = request.META.get('HTTP_AUTHORIZATION', '').split(' ')

            if auth_header_parts[0].lower() == 'bearer':
                if auth_header_parts[1] == token:
                    return view_func(request, *args, **kwargs)

            return HttpResponse(status=401)

        return view_wrapper

    return decorator_wrapper