-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpermissions.py
45 lines (36 loc) · 1.69 KB
/
permissions.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 rest_framework import authentication, permissions
from Node.models import Node
import base64
class AccessPermission(permissions.BasePermission):
def has_permission(self, request, view):
auth_header = request.META.get('HTTP_AUTHORIZATION', '')
token_type, _, credentials = auth_header.partition(' ')
decoded_credentials = base64.b64decode(credentials).decode("utf-8").split(':')
try:
node = Node.objects.get(id=decoded_credentials[0])
stored = f'{node.id}:{node.password}'
expected = base64.b64encode(bytes(stored, 'UTF-8')).decode()
except:
expected = base64.b64encode(b':').decode()
if token_type == 'Basic' and credentials == expected:
return True
else:
return False
class CustomAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
auth_header = request.META.get('HTTP_AUTHORIZATION', '')
token_type, _, credentials = auth_header.partition(' ')
decoded_credentials = base64.b64decode(credentials).decode("utf-8").split(':')
try:
node = Node.objects.get(id=decoded_credentials[0])
stored = f'{node.id}:{node.password}'
expected = base64.b64encode(bytes(stored, 'UTF-8')).decode()
except:
expected = base64.b64encode(b':').decode()
# expected = base64.b64encode(b'socialdistribution_t14:c404t14').decode()
if token_type == 'Basic' and credentials == expected:
return (True, None)
else:
return None
def authenticate_header(self, request):
return '{"username" : <username>, "password" : <password>}'