-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
48 lines (39 loc) · 1.23 KB
/
utils.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
46
47
48
from django.contrib.auth.models import User
# Utilities for API Application
def get_auth_token_header(request):
"""
The token that is going to be provided is going to be stored within
this area of the request
:param request:
:return:
"""
return request.headers._store.get("authorization")
def token_exists(token_to_validate):
"""
Returns if the token exists or not
:param token_to_validate:
:return: bool
"""
try:
User.objects.get(auth_token=token_to_validate)
return True
except:
return False
def get_user_object_by_token(request):
# Token Header tuple, we should grab the provided token string
key, token_string = get_auth_token_header(request)
token = token_string.replace("Token ", "")
user_obj = User.objects.get(auth_token=token)
return user_obj
def get_user_object_by_token_or_auth(request):
"""
We'd like to have a function that will check try to return the user object
by auth or by a token.
Currently, not handling the API request cases without a token and without and auth
:param request:
:return:
"""
try:
return get_user_object_by_token(request)
except:
return request.user