You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from ninja import NinjaAPI
from ninja.security import HttpBearer
api = NinjaAPI()
class InvalidToken(Exception):
pass
@api.exception_handler(InvalidToken)
def on_invalid_token(request, exc):
return api.create_response(request, {"detail": "Invalid token supplied"}, status=401)
class AuthBearer(HttpBearer):
def authenticate(self, request, token):
if token == "supersecret":
return token
raise InvalidToken
@api.get("/bearer", auth=AuthBearer())
def bearer(request):
return {"token": request.auth}
This allows to add a custom authentication scheme, and return a custom response if the authentication failed.
However, the status code 401 and the form of the response is not documented in the API. If I try it out in the UI, it is also marked as "Undocumented":
I could document it myself by adding a schema, and adding it to the function decorator like this:
However, I'm adding the auth handler globally, by doing api = NinjaAPI(auth=[AuthBearer()], ...), instead of adding it to every endpoint.
Therefore, I would also like to be able to add the UnauthorizedSchema globally as well. Is there a way to tie this scheme to the on_invalid_token exception handler or the custom AuthBearer class, so that every endpoint that requires authentication also contains the documentation about what is returned when the user isn't authenticated properly?
The text was updated successfully, but these errors were encountered:
Take this example code from the documentation: https://django-ninja.dev/guides/authentication/#custom-exceptions
This allows to add a custom authentication scheme, and return a custom response if the authentication failed.
However, the status code 401 and the form of the response is not documented in the API. If I try it out in the UI, it is also marked as "Undocumented":
I could document it myself by adding a schema, and adding it to the function decorator like this:
However, I'm adding the auth handler globally, by doing
api = NinjaAPI(auth=[AuthBearer()], ...)
, instead of adding it to every endpoint.Therefore, I would also like to be able to add the
UnauthorizedSchema
globally as well. Is there a way to tie this scheme to theon_invalid_token
exception handler or the customAuthBearer
class, so that every endpoint that requires authentication also contains the documentation about what is returned when the user isn't authenticated properly?The text was updated successfully, but these errors were encountered: