backends.py
1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from floraconcierge.apiauth.models import User
from floraconcierge.errors import ResultDefaultError
from floraconcierge.shortcuts import get_apiclient
from floraconcierge.shortcuts.users import login, get_logged_user, social_login
class FloraConciergeBackend(ModelBackend):
def authenticate(self, username=None, password=None, **kwargs):
email = None
try:
validate_email(username)
email = username
except ValidationError:
pass
if email:
try:
return User.from_api_user(login(username, password))
except ResultDefaultError, e:
raise ValidationError(e.message)
def get_user(self, user_id):
try:
if not get_apiclient().env.user_auth_key:
return None
user = get_logged_user()
if user:
return User.from_api_user(user)
except ResultDefaultError:
return None
class FloraConciergeOauthBackend(FloraConciergeBackend):
def authenticate(self, flora_token=None, **kwargs):
try:
return User.from_api_user(social_login(flora_token))
except ResultDefaultError, e:
raise ValidationError(e.message)