server/src/metaeducation/auth.py
author durandn
Mon, 29 Feb 2016 12:23:37 +0100
changeset 6 39cecdd5260e
parent 1 5f50937893ac
child 11 cfc868991b82
permissions -rw-r--r--
Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     1
from rest_framework.authentication import BaseAuthentication
6
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
     2
from django.contrib.auth import get_user_model, login
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
     3
from django.contrib.auth.models import Permission
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
     4
from django.conf import settings
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
     5
import requests
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
     6
import re
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
     7
import json
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     8
6
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
     9
class MtdcOAuth2ClientCredentialsAuthentication(BaseAuthentication):
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    10
    
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    11
    def authenticate(self, request):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    12
        # get token, get username
6
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    13
        if 'act_as' not in request.GET or 'HTTP_RENKAN_ACT_AS' not in request.META:
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    14
            return
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    15
        else:
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    16
            username = request.GET.get('act_as', request.META.get("HTTP_RENKAN_ACT_AS", ""))
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    17
        try: 
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    18
            user = get_user_model().objects.get(username=username)
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    19
        except get_user_model().DoesNotExist:
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    20
            return
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    21
        if 'HTTP_AUTHORIZATION' not in request.META:
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    22
            return
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    23
        else:
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    24
            token = re.search("(?<=\s).*", request.META["HTTP_AUTHORIZATION"]).group(0)
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    25
        
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    26
        # send token to Oauth server
6
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    27
        token_validate_response = requests.get(
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    28
            settings.MTDC_VALIDATE_TOKEN_URL+token
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    29
        )
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    30
        if token_validate_response.status_code != 200:
39cecdd5260e Added OAuth2 Client Credentials Authentication workflow for Mtdc Application + Corrected mistakes on Authorization Code flow
durandn
parents: 1
diff changeset
    31
            return
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    32
        return (user, None)
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    33