-
Notifications
You must be signed in to change notification settings - Fork 15
/
jwt_http.py
111 lines (89 loc) · 3.38 KB
/
jwt_http.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from odoo import http
from odoo.http import request, Response
from .validator import validator
import simplejson as json
return_fields = ['id', 'login', 'name', 'company_id']
class JwtHttp:
def get_state(self):
return {
'd': request.session.db
}
def parse_request(self):
http_method = request.httprequest.method
try:
body = http.request.params
except Exception:
body = {}
headers = dict(list(request.httprequest.headers.items()))
if 'wsgi.input' in headers:
del headers['wsgi.input']
if 'wsgi.errors' in headers:
del headers['wsgi.errors']
if 'HTTP_AUTHORIZATION' in headers:
headers['Authorization'] = headers['HTTP_AUTHORIZATION']
# extract token
token = ''
if 'Authorization' in headers:
try:
# Bearer token_string
token = headers['Authorization'].split(' ')[1]
except Exception:
pass
return http_method, body, headers, token
def date2str(self, d, f='%Y-%m-%d %H:%M:%S'):
"""
Convert datetime to string
:param self:
:param d: datetime object
:param f='%Y-%m-%d%H:%M:%S': string format
"""
try:
s = d.strftime(f)
except:
s = None
finally:
return s
def response(self, success=True, message=None, data=None, code=200):
"""
Create a HTTP Response for controller
:param success=True indicate this response is successful or not
:param message=None message string
:param data=None data to return
:param code=200 http status code
"""
payload = json.dumps({
'success': success,
'message': message,
'data': data,
})
return Response(payload, status=code, headers=[
('Content-Type', 'application/json'),
])
def response_500(self, message='Internal Server Error', data=None):
return self.response(success=False, message=message, data=data, code=500)
def response_404(self, message='404 Not Found', data=None):
return self.response(success=False, message=message, data=data, code=404)
def response_403(self, message='403 Forbidden', data=None):
return self.response(success=False, message=message, data=data, code=403)
def errcode(self, code, message=None):
return self.response(success=False, code=code, message=message)
def do_login(self, login, password):
# get current db
state = self.get_state()
uid = request.session.authenticate(state['d'], login, password)
if not uid:
return self.errcode(code=400, message='incorrect login')
# login success, generate token
user = request.env.user.read(return_fields)[0]
token = validator.create_token(user)
return self.response(data={ 'user': user, 'token': token })
def do_logout(self, token):
request.session.logout()
request.env['jwt_provider.access_token'].sudo().search([
('token', '=', token)
]).unlink()
def cleanup(self):
# Clean up things after success request
# use logout here to make request as stateless as possible
request.session.logout()
jwt_http = JwtHttp()