Skip to content

Commit

Permalink
Merge pull request #1389 from longguikeji/feature-863
Browse files Browse the repository at this point in the history
Feature 863
  • Loading branch information
fanhe-lg authored Nov 7, 2022
2 parents a101ad6 + 5eba63b commit 9227934
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
7 changes: 4 additions & 3 deletions api/v1/views/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from email import header
from arkid.core.event import register_event, dispatch_event, Event
from arkid.core.event import register_event, dispatch_event, Event, BEFORE_REFRESH_TOKEN
from arkid.core.api import api, operation
from arkid.core.translation import gettext_default as _
from arkid.core.token import refresh_token
Expand All @@ -12,15 +12,16 @@
@operation(AuthOut, use_id=True)
def auth(request, tenant_id: str, event_tag: str, data: AuthIn):
tenant = request.tenant
request_id = request.META.get('request_id')
request_id = request.META.get('HTTP_REQUEST_ID')

# 认证
responses = dispatch_event(Event(tag=event_tag, tenant=tenant, request=request, uuid=request_id))
if not responses:
return {'error': 'error_code', 'message': '认证插件未启用'}

useless, (user, useless) = responses[0]

#befrore_refresh_token
dispatch_event(Event(tag=BEFORE_REFRESH_TOKEN, tenant=tenant, request=request, uuid=request_id, data={'user':user}))
# 生成 token
token = refresh_token(user)
dispatch_task.delay('async_get_arkstore_access_token', tenant.id.hex, token)
Expand Down
8 changes: 4 additions & 4 deletions arkid/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def refresh_token_active_date(token):
class ArkidApi(NinjaAPI):
def create_response(self, request, *args, **kwargs):
response = super().create_response(request, *args, **kwargs)
if request.META.get('request_id'):
response.headers['request_id'] = request.META.get('request_id')
if request.META.get('HTTP_REQUEST_ID'):
response.headers['request_id'] = request.META.get('HTTP_REQUEST_ID')
return response


Expand Down Expand Up @@ -222,10 +222,10 @@ def replace_view_func(operation):

old_view_func = operation.view_func
def func(request, *params, **kwargs):
request_id = request.META.get('request_id')
request_id = request.META.get('HTTP_REQUEST_ID')
if not request_id and use_id:
request_id = uuid.uuid4().hex
request.META['request_id'] = request_id
request.META['HTTP_REQUEST_ID'] = request_id

dispatch_event(Event(tag+'_pre', request.tenant, request=request, uuid=request_id))
response = old_view_func(request=request, *params, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions arkid/core/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ErrorCode(Enum):
PERMISSION_GROUP_NOT_DELETE = ('10038', _('the permission group not delete', '该分组权限不允许删除'))
SYSTEM_PERMISSION_NOT_OPERATION = ('10039', _('system permission not operation', '系统权限不支持此操作'))
PERMISSION_NOT_BELONG_TO_TENANT = ('10040', _('permission not belong to tenant', '该应用不属于该租户'))
OTP_2FA_REQUIRED = ('10050', _('user require otp two-factor authentication', '该用户需要OTP双因素认证'))

# SMS_PROVIDER_IS_MISSING = '11001'
# AUTHCODE_PROVIDER_IS_MISSING = '11002'
Expand Down
29 changes: 23 additions & 6 deletions arkid/core/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import json
from django.core.serializers.json import DjangoJSONEncoder
from types import SimpleNamespace
event_id_map = {}
from django.core.cache import cache
import pickle
import codecs

# event_id_map = {}


def send_event_through_webhook(event):
Expand Down Expand Up @@ -245,17 +249,28 @@ def signal_func(event, **kwargs2):

def remove_event_id(event):
if event.uuid:
event_id_map.pop(event.uuid, None)
# event_id_map.pop(event.uuid, None)
cache.delete(f'event:{event.uuid}')


def listen_event(tag, func, listener=None, **kwargs):
def signal_func(sender, event, **kwargs2):
if event.uuid and event_id_map.get(event.uuid, {}).get(func):
return event_id_map.get(event.uuid, {}).get(func), listener

# if event.uuid and event_id_map.get(event.uuid, {}).get(func):
# return event_id_map.get(event.uuid, {}).get(func), listener
func_id = str(id(func))
if event.uuid and cache.get(f'event:{event.uuid}', {}).get(func_id):
pickled_res = cache.get(f'event:{event.uuid}').get(func_id)
unpickled = pickle.loads(codecs.decode(pickled_res.encode(), "base64"))

return unpickled, listener
res = func(sender=sender, event=event, **kwargs2)

if event.uuid:
event_id_map[event.uuid] = {func: res}
# event_id_map[event.uuid] = {func: res}
pickled_res = codecs.encode(pickle.dumps(res), "base64").decode()
cached = cache.get(f'event:{event.uuid}', {})
cached[func_id] = pickled_res
cache.set(f'event:{event.uuid}', cached)
return res, listener

if isinstance(tag, (list, tuple)):
Expand Down Expand Up @@ -342,6 +357,7 @@ def unlisten_event(tag, func, **kwargs):
BEFORE_AUTH = 'BEFORE_AUTH'
AUTH_SUCCESS = 'AUTH_SUCCESS'
AUTH_FAIL = 'AUTH_FAIL'
BEFORE_REFRESH_TOKEN = 'BEFORE_REFRESH_TOKEN'

CREATE_ACCOUNT_LIFE_CONFIG = 'CREATE_ACCOUNT_LIFE_CONFIG'
UPDATE_ACCOUNT_LIFE_CONFIG = 'UPDATE_ACCOUNT_LIFE_CONFIG'
Expand Down Expand Up @@ -414,6 +430,7 @@ def unlisten_event(tag, func, **kwargs):
register_event(BEFORE_AUTH, _('before_auth', '认证前'))
register_event(AUTH_SUCCESS, _('auth success', '认证成功'))
register_event(AUTH_FAIL, _('auth fail', '认证失败'))
register_event(BEFORE_REFRESH_TOKEN, _('before_refresh_token', '刷新token前'))
register_event(CREATE_GROUP_PERMISSION, _('create group permission', '创建权限分组'))
register_event(UPDATE_GROUP_PERMISSION, _('update group permission', '修改权限分组'))
register_event(DELETE_GROUP_PERMISSION, _('delete group permission', '删除权限分组'))
Expand Down

0 comments on commit 9227934

Please sign in to comment.