jazzband / django-oauth-toolkit

OAuth2 goodies for the Djangonauts!

Home Page:https://django-oauth-toolkit.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add a documentation how to integrate `django-ninja`

Zerotask opened this issue · comments

Is your feature request related to a problem? Please describe.
We're currently using django-oauth-toolkit together with DRF and we want to consider a switch to https://django-ninja.dev.

Describe the solution you'd like
Either some helper/utils function/classes to easily integrate Django-ninja and/or a documentation page (similar to https://django-oauth-toolkit.readthedocs.io/en/latest/rest-framework/rest-framework.html)

Describe alternatives you've considered
Haven't considered any alternatives yet.

Hello, any news on this,
@Zerotask have you successfully pulled this off?

Hello, any news on this, @Zerotask have you successfully pulled this off?

Yes, we do it like so:

from django.http import HttpRequest
from ninja.security import HttpBearer
from oauth2_provider.oauth2_backends import get_oauthlib_core


class AuthBearer(HttpBearer):
    def authenticate(self, request: HttpRequest, token: str):
        oauthlib_core = get_oauthlib_core()
        valid, _ = oauthlib_core.verify_request(request, scopes=[])
        if not valid:
            return None
        return token

Then you just pass it to the NinjaAPI constructor

from ninja import NinjaAPI


api = NinjaAPI(
    auth=AuthBearer(),
)