2016-09-01 00:45:44 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-08-19 06:20:37 +02:00
|
|
|
import hashlib
|
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
from django.contrib.auth.models import User, Permission
|
|
|
|
from kfet.models import Account, GenericTeamToken
|
2016-08-19 06:20:37 +02:00
|
|
|
|
2017-02-23 12:04:33 +01:00
|
|
|
|
2016-08-19 06:20:37 +02:00
|
|
|
class KFetBackend(object):
|
|
|
|
def authenticate(self, request):
|
2016-08-20 23:31:30 +02:00
|
|
|
password = request.POST.get('KFETPASSWORD', '')
|
|
|
|
password = request.META.get('HTTP_KFETPASSWORD', password)
|
2016-08-19 06:20:37 +02:00
|
|
|
if not password:
|
|
|
|
return None
|
|
|
|
|
2017-02-23 12:04:33 +01:00
|
|
|
password_sha256 = hashlib.sha256(password.encode('utf-8')).hexdigest()
|
2016-08-19 06:20:37 +02:00
|
|
|
try:
|
2016-09-03 02:54:02 +02:00
|
|
|
account = Account.objects.get(password=password_sha256)
|
2017-02-10 22:58:01 +01:00
|
|
|
user = account.profile.user
|
2016-08-19 06:20:37 +02:00
|
|
|
except Account.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return user
|
2016-08-20 19:35:45 +02:00
|
|
|
|
2017-02-23 12:04:33 +01:00
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
class GenericTeamBackend(object):
|
2017-02-23 12:04:33 +01:00
|
|
|
"""
|
|
|
|
Authenticate using the generic_team user.
|
|
|
|
"""
|
2016-08-20 19:35:45 +02:00
|
|
|
def authenticate(self, username=None, token=None):
|
|
|
|
valid_token = GenericTeamToken.objects.get(token=token)
|
|
|
|
if username == 'kfet_genericteam' and valid_token:
|
2016-09-29 21:36:17 +02:00
|
|
|
# Création du user s'il n'existe pas déjà
|
|
|
|
user, _ = User.objects.get_or_create(username='kfet_genericteam')
|
|
|
|
account, _ = Account.objects.get_or_create(
|
2017-02-23 12:04:33 +01:00
|
|
|
profile=user.profile,
|
|
|
|
trigramme='GNR'
|
|
|
|
)
|
2016-09-29 21:36:17 +02:00
|
|
|
|
|
|
|
# Ajoute la permission kfet.is_team à ce user
|
2016-08-20 19:35:45 +02:00
|
|
|
perm_is_team = Permission.objects.get(codename='is_team')
|
|
|
|
user.user_permissions.add(perm_is_team)
|
2016-09-29 21:36:17 +02:00
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
return user
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_user(self, user_id):
|
|
|
|
try:
|
|
|
|
return User.objects.get(pk=user_id)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|