-
Notifications
You must be signed in to change notification settings - Fork 15
/
views.py
51 lines (34 loc) · 1.6 KB
/
views.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
import urllib.parse
from django.shortcuts import redirect
from django.http import request, JsonResponse
from django.conf import settings
from django.contrib.auth import logout
from .auth import OXSessionDecryptor
def login(request):
url = "/accounts/login/"
next = request.GET.get("next", None)
if next:
url = "/accounts/login/?r={}".format(urllib.parse.quote(next))
return redirect(url)
def get_user_data(request):
cookie = request.COOKIES.get(settings.COOKIE_NAME, None)
if not cookie:
return JsonResponse({"logged_in": False, "cookie": False, "validation": False, "decryption": False})
decrypt = OXSessionDecryptor(secret_key_base=settings.SHARED_SECRET, encrypted_cookie_salt=settings.ENCRYPTED_COOKIE_SALT, encrypted_signed_cookie_salt=settings.SIGNED_ENCRYPTED_COOKIE_SALT)
#validate = decrypt.validate_cookie(cookie)
# TODO: Fix Validation. Currently, does not validate cookie unless the logged in user is id=1
#if not validate:
# return JsonResponse({"logged_in": False, "cookie": True, "validation": False, "decryption": False})
decrypted_user = decrypt.get_cookie_data(cookie)
if not decrypted_user:
return JsonResponse({"logged_in": False, "cookie": True, "validation": True, "decryption": False})
try:
return JsonResponse(decrypted_user.decode())
except AttributeError:
return JsonResponse(decrypted_user)
def logout(request):
url = "/accounts/logout/"
next = request.GET.get("next", None)
if next:
url = "/accounts/logout/?r={}".format(urllib.parse.quote(next))
return redirect(url)