-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJwtRequest.py
380 lines (306 loc) · 10.8 KB
/
JwtRequest.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from simplejson import dumps
import datetime
import traceback
import functools
from odoo import http
from odoo.http import request, Response
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from .middleware.MiddlewareData import MiddlewareData
from .middleware.MiddlewareException import MiddlewareException
from . import util
import logging
_logger = logging.getLogger(__name__)
class InvalidTokenException(Exception):
pass
class JwtRequest:
body = {}
method = 'get'
headers = {}
token = ''
data: MiddlewareData
# list of main middlewares
middleware_list = {}
# list of innate middlewares
innate = []
def __init__(self):
self.odoo_req = request
self.data = MiddlewareData()
def parse_request(self):
'''
This can only be called inside controller method.
Parse and store request info { method, body, headers, token }
'''
method = str(request.httprequest.method).lower()
try:
body = http.request.params
except Exception:
body = {}
headers = dict(list(request.httprequest.headers.items()))
token = ''
# checking headers
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']
if 'Authorization' in headers:
try:
# Bearer token_string
token = headers['Authorization'].split(' ')[1]
except Exception:
pass
self.method = method
self.headers = headers
self.body = body
self.token = token
def register_middleware(self, alias: str, handler):
'''
Register middleware handler.
Parameters
----------
`alias` : str
alias of handler, e.g., `jwt`
`handler` : callable(request Request)
a handler to be execute. Must have **kw
'''
self.middleware_list[alias] = handler
def middleware_always(self, handler):
self.innate.append(handler)
def _run_handler(self, handler, param=None, alias=''):
try:
if callable(handler):
handler(req=self, data=self.data, param=param)
except MiddlewareException as e:
_logger.warning(f'Middleware [{str(alias or handler)}]: {str(e)}')
message, code = e.build_response()
return self.response(data=message, status=code)
except Exception as e:
_logger.warning(f'Middleware-generic [{str(alias or handler)}]: {str(e)}')
# custom exception
if getattr(e, 'response'):
return e.response()
# bad request
return self.response({}, 400)
def _parse_handler(self, handler):
if callable(handler): return handler, None
if type(handler) is tuple:
h = self.middleware_list.get(handler[0])
p = handler[1] if len(handler) > 1 else None
return h, p
if type(handler) is str:
# look up middleware_list
h = self.middleware_list.get(handler)
if h and callable(h): return h, None
return None, None
def _requires_innate(self):
for m in self.innate:
handler, param = self._parse_handler(m)
if handler:
error = self._run_handler(handler, param)
if error: return error
def exec_middleware(self, alias):
handler, param = self._parse_handler(alias)
if handler:
handler(req=self, data=self.data, param=param)
def _requires_middlewares(self, *alias_list):
'''
Iterates each middleware to validate. Can only be called inside controller method.
@return Response if any validation errors. Else none.
'''
for alias in alias_list:
handler, param = self._parse_handler(alias)
if handler:
error = self._run_handler(handler, param, alias)
if error: return error
def _requires(self, *alias_list):
'''
Iterates each middleware to validate. Can only be called inside controller method.
@return Response if any validation errors. Else none.
'''
error = self._requires_innate()
if error: return error
error = self._requires_middlewares(*alias_list)
if error: return error
def start(self):
'''
init events
'''
self.end_events = []
self.parse_request()
def end(self, response=None):
'''
trigger events
'''
for handler in self.end_events:
if callable(handler):
handler(req=self, res=response)
def on_end(self, handler):
self.end_events.append(handler)
def middlewares(self, *alias_list):
'''
decorator for http controller
'''
def exec_http(handler):
@functools.wraps(handler)
def execute_all(*k, **kw):
self.start()
error = self._requires(*alias_list)
# middleware error response
if error:
self.end(error)
return error
# controller response
try:
response = handler(*k, **kw)
self.end(response)
return response
except Exception as e:
self.end(e)
raise e
return execute_all
return exec_http
def pure_middlewares(self, *alias_list):
'''
decorator for http controller, run middlewares without innate
'''
def exec_http(handler):
@functools.wraps(handler)
def execute_all(*k, **kw):
self.start()
error = self._requires_middlewares(*alias_list)
# middleware error response
if error:
self.end(error)
return error
# controller response
try:
response = handler(*k, **kw)
self.end(response)
return response
except Exception as e:
self.end(e)
raise e
return execute_all
return exec_http
def is_rpc(self):
return 'application/json' in request.httprequest._parsed_content_type
def is_ok_response(self, status=200):
return status >= 200 and status < 300
def http_response(self, data={}, status=200):
'''
Response normal http request (with controller type='http')
'''
return Response(dumps(data), status=status, headers=[
('Content-Type', 'application/json'),
])
def rpc_response(self, data={}, status=200):
'''
Response json rpc request (with controller type='json')
'''
r = {
'success': True if self.is_ok_response(status) else False,
'code': status,
}
if not self.is_ok_response(status):
return { **r, **data }
return { **r, 'data': data }
def response(self, data={}, status=200):
'''
Create a response to either http or rpc request
'''
if self.is_rpc():
return self.rpc_response(data, status)
return self.http_response(data, status)
def response_500(self, data={}):
return self.response(data, 500)
def response_404(self, data={}):
return self.response(data, 404)
def create_token(self, user):
'''
Create a token based on user model
'''
try:
exp = datetime.datetime.utcnow() + datetime.timedelta(days=30)
payload = {
'exp': exp,
'iat': datetime.datetime.utcnow(),
'sub': user.id,
'lgn': user.login,
}
token = util.sign_token(payload)
self.save_token(token, user.id, exp)
return token
except Exception as ex:
_logger.error(traceback.format_exc())
raise
def save_token(self, token, uid, exp):
'''Save token to database
'''
request.env['jwt_provider.access_token'].sudo().create({
'user_id': uid,
'expires': exp.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
'token': token,
})
def get_state(self):
'''
get database state
'''
return {
'd': request.session.db
}
def login(self, login, password, with_token=True):
'''
Try logging user in use their login & password.
If `with_token` is `True`, will create a jwt token and return it on success or `false` on failure
You can access the logged in user through `request.env.user`
'''
state = self.get_state()
uid = request.session.authenticate(state['d'], login, password)
if not uid:
return False
if with_token:
return self.create_token(request.env.user)
return True
def logout(self, token=''):
try:
request.session.logout()
if token:
request.env['jwt_provider.access_token'].sudo().search([
('token', '=', token)
]).unlink()
except:
pass
def cleanup(self):
# Clean up things after success request
# use logout here to make request as stateless as possible
request.session.logout()
def verify(self, token):
'''
Check if jwt token existed in db and is not expired
'''
record = request.env['jwt_provider.access_token'].sudo().search([
('token', '=', token)
])
if len(record) != 1 or record.is_expired:
return False
return record.user_id
def validate_token(self, token, auth=False):
'''
Validate a given jwt token.
Return True on success or raise exceptions on failure.
If auth=True, will also log user in with that token.
'''
# first token must be in our db
if not self.verify(token):
raise InvalidTokenException()
# decode token, will raise exceptions
payload = util.decode_token(token)
if auth:
# signature: https://github.com/odoo/odoo/blob/14.0/odoo/http.py#L987
uid = request.session.authenticate(
request.session.db, login=payload['lgn'], password=token)
if not uid:
raise InvalidTokenException()
return True
jwt_request = JwtRequest()