-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.py
executable file
·52 lines (42 loc) · 1.93 KB
/
middleware.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
# -*- coding: utf-8 -*-
import logging
from django.http import HttpResponseForbidden, HttpResponseNotAllowed
from back_emedido.apps.auth_token.models import AuthToken
from django.http import JsonResponse, HttpResponse
from datetime import datetime
from django.urls import reverse
def TokenValidMiddleware(get_response):
# One-time configuration and initialization.
def middleware(request):
# Code to be executed for each request before
# the view (and later middleware) are called.
# Login is not chequed
# With the two lines after the if, we make it continue to next middleware.
if request.path.startswith(reverse('authenticate')):
response = get_response(request)
return response
#return None
if request.path.startswith(reverse('resetn')):
response = get_response(request)
return response
# For every incoming requests, we will check if it was a valid token
ret = {'status':'ERR_TOKEN', 'message': 'Token inválido'}
access_token = request.META.get('HTTP_TOKEN', '') or ''
operator = request.META.get('HTTP_OPERATOR', '') or ''
if not access_token or not operator:
ret['message'] = "Credenciales del request inválidas"
return JsonResponse(ret)
authobj = AuthToken.objects.filter(token=access_token).first()
if authobj:
# If date is after now, it is expired
if authobj.expiration < datetime.now():
# logger.exception("RequestToken make by operator %s expired:
# %s", operator, access_token)
return JsonResponse(ret)
else:
#logger.exception("RequestToken cannot be decoded: %s", token)
return JsonResponse(ret)
# If everythig ok, we continue with the chain of middlewares
response = get_response(request)
return response
return middleware