Skip to content

Optional auth #709

Mar 20, 2023 · 2 comments · 5 replies
Discussion options

You must be logged in to vote

The solution is to override the HttpBearer class to return AnonymousUser

    def __call__(self, request: HttpRequest) -> Optional[Any]:
        headers = get_headers(request)
        auth_value = headers.get(self.header)
        if not auth_value:
            return AnonymousUser()  # if there is no key, we return AnonymousUser object
        parts = auth_value.split(" ")

        if parts[0].lower() != self.openapi_scheme:
            if settings.DEBUG:
                logger.error(f"Unexpected auth - '{auth_value}'")
            return None
        token = " ".join(parts[1:])
        return self.authenticate(request, token)

Massive thanks to @eadwinCode for his help in eadwinCode/django…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
5 replies
@baseplate-admin
Comment options

@baseplate-admin
Comment options

@baseplate-admin
Comment options

@vitalik
Comment options

@baseplate-admin
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by baseplate-admin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants