正在显示
8 个修改的文件
包含
90 行增加
和
82 行删除
app/decorators/auth_decorator.py
0 → 100644
1 | +from functools import wraps | |
2 | +from re import I | |
3 | +from authlib.integrations.flask_oauth2 import current_token | |
4 | +from flask import abort | |
5 | +from sqlalchemy.sql.elements import Null | |
6 | +from sqlalchemy.sql.functions import mode | |
7 | +from app.util.component.ParameterUtil import ParameterUtil | |
8 | +from app.models import db | |
9 | +from app.modules.auth.oauth2 import require_oauth | |
10 | + | |
11 | +# 认证装饰器 | |
12 | + | |
13 | +class auth_decorator(object): | |
14 | + def __init__(self, action='', permission=''): | |
15 | + self.permission = permission | |
16 | + self.action = action | |
17 | + | |
18 | + def __call__(self, func): | |
19 | + @wraps(func) | |
20 | + @require_oauth("profile") | |
21 | + def wrapped_function(*args, **kwargs): | |
22 | + if current_token and current_token.user and current_token.user.role: | |
23 | + print(func.__name__) | |
24 | + if self.permission and len(self.permission) > 0: | |
25 | + # 判断角色是否在permission列表中 | |
26 | + role = current_token.user.role | |
27 | + for p in self.permission: | |
28 | + if role == p: | |
29 | + return func(*args, **kwargs) | |
30 | + | |
31 | + abort(403) | |
32 | + else: | |
33 | + # 无permission,不校验 | |
34 | + return func(*args, **kwargs) | |
35 | + else: | |
36 | + pass # 无token,401 | |
37 | + | |
38 | + return wrapped_function | ... | ... |
... | ... | @@ -10,6 +10,8 @@ from .oauth2 import authorization, require_oauth, generate_user_info |
10 | 10 | from authlib.oauth2 import OAuth2Error |
11 | 11 | from authlib.integrations.flask_oauth2 import current_token |
12 | 12 | from . import user_create, client_create, client_query, user_query, user_update, user_delete |
13 | +import configure | |
14 | +from app.decorators.auth_decorator import auth_decorator | |
13 | 15 | |
14 | 16 | |
15 | 17 | def current_user(): |
... | ... | @@ -32,68 +34,6 @@ def split_by_crlf(s): |
32 | 34 | class DataManager(BlueprintApi): |
33 | 35 | bp = Blueprint("Auth", __name__, url_prefix="/auth") |
34 | 36 | |
35 | - # @staticmethod | |
36 | - # @bp.route("/test", methods=("GET", "POST")) | |
37 | - # def Test(): | |
38 | - # res = {} | |
39 | - # try: | |
40 | - # res["user"] = User.query.all() | |
41 | - # except Exception as e: | |
42 | - # raise e | |
43 | - # return res | |
44 | - | |
45 | - # @staticmethod | |
46 | - # @bp.route("/login", methods=("GET", "POST")) | |
47 | - # def Login(): | |
48 | - # if request.method == "POST": | |
49 | - # username = request.form["username"] | |
50 | - # password = request.form["password"] | |
51 | - # user = User.query.filter_by(username=username).first() | |
52 | - # if not user: | |
53 | - # user = User(username=username, | |
54 | - # password=password, role="admin") | |
55 | - # db.session.add(user) | |
56 | - # db.session.commit() | |
57 | - # session["id"] = user.id | |
58 | - # return redirect("/auth/authorize") | |
59 | - # user = current_user() | |
60 | - # if user: | |
61 | - # clients = OAuth2Client.query.filter_by(user_id=user.id).all() | |
62 | - # else: | |
63 | - # clients = [] | |
64 | - # return render_template("auth/authorize.html", user=user, clients=clients) | |
65 | - | |
66 | - # @staticmethod | |
67 | - # @bp.route("/create_client", methods=("GET", "POST")) | |
68 | - # def create_client(): | |
69 | - # user = current_user() | |
70 | - # if not user: | |
71 | - # return redirect("/auth/login") | |
72 | - # if request.method == "GET": | |
73 | - # return render_template("auth/create_client.html") | |
74 | - # form = request.form | |
75 | - # client_id = gen_salt(24) | |
76 | - # client = OAuth2Client(client_id=client_id, user_id=user.id) | |
77 | - # # Mixin doesn"t set the issue_at date | |
78 | - # client.client_id_issued_at = int(time.time()) | |
79 | - # if client.token_endpoint_auth_method == "none": | |
80 | - # client.client_secret = "" | |
81 | - # else: | |
82 | - # client.client_secret = gen_salt(48) | |
83 | - # client_metadata = { | |
84 | - # "client_name": form["client_name"], | |
85 | - # "client_uri": form["client_uri"], | |
86 | - # "grant_types": split_by_crlf(form["grant_type"]), | |
87 | - # "redirect_uris": split_by_crlf(form["redirect_uri"]), | |
88 | - # "response_types": split_by_crlf(form["response_type"]), | |
89 | - # "scope": form["scope"], | |
90 | - # "token_endpoint_auth_method": form["token_endpoint_auth_method"] | |
91 | - # } | |
92 | - # client.set_client_metadata(client_metadata) | |
93 | - # db.session.add(client) | |
94 | - # db.session.commit() | |
95 | - # return redirect("/auth/login") | |
96 | - | |
97 | 37 | @staticmethod |
98 | 38 | @bp.route("/authorize", methods=("GET", "POST")) |
99 | 39 | def authorize(): |
... | ... | @@ -132,10 +72,7 @@ class DataManager(BlueprintApi): |
132 | 72 | return jsonify(dict(error.get_body())) |
133 | 73 | return render_template("auth/authorize.html", user=user, grant=grant, error=error) |
134 | 74 | |
135 | - # if request.form["confirm"]: | |
136 | - # grant_user = user | |
137 | - # else: | |
138 | - # grant_user = None | |
75 | + | |
139 | 76 | |
140 | 77 | @staticmethod |
141 | 78 | @bp.route("/token", methods=["POST"]) |
... | ... | @@ -153,7 +90,6 @@ class DataManager(BlueprintApi): |
153 | 90 | |
154 | 91 | @staticmethod |
155 | 92 | @bp.route("/logout", methods=["GET"]) |
156 | - # @require_oauth("profile") | |
157 | 93 | def logout(): |
158 | 94 | url = '' |
159 | 95 | try: |
... | ... | @@ -170,24 +106,13 @@ class DataManager(BlueprintApi): |
170 | 106 | except OAuth2Error as error: |
171 | 107 | return jsonify(dict(error.get_body())) |
172 | 108 | return redirect(url) |
173 | - # if current_token: | |
174 | - # remove_user() | |
175 | - # # accesstoken = OAuth2Token.query.filter_by( | |
176 | - # # access_token=current_token.access_token).first() | |
177 | - # try: | |
178 | - # # accesstoken.revoked = True | |
179 | - # # db.session.commit() | |
180 | - # except error as e: | |
181 | - # return jsonify(dict(e.get_body())) | |
182 | - # else: | |
183 | - # return jsonify({"result": False, "message": "access_token is null"}) | |
184 | - | |
185 | - # return jsonify({"result": True, "message": "logout success"}) | |
186 | - | |
109 | + | |
110 | + | |
187 | 111 | """接口""" |
188 | 112 | @staticmethod |
189 | 113 | @bp.route("/users", methods=["GET"]) |
190 | 114 | @swag_from(user_query.Api.api_doc) |
115 | + @auth_decorator(configure.UserPermission) | |
191 | 116 | def user_query(): |
192 | 117 | """ |
193 | 118 | 获取用户列表 |
... | ... | @@ -197,6 +122,7 @@ class DataManager(BlueprintApi): |
197 | 122 | @staticmethod |
198 | 123 | @bp.route("/users", methods=["POST"]) |
199 | 124 | @swag_from(user_create.Api.api_doc) |
125 | + @auth_decorator(configure.UserPermission) | |
200 | 126 | def user_create(): |
201 | 127 | """ |
202 | 128 | 创建用户 |
... | ... | @@ -206,6 +132,7 @@ class DataManager(BlueprintApi): |
206 | 132 | @staticmethod |
207 | 133 | @bp.route("/userEdit", methods=["POST"]) |
208 | 134 | @swag_from(user_update.Api.api_doc) |
135 | + @auth_decorator(configure.UserPermission) | |
209 | 136 | def user_update(): |
210 | 137 | """ |
211 | 138 | 更新用户信息 |
... | ... | @@ -215,6 +142,7 @@ class DataManager(BlueprintApi): |
215 | 142 | @staticmethod |
216 | 143 | @bp.route("/userDelete", methods=["POST"]) |
217 | 144 | @swag_from(user_delete.Api.api_doc) |
145 | + @auth_decorator(configure.UserPermission) | |
218 | 146 | def user_delete(): |
219 | 147 | """ |
220 | 148 | 删除用户 | ... | ... |
... | ... | @@ -17,6 +17,8 @@ from . import database_edit |
17 | 17 | from . import database_alias_check |
18 | 18 | from . import database_connect_test |
19 | 19 | from . import database_info |
20 | +import configure | |
21 | +from app.decorators.auth_decorator import auth_decorator | |
20 | 22 | |
21 | 23 | class DataManager(BlueprintApi): |
22 | 24 | |
... | ... | @@ -26,6 +28,7 @@ class DataManager(BlueprintApi): |
26 | 28 | @staticmethod |
27 | 29 | @bp.route('/Register', methods=['POST']) |
28 | 30 | @swag_from(database_register.Api.api_doc) |
31 | + @auth_decorator(configure.DataPermission) | |
29 | 32 | def api_database_register(): |
30 | 33 | """ |
31 | 34 | 数据源注册 |
... | ... | @@ -35,6 +38,7 @@ class DataManager(BlueprintApi): |
35 | 38 | @staticmethod |
36 | 39 | @bp.route('/List', methods=['POST']) |
37 | 40 | @swag_from(database_list.Api.api_doc) |
41 | + @auth_decorator(configure.DataPermission) | |
38 | 42 | def api_database_list(): |
39 | 43 | """ |
40 | 44 | 数据源列表 |
... | ... | @@ -44,6 +48,7 @@ class DataManager(BlueprintApi): |
44 | 48 | @staticmethod |
45 | 49 | @bp.route('/Delete', methods=['POST']) |
46 | 50 | @swag_from(database_delete.Api.api_doc) |
51 | + @auth_decorator(configure.DataPermission) | |
47 | 52 | def api_database_delete(): |
48 | 53 | """ |
49 | 54 | 数据源注销 |
... | ... | @@ -53,6 +58,7 @@ class DataManager(BlueprintApi): |
53 | 58 | @staticmethod |
54 | 59 | @bp.route('/Edit', methods=['POST']) |
55 | 60 | @swag_from(database_edit.Api.api_doc) |
61 | + @auth_decorator(configure.DataPermission) | |
56 | 62 | def database_edit(): |
57 | 63 | """ |
58 | 64 | 修改数据源 |
... | ... | @@ -62,6 +68,7 @@ class DataManager(BlueprintApi): |
62 | 68 | @staticmethod |
63 | 69 | @bp.route('/Test', methods=['POST']) |
64 | 70 | @swag_from(database_test.Api.api_doc) |
71 | + @auth_decorator(configure.DataPermission) | |
65 | 72 | def api_database_test(): |
66 | 73 | """ |
67 | 74 | 数据源测试 |
... | ... | @@ -71,6 +78,7 @@ class DataManager(BlueprintApi): |
71 | 78 | @staticmethod |
72 | 79 | @bp.route('/CheckAlias', methods=['POST']) |
73 | 80 | @swag_from(database_alias_check.Api.api_doc) |
81 | + @auth_decorator(configure.DataPermission) | |
74 | 82 | def api_database_alias_check(): |
75 | 83 | """ |
76 | 84 | 数据源别名测试 |
... | ... | @@ -80,6 +88,7 @@ class DataManager(BlueprintApi): |
80 | 88 | @staticmethod |
81 | 89 | @bp.route('/CheckConnect', methods=['POST']) |
82 | 90 | @swag_from(database_connect_test.Api.api_doc) |
91 | + @auth_decorator(configure.DataPermission) | |
83 | 92 | def api_database_connect_test(): |
84 | 93 | """ |
85 | 94 | 数据源连接测试 | ... | ... |
... | ... | @@ -13,6 +13,8 @@ from . import get_meta |
13 | 13 | from . import data_entry_by_meta |
14 | 14 | from . import get_data_list |
15 | 15 | from . import data_entry_simple |
16 | +import configure | |
17 | +from app.decorators.auth_decorator import auth_decorator | |
16 | 18 | |
17 | 19 | class DataManager(BlueprintApi): |
18 | 20 | |
... | ... | @@ -21,6 +23,7 @@ class DataManager(BlueprintApi): |
21 | 23 | |
22 | 24 | @staticmethod |
23 | 25 | @bp.route('/Download/<file>', methods=['GET']) |
26 | + @auth_decorator(configure.DataPermission) | |
24 | 27 | def table_download_file(file): |
25 | 28 | parent = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
26 | 29 | dirpath = os.path.join(parent,"file_tmp") |
... | ... | @@ -41,6 +44,7 @@ class DataManager(BlueprintApi): |
41 | 44 | |
42 | 45 | @staticmethod |
43 | 46 | @bp.route('/DeleteFile/<file>', methods=['GET']) |
47 | + @auth_decorator(configure.DataPermission) | |
44 | 48 | def d_file(file): |
45 | 49 | parent = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
46 | 50 | dirpath = os.path.join(parent, "file_tmp") |
... | ... | @@ -59,6 +63,7 @@ class DataManager(BlueprintApi): |
59 | 63 | @staticmethod |
60 | 64 | @bp.route('/DataDownloadTask', methods=['POST']) |
61 | 65 | @swag_from(data_download_task.Api.api_doc) |
66 | + @auth_decorator(configure.DataPermission) | |
62 | 67 | def api_data_download_task(): |
63 | 68 | """ |
64 | 69 | 下载数据任务 |
... | ... | @@ -69,6 +74,7 @@ class DataManager(BlueprintApi): |
69 | 74 | @staticmethod |
70 | 75 | @bp.route('/GetMeta', methods=['POST']) |
71 | 76 | @swag_from(get_meta.Api.api_doc) |
77 | + @auth_decorator(configure.DataPermission) | |
72 | 78 | def get_meta(): |
73 | 79 | """ |
74 | 80 | 数据Meta |
... | ... | @@ -78,6 +84,7 @@ class DataManager(BlueprintApi): |
78 | 84 | @staticmethod |
79 | 85 | @bp.route('/GetDataList', methods=['POST']) |
80 | 86 | @swag_from(get_data_list.Api.api_doc) |
87 | + @auth_decorator(configure.DataPermission) | |
81 | 88 | def get_data_list(): |
82 | 89 | """ |
83 | 90 | 本地数据list |
... | ... | @@ -87,6 +94,7 @@ class DataManager(BlueprintApi): |
87 | 94 | @staticmethod |
88 | 95 | @bp.route('/DataEntryByMeta', methods=['POST']) |
89 | 96 | @swag_from(data_entry_by_meta.Api.api_doc) |
97 | + @auth_decorator(configure.DataPermission) | |
90 | 98 | def data_entry_by_meta(): |
91 | 99 | """ |
92 | 100 | 数据入库ByMeta |
... | ... | @@ -96,6 +104,7 @@ class DataManager(BlueprintApi): |
96 | 104 | @staticmethod |
97 | 105 | @bp.route('/DataEntrySimple', methods=['POST']) |
98 | 106 | @swag_from(data_entry_simple.Api.api_doc) |
107 | + @auth_decorator(configure.DataPermission) | |
99 | 108 | def data_entry_simple(): |
100 | 109 | """ |
101 | 110 | 数据入库Simple | ... | ... |
... | ... | @@ -23,6 +23,9 @@ from . import table_vacuate_info |
23 | 23 | from . import table_vacuate_ref |
24 | 24 | from . import table_vacuate_delete |
25 | 25 | from . import field_value |
26 | +import configure | |
27 | +from app.decorators.auth_decorator import auth_decorator | |
28 | + | |
26 | 29 | class DataManager(BlueprintApi): |
27 | 30 | |
28 | 31 | bp = Blueprint("DataManager", __name__, url_prefix="/API/Manager") |
... | ... | @@ -30,6 +33,7 @@ class DataManager(BlueprintApi): |
30 | 33 | @staticmethod |
31 | 34 | @bp.route('/FieldEdit', methods=['POST']) |
32 | 35 | @swag_from(field_edit.Api.api_doc) |
36 | + @auth_decorator(configure.DataPermission) | |
33 | 37 | def field_edit(): |
34 | 38 | """ |
35 | 39 | 修改属性别名 |
... | ... | @@ -48,6 +52,7 @@ class DataManager(BlueprintApi): |
48 | 52 | @staticmethod |
49 | 53 | @bp.route('/FieldValue', methods=['POST']) |
50 | 54 | @swag_from(field_value.Api.api_doc) |
55 | + @auth_decorator(configure.DataPermission) | |
51 | 56 | def field_value(): |
52 | 57 | """ |
53 | 58 | 属性值 |
... | ... | @@ -67,6 +72,7 @@ class DataManager(BlueprintApi): |
67 | 72 | @staticmethod |
68 | 73 | @bp.route('/TableEdit', methods=['POST']) |
69 | 74 | @swag_from(table_edit.Api.api_doc) |
75 | + @auth_decorator(configure.DataPermission) | |
70 | 76 | def table_edit(): |
71 | 77 | """ |
72 | 78 | 修改数据 |
... | ... | @@ -77,6 +83,7 @@ class DataManager(BlueprintApi): |
77 | 83 | @staticmethod |
78 | 84 | @bp.route('/TableDelete', methods=['POST']) |
79 | 85 | @swag_from(table_delete.Api.api_doc) |
86 | + @auth_decorator(configure.DataPermission) | |
80 | 87 | def table_delete(): |
81 | 88 | """ |
82 | 89 | 删除数据 |
... | ... | @@ -97,6 +104,7 @@ class DataManager(BlueprintApi): |
97 | 104 | @staticmethod |
98 | 105 | @bp.route('/TableRefresh', methods=['POST']) |
99 | 106 | @swag_from(table_refresh.Api.api_doc) |
107 | + @auth_decorator(configure.DataPermission) | |
100 | 108 | def table_refresh(): |
101 | 109 | """ |
102 | 110 | 刷新数据 |
... | ... | @@ -116,6 +124,7 @@ class DataManager(BlueprintApi): |
116 | 124 | @staticmethod |
117 | 125 | @bp.route('/TableVacuate', methods=['POST']) |
118 | 126 | @swag_from(table_vacuate.Api.api_doc) |
127 | + @auth_decorator(configure.DataPermission) | |
119 | 128 | def table_vacuate(): |
120 | 129 | """ |
121 | 130 | 数据抽稀 |
... | ... | @@ -125,6 +134,7 @@ class DataManager(BlueprintApi): |
125 | 134 | @staticmethod |
126 | 135 | @bp.route('/TableVacuateOne', methods=['POST']) |
127 | 136 | @swag_from(table_vacuate_one.Api.api_doc) |
137 | + @auth_decorator(configure.DataPermission) | |
128 | 138 | def api_table_vacuate_one(): |
129 | 139 | """ |
130 | 140 | 单独数据抽稀 |
... | ... | @@ -153,6 +163,7 @@ class DataManager(BlueprintApi): |
153 | 163 | @staticmethod |
154 | 164 | @bp.route('/TableVacuateDelete', methods=['POST']) |
155 | 165 | @swag_from(table_vacuate_delete.Api.api_doc) |
166 | + @auth_decorator(configure.DataPermission) | |
156 | 167 | def api_table_vacuate_delete(): |
157 | 168 | """ |
158 | 169 | 数据抽稀删除 | ... | ... |
... | ... | @@ -11,6 +11,9 @@ from . import task_detail |
11 | 11 | from . import task_delete |
12 | 12 | from . import task_count |
13 | 13 | from . import task_kill |
14 | +from app.decorators.auth_decorator import auth_decorator | |
15 | +import configure | |
16 | +from app.modules.auth.oauth2 import require_oauth | |
14 | 17 | |
15 | 18 | class DataManager(BlueprintApi): |
16 | 19 | |
... | ... | @@ -38,6 +41,7 @@ class DataManager(BlueprintApi): |
38 | 41 | @staticmethod |
39 | 42 | @bp.route('/Delete', methods=['POST']) |
40 | 43 | @swag_from(task_delete.Api.api_doc) |
44 | + @require_oauth("profile") | |
41 | 45 | def task_delete(): |
42 | 46 | """ |
43 | 47 | 删除任务 |
... | ... | @@ -47,6 +51,7 @@ class DataManager(BlueprintApi): |
47 | 51 | @staticmethod |
48 | 52 | @bp.route('/Kill', methods=['POST']) |
49 | 53 | @swag_from(task_kill.Api.api_doc) |
54 | + @require_oauth("profile") | |
50 | 55 | def task_kill(): |
51 | 56 | """ |
52 | 57 | Kill任务 | ... | ... |
... | ... | @@ -8,7 +8,8 @@ from flasgger import swag_from |
8 | 8 | from flask import Blueprint |
9 | 9 | from app.util import BlueprintApi |
10 | 10 | from . import monitoring, metrics, monitor_host_create, monitor_host_list, monitor_host_delete, monitor_host_edit |
11 | - | |
11 | +from app.decorators.auth_decorator import auth_decorator | |
12 | +import configure | |
12 | 13 | |
13 | 14 | user_socket_list = [] |
14 | 15 | user_socket_dict = {} |
... | ... | @@ -48,6 +49,7 @@ class Monitor(BlueprintApi): |
48 | 49 | @staticmethod |
49 | 50 | @bp.route('/RegisterHost', methods=['POST']) |
50 | 51 | @swag_from(monitor_host_create.Api.api_doc) |
52 | + @auth_decorator(configure.MonitorPermission) | |
51 | 53 | def monitor_host_create(): |
52 | 54 | ''' |
53 | 55 | 注册监控主机 |
... | ... | @@ -66,6 +68,7 @@ class Monitor(BlueprintApi): |
66 | 68 | @staticmethod |
67 | 69 | @bp.route('/HostDelete', methods=['POST']) |
68 | 70 | @swag_from(monitor_host_delete.Api.api_doc) |
71 | + @auth_decorator(configure.MonitorPermission) | |
69 | 72 | def monitor_host_delete(): |
70 | 73 | ''' |
71 | 74 | 删除主机 |
... | ... | @@ -75,6 +78,7 @@ class Monitor(BlueprintApi): |
75 | 78 | @staticmethod |
76 | 79 | @bp.route('/HostEdit', methods=['POST']) |
77 | 80 | @swag_from(monitor_host_edit.Api.api_doc) |
81 | + @auth_decorator(configure.MonitorPermission) | |
78 | 82 | def monitor_host_edit(): |
79 | 83 | ''' |
80 | 84 | 编辑主机配置 | ... | ... |
... | ... | @@ -22,6 +22,10 @@ swagger_configure = {"title": "DMapManager"} |
22 | 22 | entry_data_thread = 3 |
23 | 23 | scan_module = ["app.modules"] # API所在的模块 |
24 | 24 | SECRET_KEY = b'_5#y2L"F4Q8z\n\xec]/' |
25 | +UserPermission = ['admin'] | |
26 | +MonitorPermission = ['admin'] | |
27 | +DataPermission = ['admin', 'dataman'] | |
28 | +PublishPermission = ['admin', 'dataman', 'publisher'] | |
25 | 29 | |
26 | 30 | log_level = logging.INFO |
27 | 31 | ... | ... |
请
注册
或
登录
后发表评论