-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrap.py
More file actions
30 lines (23 loc) · 873 Bytes
/
rap.py
File metadata and controls
30 lines (23 loc) · 873 Bytes
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
from functools import wraps
from django.http import JsonResponse
from controller import config
def get_backends_for_client_token(view_fn):
"""
Ensure a valid client authentication token was received
and pass the allowed backends to the wrapped view function
"""
@wraps(view_fn)
def wrapped_view(request, *args, **kwargs):
token = request.headers.get("Authorization")
error = None
if not token:
error = "No token provided"
else:
token_backends = config.CLIENT_TOKENS.get(token)
if token_backends is None:
error = "Invalid token"
if error:
return JsonResponse({"error": "Unauthorized", "details": error}, status=401)
kwargs["token_backends"] = token_backends
return view_fn(request, *args, **kwargs)
return wrapped_view