Skip to content

Commit a44fafd

Browse files
authored
Merge pull request #526 from leancloud/on-auth-data
feat: on_auth_data hook
2 parents af44bad + e089f1b commit a44fafd

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

leancloud/engine/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from .leanengine import register_cloud_func
3535
from .leanengine import register_on_bigquery
3636
from .leanengine import register_on_login
37+
from .leanengine import register_on_auth_data
3738
from .leanengine import register_on_verified
3839
from .leanengine import user
3940

@@ -121,6 +122,9 @@ def on_verified(self, *args, **kwargs):
121122
def on_login(self, *args, **kwargs):
122123
return register_on_login(self.app.cloud_codes, *args, **kwargs)
123124

125+
def on_auth_data(self, *args, **kwargs):
126+
return register_on_auth_data(self.app.cloud_codes, *args, **kwargs)
127+
124128
def on_bigquery(self, *args, **kwargs):
125129
warnings.warn(
126130
"on_bigquery is deprecated, please use on_insight instead",

leancloud/engine/leanengine.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def __init__(self, fetch_user):
135135
),
136136
Rule("/__engine/1.1/functions/_User/onLogin", endpoint="on_login"),
137137
Rule("/__engine/1/functions/_User/onLogin", endpoint="on_login"),
138+
Rule("/__engine/1.1/functions/_User/onAuthData", endpoint="on_auth_data"),
139+
Rule("/__engine/1/functions/_User/onAuthData", endpoint="on_auth_data"),
138140
Rule(
139141
"/__engine/1/functions/<class_name>/<hook_name>",
140142
endpoint="cloud_hook",
@@ -163,6 +165,8 @@ def __init__(self, fetch_user):
163165
Rule("/1.1/functions/BigQuery/<event>", endpoint="on_bigquery"),
164166
Rule("/1.1/functions/_User/onLogin", endpoint="on_login"),
165167
Rule("/1/functions/_User/onLogin", endpoint="on_login"),
168+
Rule("/1.1/functions/_User/onAuthData", endpoint="on_auth_data"),
169+
Rule("/1/functions/_User/onAuthData", endpoint="on_auth_data"),
166170
Rule("/1/functions/<class_name>/<hook_name>", endpoint="cloud_hook"),
167171
Rule("/1.1/functions/<class_name>/<hook_name>", endpoint="cloud_hook"),
168172
Rule("/1/functions/onVerified/<verify_type>", endpoint="on_verified"),
@@ -247,6 +251,10 @@ def dispatch_request(self, environ):
247251
result = {
248252
"result": dispatch_on_login(self.cloud_codes, app_params, **values)
249253
}
254+
elif endpoint == "on_auth_data":
255+
result = {
256+
"result": dispatch_on_auth_data(self.cloud_codes, app_params, **values)
257+
}
250258
elif endpoint == "ops_meta_data":
251259
from .authorization import MASTER_KEY
252260

@@ -508,6 +516,26 @@ def dispatch_on_login(_cloud_codes, app_params, params):
508516

509517
return func(user)
510518

519+
def register_on_auth_data(_cloud_codes, func):
520+
func_name = "__on_auth_data__User"
521+
522+
if func_name in _cloud_codes:
523+
raise RuntimeError("on authdata is already registered")
524+
_cloud_codes[func_name] = func
525+
526+
def dispatch_on_auth_data(_cloud_codes, app_params, params):
527+
from .authorization import HOOK_KEY
528+
current_hook_key = app_params.get("hook_key")
529+
if not current_hook_key or current_hook_key != HOOK_KEY:
530+
raise LeanEngineError(code=401, message="Unauthorized.")
531+
532+
func = _cloud_codes.get("__on_auth_data__User")
533+
if not func:
534+
return
535+
536+
auth_data = params["object"]
537+
return func(auth_data)
538+
511539

512540
def dispatch_ops_meta_data(_cloud_codes):
513541
return list(_cloud_codes.keys())

tests/test_engine.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,32 @@ def on_login(user):
500500
assert response.ok
501501

502502

503+
def test_on_auth_data(): # type: () -> None
504+
@engine.on_auth_data
505+
def on_auth_data(auth_data):
506+
if auth_data["foo"]["openid"] == "openid":
507+
auth_data["foo"]["uid"] = "openid"
508+
return auth_data
509+
510+
response = requests.post(
511+
url + "/__engine/1.1/functions/_User/onAuthData",
512+
json={"object": {
513+
"foo": {
514+
"openid": "openid",
515+
"access_token": "access_token",
516+
"expires_in": 123456789,
517+
}
518+
}},
519+
headers={
520+
"x-avoscloud-application-id": TEST_APP_ID,
521+
"x-avoscloud-application-key": TEST_APP_KEY,
522+
"x-lc-hook-key": TEST_HOOK_KEY,
523+
},
524+
)
525+
assert response.ok
526+
assert response.json()["result"]["foo"]["uid"] == "openid"
527+
528+
503529
def test_insight(): # type: () -> None
504530
@engine.on_insight("end")
505531
def on_insight_end(ok, data):

0 commit comments

Comments
 (0)