ore.api.external   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 32
dl 0
loc 48
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A OurApiKeyAuthentication.extract_credentials() 0 20 5
1
from . import common
2
3
import logging
4
from tastypie.authentication import ApiKeyAuthentication
5
from tastypie.models import ApiKey
6
7
logger = logging.getLogger('ore')
8
9
10
class OurApiKeyAuthentication(ApiKeyAuthentication):
11
12
    """
13
        Our own authenticator version does not demand the user name to be part of the auth header.
14
    """
15
16
    def extract_credentials(self, request):
17
        if request.META.get('HTTP_AUTHORIZATION') and request.META[
18
                'HTTP_AUTHORIZATION'].lower().startswith('apikey '):
19
            (auth_type, api_key) = request.META[
20
                'HTTP_AUTHORIZATION'].split(' ')
21
22
            if auth_type.lower() != 'apikey':
23
                logger.debug("Incorrect authorization header: " +
24
                             str(request.META['HTTP_AUTHORIZATION']))
25
                raise ValueError("Incorrect authorization header.")
26
            try:
27
                key = ApiKey.objects.get(key=api_key.strip())
28
            except Exception:
29
                logger.debug("Incorrect API key in header: " +
30
                             str(request.META['HTTP_AUTHORIZATION']))
31
                raise ValueError("Incorrect API key.")
32
            return key.user.username, api_key
33
        else:
34
            logger.debug("Missing authorization header: " + str(request.META))
35
            raise ValueError("Missing authorization header.")
36
37
38
class GraphResource(common.GraphResource):
39
40
    class Meta(common.GraphResource.Meta):
41
        authentication = OurApiKeyAuthentication()
42
43
44
class ProjectResource(common.ProjectResource):
45
46
    class Meta(common.ProjectResource.Meta):
47
        authentication = OurApiKeyAuthentication()
48