-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackends.py
45 lines (38 loc) · 1.28 KB
/
backends.py
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
44
45
from .api import EjabberdAPI
from .models import EjabberdAccount
from .utils import int_to_token
from virtualhost.models import User
class EjabberdAPIBackend(object):
def authenticate(self, api=None, **kwargs):
if api is None:
return None
return EjabberdAccount(api) if api.authorized else None
def get_user(self, hash_token):
try:
token = int_to_token(hash_token)
except Exception as e:
return None
api = EjabberdAPI()
api.fetch_token(token)
try:
return EjabberdAccount(api)
except KeyError:
return None
class DjangoUserBackend(object):
def authenticate(self, username, password, **kwargs):
username, host = username.split('@')
try:
user = User.objects.get(username=username, host=host)
except User.DoesNotExist:
return None
if user.is_admin and user.check_password(password):
api = EjabberdAPI()
return EjabberdAccount(api, user=user)
return None
def get_user(self, user_id):
try:
user = User.objects.get(id=user_id)
api = EjabberdAPI()
return EjabberdAccount(api, user=user)
except User.DoesNotExist:
return None