-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathleanengine.py
More file actions
386 lines (304 loc) · 14.3 KB
/
Copy pathleanengine.py
File metadata and controls
386 lines (304 loc) · 14.3 KB
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
381
382
383
384
385
386
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import functools
import json
import logging
import sys
import traceback
import six
from werkzeug.exceptions import HTTPException
from werkzeug.exceptions import NotAcceptable
from werkzeug.routing import Map
from werkzeug.routing import Rule
from werkzeug.wrappers import Response
import leancloud
from . import context
__author__ = 'asaka <lan@leancloud.rocks>'
logger = logging.getLogger('leancloud.cloudcode.cloudcode')
user = context.local('user')
current = context.local('current')
class LeanEngineError(Exception):
def __init__(self, code=400, message='error'):
if isinstance(code, six.string_types):
message = code
code = 400
self.code = code
self.message = message
class LeanEngineApplication(object):
def __init__(self, fetch_user):
self.fetch_user = fetch_user
self.url_map = Map([
Rule('/__engine/1/functions/<func_name>', endpoint='cloud_function'),
Rule('/__engine/1.1/functions/<func_name>', endpoint='cloud_function'),
Rule('/__engine/1/call/<func_name>', endpoint='rpc_function'),
Rule('/__engine/1.1/call/<func_name>', endpoint='rpc_function'),
Rule('/__engine/1/functions/BigQuery/<event>', endpoint='on_bigquery'),
Rule('/__engine/1.1/functions/BigQuery/<event>', endpoint='on_bigquery'),
Rule('/__engine/1.1/functions/_User/onLogin', endpoint='on_login'),
Rule('/__engine/1/functions/_User/onLogin', endpoint='on_login'),
Rule('/__engine/1/functions/<class_name>/<hook_name>', endpoint='cloud_hook'),
Rule('/__engine/1.1/functions/<class_name>/<hook_name>', endpoint='cloud_hook'),
Rule('/__engine/1/functions/onVerified/<verify_type>', endpoint='on_verified'),
Rule('/__engine/1.1/functions/onVerified/<verify_type>', endpoint='on_verified'),
Rule('/__engine/1/functions/_ops/metadatas', endpoint='ops_meta_data'),
Rule('/__engine/1.1/functions/_ops/metadatas', endpoint='ops_meta_data'),
Rule('/1/functions/<func_name>', endpoint='cloud_function'),
Rule('/1.1/functions/<func_name>', endpoint='cloud_function'),
Rule('/1/call/<func_name>', endpoint='rpc_function'),
Rule('/1.1/call/<func_name>', endpoint='rpc_function'),
Rule('/1/functions/BigQuery/<event>', endpoint='on_bigquery'),
Rule('/1.1/functions/BigQuery/<event>', endpoint='on_bigquery'),
Rule('/1.1/functions/_User/onLogin', endpoint='on_login'),
Rule('/1/functions/_User/onLogin', endpoint='on_login'),
Rule('/1/functions/<class_name>/<hook_name>', endpoint='cloud_hook'),
Rule('/1.1/functions/<class_name>/<hook_name>', endpoint='cloud_hook'),
Rule('/1/functions/onVerified/<verify_type>', endpoint='on_verified'),
Rule('/1.1/functions/onVerified/<verify_type>', endpoint='on_verified'),
Rule('/1/functions/_ops/metadatas', endpoint='ops_meta_data'),
Rule('/1.1/functions/_ops/metadatas', endpoint='ops_meta_data'),
])
self.cloud_codes = {}
def __call__(self, environ, start_response):
self.process_session(environ)
response = self.dispatch_request(environ)
return response(environ, start_response)
def process_session(self, environ):
request = environ['leanengine.request']
context.local.current = context.Current()
context.local.current.meta = {
'remote_address': get_remote_address(request),
}
if environ['_app_params']['session_token'] not in (None, ''):
session_token = environ['_app_params']['session_token']
if self.fetch_user:
user = leancloud.User.become(session_token)
context.local.current.user = user
context.local.user = user
context.local.current.session_token = session_token
return
try:
data = json.loads(request.get_data(as_text=True))
except ValueError:
context.local.user = None
return
if 'user' in data and data['user']:
user = leancloud.User()
user._update_data(data['user'])
context.local.current.user = user
context.local.current.session_token = user.get_session_token()
context.local.user = user
return
context.local.user = None
def dispatch_request(self, environ):
request = environ['leanengine.request']
app_params = environ['_app_params']
adapter = self.url_map.bind_to_environ(request.environ)
try:
endpoint, values = adapter.match()
except HTTPException as e:
return e
params = request.get_data(as_text=True)
values['params'] = json.loads(params) if params != '' else {}
try:
if endpoint == 'cloud_function':
result = {'result': dispatch_cloud_func(self.cloud_codes, app_params, decode_object=False, **values)}
elif endpoint == 'rpc_function':
result = {'result': dispatch_cloud_func(self.cloud_codes, app_params, decode_object=True, **values)}
elif endpoint == 'cloud_hook':
result = dispatch_cloud_hook(self.cloud_codes, app_params, **values)
elif endpoint == 'on_verified':
result = {'result': dispatch_on_verified(self.cloud_codes, app_params, **values)}
elif endpoint == 'on_login':
result = {'result': dispatch_on_login(self.cloud_codes, app_params, **values)}
elif endpoint == 'ops_meta_data':
from .authorization import MASTER_KEY
if request.environ.get('_app_params', {}).get('master_key') != MASTER_KEY:
raise LeanEngineError(code=401, message='Unauthorized.')
result = {'result': dispatch_ops_meta_data(self.cloud_codes)}
elif endpoint == 'on_bigquery':
result = {'result': dispatch_on_bigquery(self.cloud_codes, app_params, **values)}
else:
raise ValueError # impossible
return Response(json.dumps(result), mimetype='application/json')
except LeanEngineError as e:
return Response(
json.dumps({'code': e.code, 'error': e.message}),
status=e.code if e.code else 400,
mimetype='application/json'
)
except Exception:
print(traceback.format_exc(), file=sys.stderr)
return Response(
json.dumps({'code': 141, 'error': 'Cloud Code script had an error.'}),
status=500,
mimetype='application/json'
)
def update_cloud_codes(self, engine_cloud_codes):
already_register_func_name = set(self.cloud_codes.keys()).intersection(set(engine_cloud_codes.keys()))
if already_register_func_name:
is_are = "is" if len(already_register_func_name) == 1 else "are"
raise RuntimeError("cloud function: {0} {1} already registerd.".format(",".join(already_register_func_name), is_are))
self.cloud_codes.update(engine_cloud_codes)
hook_name_mapping = {
'beforeSave': '__before_save_for_',
'afterSave': '__after_save_for_',
'beforeUpdate': '__before_update_for_',
'afterUpdate': '__after_update_for_',
'beforeDelete': '__before_delete_for_',
'afterDelete': '__after_delete_for_',
}
root_engine = None
def register_cloud_func(_cloud_codes, func_or_func_name):
if isinstance(func_or_func_name, six.string_types):
func_name = func_or_func_name
def inner_func(func):
if func_name in _cloud_codes:
raise RuntimeError('cloud function: {0} is already registered'.format(func_name))
_cloud_codes[func_name] = func
return func
return inner_func
func = func_or_func_name
func_name = func.__name__
if func_name in _cloud_codes:
raise RuntimeError('cloud function: {0} is already registered'.format(func_name))
_cloud_codes[func_name] = func
return func
def dispatch_cloud_func(_cloud_codes, app_params, func_name, decode_object, params):
# let's check realtime hook sign first
realtime_hook_funcs = [
'_messageReceived', '_receiversOffline', '_messageSent', '_conversationStart', '_conversationStarted',
'_conversationAdd', '_conversationRemove', '_conversationUpdate'
]
from .authorization import HOOK_KEY
if func_name in realtime_hook_funcs:
current_hook_key = app_params.get('hook_key')
if not current_hook_key or current_hook_key != HOOK_KEY:
raise LeanEngineError(code=401, message='Unauthorized.')
# delete all keys in params which starts with low dash.
# JS SDK may send it's app info with them.
params = {k: v for k, v in params.items() if (not k.startswith('_')) or k == '__type'}
if decode_object:
params = leancloud.utils.decode('', params)
func = _cloud_codes.get(func_name)
if not func:
raise LeanEngineError(code=404, message="cloud func named '{0}' not found.".format(func_name))
logger.info("{0} is called!".format(func_name))
result = func(**params)
if decode_object:
result = leancloud.utils.encode(result, dump_objects=True)
return result
def register_cloud_hook(_cloud_codes, class_name, hook_name):
# hack the hook name
hook_name = hook_name_mapping[hook_name] + class_name
if hook_name in _cloud_codes:
raise RuntimeError('cloud hook {0} on class {1} is already registered'.format(hook_name, class_name))
def new_func(func):
_cloud_codes[hook_name] = func
return new_func
before_save = functools.partial(register_cloud_hook, hook_name='beforeSave')
after_save = functools.partial(register_cloud_hook, hook_name='afterSave')
before_update = functools.partial(register_cloud_hook, hook_name='beforeUpdate')
after_update = functools.partial(register_cloud_hook, hook_name='afterUpdate')
before_delete = functools.partial(register_cloud_hook, hook_name='beforeDelete')
after_delete = functools.partial(register_cloud_hook, hook_name='afterDelete')
def dispatch_cloud_hook(_cloud_codes, app_params, class_name, hook_name, params):
from .authorization import HOOK_KEY
current_hook_key = app_params.get('hook_key')
if not current_hook_key or current_hook_key != HOOK_KEY:
raise LeanEngineError(code=401, message='Unauthorized.')
hook_name = hook_name_mapping[hook_name] + class_name
if hook_name not in _cloud_codes:
raise NotAcceptable
obj = leancloud.Object.create(class_name)
obj._update_data(params['object'])
if '__updateKeys' in params['object']:
obj.updated_keys = params['object']['__updateKeys']
if hook_name.startswith('__before'):
if obj.has('__before'):
obj.set('__before', obj.get('__before'))
else:
obj.disable_before_hook()
elif hook_name.startswith('__after'):
if obj.has('__after'):
obj.set('__after', obj.get('__after'))
else:
obj.disable_after_hook()
logger.info("{0}:{1} is called!".format(class_name, hook_name))
func = _cloud_codes[hook_name]
if not func:
raise leancloud.LeanEngineError(code=404, message="cloud hook named '{0}' not found.".format(hook_name))
func(obj)
if hook_name.startswith('__after'):
return {'result': 'ok'}
elif hook_name.startswith('__before_delete_for'):
return {}
else:
return obj.dump()
def register_on_verified(_cloud_codes, verify_type):
if verify_type not in set(['sms', 'email']):
raise RuntimeError('verify_type must be sms or email')
func_name = '__on_verified_{0}'.format(verify_type)
def new_func(func):
if func_name in _cloud_codes:
raise RuntimeError('on verified is already registered')
_cloud_codes[func_name] = func
return new_func
def dispatch_on_verified(_cloud_codes, app_params, verify_type, params):
func_name = '__on_verified_' + verify_type
from .authorization import HOOK_KEY
hook_key = app_params.get('hook_key')
if not hook_key or hook_key != HOOK_KEY:
raise LeanEngineError(code=401, message='Unauthorized.')
user = leancloud.User()
user._update_data(params['object'])
func = _cloud_codes.get(func_name)
if not func:
return
return func(user)
def register_on_login(_cloud_codes, func):
func_name = '__on_login__User'
if func_name in _cloud_codes:
raise RuntimeError('on login is already registered')
_cloud_codes[func_name] = func
def dispatch_on_login(_cloud_codes, app_params, params):
from .authorization import HOOK_KEY
current_hook_key = app_params.get('hook_key')
if not current_hook_key or current_hook_key != HOOK_KEY:
raise LeanEngineError(code=401, message='Unauthorized.')
func = _cloud_codes.get('__on_login__User')
if not func:
return
user = leancloud.User()
user._update_data(params['object'])
return func(user)
def dispatch_ops_meta_data(_cloud_codes):
return list(_cloud_codes.keys())
def register_on_bigquery(_cloud_codes, event):
if event == 'end':
func_name = '__on_complete_bigquery_job'
else:
raise RuntimeError('event not support')
def inner_func(func):
if func_name in _cloud_codes:
raise RuntimeError('on bigquery is already registered')
_cloud_codes[func_name] = func
return inner_func
def dispatch_on_bigquery(_cloud_codes, app_params, event, params):
if event == 'onComplete':
func_name = '__on_complete_bigquery_job'
else:
return
from .authorization import HOOK_KEY
current_hook_key = app_params.get('hook_key')
if not current_hook_key or current_hook_key != HOOK_KEY:
raise LeanEngineError(code=401, message='Unauthorized.')
func = _cloud_codes.get(func_name)
if not func:
return
ok = True if params['status'] == 'OK' else False
return func(ok, params)
def get_remote_address(request):
return request.headers.get('x-real-ip') or request.headers.get('x-forwarded-for') or request.remote_addr