-
-
Save ranvijay-sachan/c3d980d51248099adb7449942f7b5bf7 to your computer and use it in GitHub Desktop.
django-oidc-provider and DRF example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
NOT PRODUCTION READY | |
Usage: | |
REST_FRAMEWORK = { | |
... | |
'DEFAULT_AUTHENTICATION_CLASSES': ( | |
.... | |
'ibr.users.accounts_api_v0.auth.OidcOauth2Auth' | |
), | |
... | |
curl --header 'Authorization: Bearer 807551eadb2740dcbad74ad6e74921a6' http://protected-view/ | |
""" | |
from rest_framework import authentication | |
from rest_framework import exceptions | |
from oidc_provider.models import Token | |
from oidc_provider.lib.utils.oauth2 import extract_access_token | |
class OidcOauth2Auth(authentication.BaseAuthentication): | |
def authenticate(self, request): | |
access_token = extract_access_token(request) | |
if not access_token: | |
# not this kind of auth | |
return None | |
oauth2_token = None | |
try: | |
oauth2_token = Token.objects.get(access_token=access_token) | |
except Token.DoesNotExist: | |
raise exceptions.AuthenticationFailed("The oauth2 token is invalid") | |
if oauth2_token.has_expired(): | |
raise exceptions.AuthenticationFailed("The oauth2 token has expired") | |
return oauth2_token.user, None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment