正在显示
6 个修改的文件
包含
78 行增加
和
17 行删除
DMapManager @ 8e446e18
1 | -Subproject commit 8e446e18a7a81ea282d05f6026f4d41d5dae1378 |
... | ... | @@ -4,11 +4,13 @@ from flask import Flask as _Flask |
4 | 4 | from flask.json import JSONEncoder as _JSONEncoder |
5 | 5 | from flask_cors import CORS |
6 | 6 | import time |
7 | + | |
8 | +from sqlalchemy.sql.expression import true | |
7 | 9 | import configure |
8 | 10 | from app.util import BlueprintApi |
9 | 11 | from app.util import find_class |
10 | 12 | from app.models import db, Table, InsertingLayerName, Database, DES, Task |
11 | -from app.modules.auth.oauth2 import config_oauth | |
13 | +from app.modules.auth.oauth2 import config_oauth, myCodeIDToken | |
12 | 14 | from flasgger import Swagger |
13 | 15 | # from rtree import index |
14 | 16 | import logging |
... | ... | @@ -28,10 +30,12 @@ from app.util.component.StructuredPrint import StructurePrint |
28 | 30 | from app.util.component.PGUtil import PGUtil |
29 | 31 | import os |
30 | 32 | |
33 | + | |
31 | 34 | class JSONEncoder(_JSONEncoder): |
32 | 35 | """ |
33 | 36 | 因为decimal不能序列化,增加Flask对decimal类的解析 |
34 | 37 | """ |
38 | + | |
35 | 39 | def default(self, o): |
36 | 40 | if isinstance(o, decimal.Decimal): |
37 | 41 | return float(o) |
... | ... | @@ -42,7 +46,9 @@ class Flask(_Flask): |
42 | 46 | json_encoder = JSONEncoder |
43 | 47 | |
44 | 48 | |
45 | -GLOBAL_DIC={} | |
49 | +GLOBAL_DIC = {} | |
50 | + | |
51 | + | |
46 | 52 | def create_app(): |
47 | 53 | """ |
48 | 54 | flask应用创建函数 |
... | ... | @@ -62,6 +68,9 @@ def create_app(): |
62 | 68 | app.config['OAUTH2_JWT_KEY'] = 'secret-key' |
63 | 69 | app.config['OAUTH2_JWT_ALG'] = 'HS256' |
64 | 70 | # app.config['SQLALCHEMY_ECHO'] = True |
71 | + | |
72 | + # allows cookies and credentials to be submitted across domains | |
73 | + app.config['CORS_SUPPORTS_CREDENTIALS'] = true | |
65 | 74 | |
66 | 75 | # 跨域设置 |
67 | 76 | CORS(app) |
... | ... | @@ -85,10 +94,10 @@ def create_app(): |
85 | 94 | '[%(levelname)s] %(asctime)s %(message)s') |
86 | 95 | handler.setFormatter(logging_format) |
87 | 96 | app.logger.addHandler(handler) |
88 | - | |
97 | + | |
89 | 98 | # 配置使用鉴权组件,不写无法认证授权 |
90 | 99 | config_oauth(app) |
91 | - | |
100 | + | |
92 | 101 | # 注册blueprint,查找BlueprintApi的子类 |
93 | 102 | for scan in configure.scan_module: |
94 | 103 | for api in find_class(scan, BlueprintApi): |
... | ... | @@ -101,7 +110,7 @@ def create_app(): |
101 | 110 | StructurePrint.print("start listen") |
102 | 111 | process = threading.Thread(target=data_entry_center) |
103 | 112 | process.start() |
104 | - | |
113 | + | |
105 | 114 | # 不检测https |
106 | 115 | os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' |
107 | 116 | |
... | ... | @@ -147,7 +156,8 @@ def data_entry_center(): |
147 | 156 | |
148 | 157 | if inter_size < configure.entry_data_thread: |
149 | 158 | # 锁表啊 |
150 | - ready_task:Task = sys_session.query(Task).filter_by(state=0,task_type=1).order_by(Task.create_time).with_lockmode("update").limit(1).one_or_none() | |
159 | + ready_task: Task = sys_session.query(Task).filter_by(state=0, task_type=1).order_by( | |
160 | + Task.create_time).with_lockmode("update").limit(1).one_or_none() | |
151 | 161 | if ready_task: |
152 | 162 | |
153 | 163 | try: |
... | ... | @@ -204,4 +214,4 @@ def data_entry_center(): |
204 | 214 | sys_session.commit() |
205 | 215 | except Exception as e: |
206 | 216 | sys_session.commit() |
207 | - StructurePrint.print(e.__str__(), "error") | |
217 | + StructurePrint.print(e.__str__(), "error") | |
\ No newline at end of file | ... | ... |
1 | +from enum import auto | |
2 | +from logging import error | |
3 | + | |
4 | +from sqlalchemy.sql.expression import false, true | |
1 | 5 | from app.util import BlueprintApi |
2 | 6 | from app.util import BlueprintApi |
3 | 7 | from flask import Blueprint, render_template, redirect, url_for, request, session, jsonify |
... | ... | @@ -6,9 +10,10 @@ from sqlalchemy import and_ |
6 | 10 | from .models import * |
7 | 11 | from werkzeug.security import gen_salt |
8 | 12 | import time |
9 | -from .oauth2 import authorization, require_oauth, generate_user_info | |
13 | +from .oauth2 import authorization, myCodeIDToken, require_oauth, generate_user_info | |
10 | 14 | from authlib.oauth2 import OAuth2Error |
11 | 15 | from authlib.integrations.flask_oauth2 import current_token |
16 | +from authlib.oidc.core import CodeIDToken | |
12 | 17 | |
13 | 18 | |
14 | 19 | def current_user(): |
... | ... | @@ -18,6 +23,12 @@ def current_user(): |
18 | 23 | return None |
19 | 24 | |
20 | 25 | |
26 | +def remove_user(): | |
27 | + user = current_user() | |
28 | + if user: | |
29 | + session.pop('id') | |
30 | + | |
31 | + | |
21 | 32 | def split_by_crlf(s): |
22 | 33 | return [v for v in s.splitlines() if v] |
23 | 34 | |
... | ... | @@ -92,17 +103,21 @@ class DataManager(BlueprintApi): |
92 | 103 | def authorize(): |
93 | 104 | user = current_user() |
94 | 105 | if request.method == 'GET': |
106 | + # 没有登录,跳转到登录界面 | |
95 | 107 | try: |
96 | 108 | grant = authorization.validate_consent_request(end_user=user) |
97 | 109 | except OAuth2Error as error: |
98 | 110 | return jsonify(dict(error.get_body())) |
99 | - return render_template('auth/authorize.html', user=user, grant=grant) | |
111 | + if not user: | |
112 | + return render_template('auth/authorize.html', user=user, grant=grant) | |
100 | 113 | # return render_template('auth/login1.html', user=user, grant=grant) |
101 | 114 | if not user and 'username' in request.form: |
102 | 115 | username = request.form.get('username') |
103 | 116 | password = request.form.get('password') |
104 | 117 | user = User.query.filter_by( |
105 | 118 | username=username, password=password).first() |
119 | + if User: | |
120 | + session['id'] = user.id | |
106 | 121 | grant_user = user |
107 | 122 | # if request.form['confirm']: |
108 | 123 | # grant_user = user |
... | ... | @@ -119,4 +134,25 @@ class DataManager(BlueprintApi): |
119 | 134 | @bp.route('/userinfo') |
120 | 135 | @require_oauth('profile') |
121 | 136 | def api_me(): |
122 | - return jsonify(generate_user_info(current_token.user, current_token.scope)) | |
137 | + try: | |
138 | + return jsonify(generate_user_info(current_token.user, current_token.scope)) | |
139 | + except error as e: | |
140 | + return jsonify(dict(e.get_body())) | |
141 | + | |
142 | + @staticmethod | |
143 | + @bp.route('/logout', methods=('GET', 'POST')) | |
144 | + @require_oauth('profile') | |
145 | + def logout(): | |
146 | + if current_token: | |
147 | + remove_user() | |
148 | + accesstoken = OAuth2Token.query.filter_by( | |
149 | + access_token=current_token.access_token).first() | |
150 | + try: | |
151 | + accesstoken.revoked = True | |
152 | + db.session.commit() | |
153 | + except error as e: | |
154 | + return jsonify(dict(e.get_body())) | |
155 | + else: | |
156 | + return jsonify({'result': False, 'message': 'access_token is null'}) | |
157 | + | |
158 | + return jsonify({'result': True, 'message': 'logout success'}) | ... | ... |
... | ... | @@ -56,4 +56,5 @@ class OAuth2Token(db.Model, OAuth2TokenMixin): |
56 | 56 | id = Column(Integer, primary_key=True) |
57 | 57 | user_id = Column( |
58 | 58 | Integer, ForeignKey('dmdms_user.id', ondelete='CASCADE')) |
59 | - user = relationship('User') | |
59 | + # name = Column(Text) | |
60 | + user = relationship('User') | |
\ No newline at end of file | ... | ... |
1 | +from os import access, remove | |
2 | +from time import time | |
1 | 3 | from authlib.integrations.flask_oauth2 import ( |
2 | 4 | AuthorizationServer, ResourceProtector) |
3 | 5 | from authlib.integrations.sqla_oauth2 import ( |
... | ... | @@ -13,11 +15,12 @@ from authlib.oidc.core.grants import ( |
13 | 15 | OpenIDImplicitGrant as _OpenIDImplicitGrant, |
14 | 16 | OpenIDHybridGrant as _OpenIDHybridGrant, |
15 | 17 | ) |
16 | -from authlib.oidc.core import UserInfo | |
18 | +from authlib.oidc.core import UserInfo, CodeIDToken, IDToken | |
19 | +from sqlalchemy.sql.sqltypes import DateTime | |
17 | 20 | from werkzeug.security import gen_salt |
18 | 21 | from .models import db, User |
19 | 22 | from .models import OAuth2Client, OAuth2AuthorizationCode, OAuth2Token |
20 | - | |
23 | +from flask import g | |
21 | 24 | |
22 | 25 | DUMMY_JWT_CONFIG = { |
23 | 26 | 'key': 'secret-key', |
... | ... | @@ -26,6 +29,17 @@ DUMMY_JWT_CONFIG = { |
26 | 29 | 'exp': 7200, |
27 | 30 | } |
28 | 31 | |
32 | +class myCodeIDToken(CodeIDToken): | |
33 | + def validate(self, now, leeway): | |
34 | + return super().validate(now=now, leeway=leeway) | |
35 | + | |
36 | + def validate_exp(self, now, leeway): | |
37 | + return super().validate_exp(now, leeway) | |
38 | + | |
39 | + | |
40 | +def validate_token(accesstoken): | |
41 | + return IDToken.validate(self=accesstoken) | |
42 | + | |
29 | 43 | |
30 | 44 | def exists_nonce(nonce, req): |
31 | 45 | exists = OAuth2AuthorizationCode.query.filter_by( |
... | ... | @@ -119,11 +133,12 @@ def config_oauth(app): |
119 | 133 | app, |
120 | 134 | query_client=query_client, |
121 | 135 | save_token=save_token |
136 | + # fetch_token=fetch_token | |
122 | 137 | ) |
123 | 138 | |
124 | 139 | # support all openid grants |
125 | 140 | authorization.register_grant(AuthorizationCodeGrant, [ |
126 | - OpenIDCode(require_nonce=True), | |
141 | + OpenIDCode(require_nonce=True) | |
127 | 142 | ]) |
128 | 143 | authorization.register_grant(ImplicitGrant) |
129 | 144 | authorization.register_grant(HybridGrant) | ... | ... |
1 | 1 | # coding=utf-8 |
2 | 2 | from flask import Flask |
3 | 3 | from app import create_app |
4 | -app:Flask = create_app() | |
4 | +app: Flask = create_app() | |
5 | 5 | if __name__ == '__main__': |
6 | 6 | app.run(host="0.0.0.0", port="8840", threaded=True, debug=True) |
7 | - # app.run(host="0.0.0.0", port="8840", threaded=True) | |
\ No newline at end of file | ||
7 | + # app.run(host="0.0.0.0", port="8840", threaded=True) | ... | ... |
请
注册
或
登录
后发表评论