提交 e49c1cc381365a2d2681b9b6105d4e1a60fe7b07

作者 qianyingz
1 个父辈 a7ad4854

add feature# 三方登录对接配置

... ... @@ -224,10 +224,10 @@ class DataManager(BlueprintApi):
224 224 @bp.route("/oa", methods=["GET"])
225 225 def oa_authorization():
226 226 client = oauth2.WebApplicationClient(
227   - configure.OA_OAUTH["client_id"])
  227 + configure.OA["client_id"])
228 228 state = client.state_generator()
229 229 auth_uri = client.prepare_request_uri(
230   - configure.OA_OAUTH["authorization_endpoint"], configure.OA_OAUTH["redirect_uri"], configure.OA_OAUTH["scope"], state)
  230 + configure.OA["authorization_endpoint"], configure.OA["redirect_uri"], configure.OA["scope"], state)
231 231 session["oauth_state"] = state
232 232 return redirect(auth_uri)
233 233
... ... @@ -239,7 +239,7 @@ class DataManager(BlueprintApi):
239 239 def oa_callback():
240 240
241 241 client = oauth2.WebApplicationClient(
242   - configure.OA_OAUTH["client_id"])
  242 + configure.OA["client_id"])
243 243
244 244 # 获取code
245 245 code = client.parse_request_uri_response(
... ... @@ -250,9 +250,9 @@ class DataManager(BlueprintApi):
250 250
251 251 # 获取token
252 252 body = client.prepare_request_body(
253   - code, redirect_uri=configure.OA_OAUTH["redirect_uri"], client_secret=configure.OA_OAUTH["client_secret"])
  253 + code, redirect_uri=configure.OA["redirect_uri"], client_secret=configure.OA["client_secret"])
254 254
255   - r = requests.post(configure.OA_OAUTH["token_endpoint"], body, headers={
  255 + r = requests.post(configure.OA["token_endpoint"], body, headers={
256 256 "Content-Type": "application/x-www-form-urlencoded"})
257 257
258 258 tokeninfo = r.json()
... ... @@ -260,7 +260,7 @@ class DataManager(BlueprintApi):
260 260
261 261 if access_token:
262 262 # 获取用户信息
263   - userinfo_url = configure.OA_OAUTH["userinfo_endpoint"]
  263 + userinfo_url = configure.OA["userinfo_endpoint"]
264 264 user_request = requests.get(userinfo_url, headers={
265 265 "Authorization": "Bearer %s" % access_token})
266 266 userinfo = user_request.json()
... ... @@ -277,7 +277,7 @@ class DataManager(BlueprintApi):
277 277 # 用户不存在,创建用户
278 278 if not user:
279 279 user = User(username=user_name, password=SM3.encode('DMap@123'), role='dataman',
280   - phone='', company='', position='', email='',
  280 + phone='', company='', position='', email='',
281 281 create_time=time.strftime(
282 282 "%Y-%m-%d %H:%M:%S", time.localtime()),
283 283 update_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
... ... @@ -286,10 +286,10 @@ class DataManager(BlueprintApi):
286 286
287 287 # dmap token授权
288 288 session["id"] = user.id
289   -
  289 +
290 290 # 存入数据库
291 291 token = OAuth2Token(
292   - client_id=configure.OA_OAUTH["client_id"],
  292 + client_id=configure.OA["client_id"],
293 293 token_type=tokeninfo.get("token_type"),
294 294 access_token=access_token,
295 295 scope=tokeninfo.get("scope"),
... ... @@ -298,14 +298,17 @@ class DataManager(BlueprintApi):
298 298 )
299 299 db.session.add(token)
300 300 db.session.commit()
  301 + redirect_uri = ""
  302 + try:
  303 + redirect_uri = session["redirect_uri"]
  304 + if not redirect_uri:
  305 + redirect_uri = '/'
  306 + except:
  307 + redirect_uri = "/"
301 308
302   - redirect_uri = session["redirect_uri"]
303   - if not redirect_uri:
304   - redirect_uri = '/'
305   -
306 309 response = make_response(redirect(redirect_uri))
307   - response.set_cookie('accessToken', access_token,max_age=604_800)
308   -
  310 + response.set_cookie('accessToken', access_token, max_age=604_800)
  311 +
309 312 return response
310 313 else:
311 314 return redirect('/')
... ...
... ... @@ -16,6 +16,7 @@ class User (db.Model):
16 16 __tablename__ = "dmap_user"
17 17 id = Column(Integer, primary_key=True)
18 18 username = Column(Text)
  19 +
19 20 password = Column(Text)
20 21 company = Column(Text)
21 22 position = Column(Text)
... ... @@ -24,7 +25,8 @@ class User (db.Model):
24 25 create_time = Column(Time)
25 26 update_time = Column(Time)
26 27 role = Column(Text)
27   - #origin = Column(Text, default="dmap")
  28 + #display_name = Column(Text, nullable=True) # 昵称
  29 + #origin = Column(Text, default="dmap") # 用户来源,默认dmap平台用户
28 30
29 31 def __str__(self):
30 32 return self.username
... ...
... ... @@ -55,7 +55,7 @@ class Api(ApiTemplate):
55 55 {"name": "database_guid",
56 56 "in": "formData",
57 57 "type": "string",
58   - "description": "数据库guid", "required": "true"},
  58 + "description": "数据库guid", "": "true"},
59 59
60 60 ],
61 61 "responses":{
... ...
... ... @@ -5,12 +5,11 @@ deploy_ip_host = "172.26.40.105:8840"
5 5 # 系统数据库
6 6 SQLALCHEMY_DATABASE_URI = "postgresql://postgres:chinadci@172.26.60.101:5432/dmap_manager"
7 7
8   -
9 8 # 指定精华表所在位置(必须为空间库),设置为None则存放在各自的实体库中
10 9 VACUATE_DB_URI = None
11 10 #VACUATE_DB_URI = SQLALCHEMY_DATABASE_URI
12 11
13   -#DMap引擎
  12 +# DMap引擎
14 13 dmap_engine = "http://172.26.60.101:8820"
15 14
16 15 # 固定配置不需要修改
... ... @@ -27,3 +26,14 @@ PublishPermission = ['admin', 'dataman', 'publisher']
27 26 ServicePermission = ['admin', 'dataman', 'publisher']
28 27
29 28 log_level = logging.INFO
  29 +
  30 +
  31 +OA = {
  32 + "client_id": "dmap",
  33 + "client_secret": "secret",
  34 + "scope": "openid profile",
  35 + "redirect_uri": "http://localhost:8841/auth/oa/callback",
  36 + "authorization_endpoint": "https://login.chinadci.com/netsso/connect/authorize",
  37 + "token_endpoint": "https://login.chinadci.com/netsso/connect/token",
  38 + "userinfo_endpoint": "https://login.chinadci.com/netsso/connect/userinfo"
  39 +}
... ...
... ... @@ -6,4 +6,4 @@ import os
6 6 os.environ['AUTHLIB_INSECURE_TRANSPORT'] = '1'
7 7 app: Flask = create_app()
8 8 if __name__ == '__main__':
9   - app.run(host="0.0.0.0", port="8841", threaded=True, debug=True)
  9 + app.run(host="0.0.0.0", port="8841", threaded=True, debug=True)
\ No newline at end of file
... ...
注册登录 后发表评论