From fb46ba439582f9716add8836cd92cf0d3e0e250e Mon Sep 17 00:00:00 2001 From: BryantHe Date: Fri, 11 Aug 2023 16:36:56 +0800 Subject: [PATCH] feat: add api auth --- app/__init__.py | 2 +- app/api/v1/__init__.py | 2 ++ app/api/v1/auth.py | 22 ++++++++++++++++++++++ app/api/v1/schema/auth.py | 20 ++++++++++++++++++++ app/util/common.py | 23 +++++++++++++++++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 app/api/v1/auth.py create mode 100644 app/api/v1/schema/auth.py diff --git a/app/__init__.py b/app/__init__.py index 1370f1e..f72cdc5 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -40,5 +40,5 @@ def create_app(): app = APIFlask(__name__) load_app_config(app) register_blueprints(app) - # load_rpc_client(app) + load_rpc_client(app) return app \ No newline at end of file diff --git a/app/api/v1/__init__.py b/app/api/v1/__init__.py index bd4b794..90acde0 100644 --- a/app/api/v1/__init__.py +++ b/app/api/v1/__init__.py @@ -1,11 +1,13 @@ from apiflask import APIBlueprint from app.api.v1.api import api +from app.api.v1.auth import auth def create_v1(): bp_v1 = APIBlueprint('v1', __name__) bp_v1.register_blueprint(api, url_prefix='/api') + bp_v1.register_blueprint(auth, url_prefix='/auth') return bp_v1 diff --git a/app/api/v1/auth.py b/app/api/v1/auth.py new file mode 100644 index 0000000..0f4ad7f --- /dev/null +++ b/app/api/v1/auth.py @@ -0,0 +1,22 @@ +from apiflask import APIBlueprint + +from app import rpc +from app.api.v1.schema.auth import WechatLoginIn, WechatLoginOut, RefreshTokenIn, RefreshTokenOut + +auth = APIBlueprint('auth', __name__) + + +@auth.post('/wechat_login') +@auth.input(WechatLoginIn) +@auth.output(WechatLoginOut) +def wechat_login(data): + result = rpc.admin.wechat_login(data['code']) + return result + + +@auth.get('/refresh_token') +@auth.input(RefreshTokenIn, location='query') +@auth.output(RefreshTokenOut) +def refresh_token(data): + result = rpc.admin.refresh_token(data['refresh_token']) + return result \ No newline at end of file diff --git a/app/api/v1/schema/auth.py b/app/api/v1/schema/auth.py new file mode 100644 index 0000000..04f76b7 --- /dev/null +++ b/app/api/v1/schema/auth.py @@ -0,0 +1,20 @@ +from apiflask import Schema +from apiflask.fields import String + + +class WechatLoginIn(Schema): + code = String(required=True) + + +class WechatLoginOut(Schema): + access_token = String() + refresh_token = String() + + +class RefreshTokenIn(Schema): + refresh_token = String(required=True) + + +class RefreshTokenOut(Schema): + access_token = String() + refresh_token = String() \ No newline at end of file diff --git a/app/util/common.py b/app/util/common.py index f24e78f..7aad113 100644 --- a/app/util/common.py +++ b/app/util/common.py @@ -1,6 +1,11 @@ import os from itertools import groupby from operator import itemgetter +from functools import wraps + +from flask import request, session + +from app import rpc def split_group(dict_list, key): @@ -13,3 +18,21 @@ def split_group(dict_list, key): basedir = os.getcwd() + + +def login_required(f): + """ + 登陆保护,验证用户是否登陆 + """ + + @wraps(f) + def wrapper(*args, **kwargs): + token = request.headers.get("Authorization", default=None) + if not token: + return '请登陆' + user_id = rpc.admin.identify(token) + if not user_id: + return '请登陆' + session['user_id'] = user_id + return f(*args, **kwargs) + return wrapper