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