-
Notifications
You must be signed in to change notification settings - Fork 105
/
utils.py
74 lines (59 loc) · 2.09 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import warnings
from django.core import signing
from django.contrib.auth import get_user_model
from django.conf import settings as django_settings
from django.core.signing import BadSignature
from .exceptions import TokenScopeError
warnings.simplefilter("once")
def get_token(user, action, **kwargs):
username = user.get_username()
if hasattr(username, "pk"):
username = username.pk
payload = {user.USERNAME_FIELD: username, "action": action}
if kwargs:
payload.update(**kwargs)
token = signing.dumps(payload)
return token
def get_token_payload(token, action, exp=None):
payload = signing.loads(token, max_age=exp)
_action = payload.pop("action")
if _action != action:
raise TokenScopeError
return payload
def get_token_paylod(token, action, exp=None):
warnings.warn("get_token_paylod is deprecated, use get_token_payload instead", DeprecationWarning, stacklevel=2)
return get_token_payload(token, action, exp)
def using_refresh_tokens():
if (
hasattr(django_settings, "GRAPHQL_JWT")
and django_settings.GRAPHQL_JWT.get("JWT_LONG_RUNNING_REFRESH_TOKEN", False)
and "graphql_jwt.refresh_token.apps.RefreshTokenConfig"
in django_settings.INSTALLED_APPS
):
return True
return False
def revoke_user_refresh_token(user):
if using_refresh_tokens():
refresh_tokens = user.refresh_tokens.all()
for refresh_token in refresh_tokens:
try:
refresh_token.revoke()
except Exception: # JSONWebTokenError
pass
def flat_dict(dict_or_list):
"""
if is dict, return list of dict keys,
if is list, return the list
"""
return list(dict_or_list.keys()) if isinstance(dict_or_list, dict) else dict_or_list
def normalize_fields(dict_or_list, extra_list):
"""
helper merge settings defined fileds and
other fields on mutations
"""
if isinstance(dict_or_list, dict):
for i in extra_list:
dict_or_list[i] = "String"
return dict_or_list
else:
return dict_or_list + extra_list