Skip to content

Instantly share code, notes, and snippets.

@koriaf
Created April 30, 2017 15:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save koriaf/907d1e16068d1a04056eedf736a203f5 to your computer and use it in GitHub Desktop.
Save koriaf/907d1e16068d1a04056eedf736a203f5 to your computer and use it in GitHub Desktop.
django-oidc-provider and DRF example
"""
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
@mitchelljkotler
Copy link

What changes need to be made to this to make it production ready?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment