backend.py 3.33 KB
Newer Older
Sharad Heft's avatar
Sharad Heft committed
1 2 3 4 5 6
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.auth.backends import ModelBackend
from django.conf import settings
from django.db import transaction

7
from test_oauth import verifiers
8
from test_oauth.models import TESTOAuth2Data, Character
9
from test_oauth.session import TESTOAuth2Session
10 11


12 13 14 15 16
DEFAULT_VERIFIERS = [
    verifiers.TESTMembershipVerifier()
]


Sharad Heft's avatar
Sharad Heft committed
17
class TESTOAuth2Backend(ModelBackend):
18 19
    def authenticate(self, token=None, **kwargs):
        if token is None:
Sharad Heft's avatar
Sharad Heft committed
20 21
            return None

22 23
        profile = TESTOAuth2Session(token=token).profile

24 25
        self.run_verifiers(profile)

26
        if getattr(settings, 'TEST_OAUTH_RESPECT_ID', False):
Gehmis's avatar
Gehmis committed
27
            user, _ = get_user_model().objects.update_or_create(
28
                pk=profile['id'],
Gehmis's avatar
Gehmis committed
29 30 31
                defaults={
                    'username': profile['username']
                }
32 33 34 35 36
            )
        else:
            user, _ = get_user_model().objects.get_or_create(
                username=profile['username']
            )
Sharad Heft's avatar
Sharad Heft committed
37

38 39 40 41 42
        data, _ = TESTOAuth2Data.objects.update_or_create(
            user=user,
            defaults={
                'access_token': token['access_token'],
                'refresh_token': token['refresh_token'],
43
                'auth_id': profile['id']
44 45 46
            }
        )

47
        if getattr(settings, 'TEST_OAUTH_CREATE_GROUPS', False):
48 49 50 51 52 53 54 55 56 57 58
            self._do_create_groups(
                user,
                profile.get('groups', [])
            )

        if getattr(settings, 'TEST_OAUTH_CREATE_CHARACTERS', False):
            self._do_create_characters(
                user,
                profile.get('characters', []),
                profile.get('primary_character', None),
            )
Sharad Heft's avatar
Sharad Heft committed
59 60 61

        return user

62 63 64 65 66 67 68 69 70 71
    def run_verifiers(self, profile):
        verifiers = getattr(
            settings,
            'TEST_OAUTH_VERIFIERS',
            DEFAULT_VERIFIERS
        )

        for v in verifiers:
            v(**profile)

Sharad Heft's avatar
Sharad Heft committed
72
    def get_user(self, user_id):
73 74
        UserModel = get_user_model()

Sharad Heft's avatar
Sharad Heft committed
75 76 77 78
        try:
            return UserModel.objects.get(pk=user_id)
        except UserModel.DoesNotExist:
            return None
79 80 81 82 83 84

    @transaction.atomic
    def _do_create_groups(self, user, groups):
        user.groups.clear()

        for g in groups:
Gehmis's avatar
Gehmis committed
85 86 87 88 89
            group, _ = Group.objects.update_or_create(
                id=g['id'],
                defaults={
                    'name': g['name']
                }
90 91 92 93 94 95 96
            )

            user.groups.add(group)

            # Adding carveout for Server Admin group
            if g['name'] == "Server Admin":
                user.is_staff = True
ozzeh's avatar
ozzeh committed
97
                user.is_superuser = True
98 99

        user.save()
100 101 102 103 104 105 106

    @transaction.atomic
    def _do_create_characters(self, user, characters, primary):
        user.auth.characters.clear()
        user.auth.primary_character = None

        for c in characters:
107
            character, _ = Character.objects.update_or_create(
108 109 110
                id=c['id'],
                defaults={
                    'name': c['name'],
111
                    'alliance_id': (c['alliance'] or {'id': None})['id'],
112 113 114 115 116 117 118 119
                    'corporation_id': c['corporation']['id']
                }
            )

            user.auth.characters.add(character)

        user.auth.primary_character_id = primary['id']
        user.auth.save()