Created
April 30, 2017 15:34
-
-
Save koriaf/907d1e16068d1a04056eedf736a203f5 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
What changes need to be made to this to make it production ready?