forked from zhy0313/opsweb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogout.py
executable file
·67 lines (66 loc) · 2.68 KB
/
logout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#-*- coding: utf-8 -*-
from flask import Flask,Blueprint,redirect,make_response,request,jsonify
from module import tools,user_auth
from xml.dom.minidom import parseString
from collections import defaultdict
import redis
import time
app = Flask(__name__)
app.config.from_pyfile('conf/redis.conf')
app.config.from_pyfile('conf/cas.conf')
redis_host = app.config.get('REDIS_HOST')
redis_port = app.config.get('REDIS_PORT')
redis_password = app.config.get('REDIS_PASSWORD')
Redis = redis.StrictRedis(host=redis_host, port=redis_port,decode_responses=True)
service_url = app.config.get('SERVICE_URL')
ENV = tools.check_env()
if ENV == 'dev':
service_url = app.config.get('SERVICE_TEST_URL')
page_logout = Blueprint('logout',__name__)
@page_logout.route('/logout')
def logout():
tm = time.strftime('%Y%m%d', time.localtime())
dingId = Redis.get('OP_dingId_%s' % request.cookies.get('dingId'))
if dingId:
#清除用户计数
Redis.srem('op_active_users_%s' %tm, dingId)
#清除用户页面菜单
Redis.hdel(f'op_menu_{tm}',f'menu_{dingId}')
#清除用户票据
ticket = Redis.get('OP_ticket_%s' % request.cookies.get('ticket'))
Redis.delete('OP_logout_ticket_%s' % ticket)
timestamp = tools.timestamp(0)
#在cas服务器上注销
cas_client = user_auth.cas_logout()
logout_url = cas_client.get_logout_url(service_url=service_url)
app_resp = make_response(redirect(logout_url))
#清除cookie
for key in request.cookies:
app_resp.set_cookie(key, expires=timestamp)
return app_resp
@page_logout.route('/sso_logout',methods = ['POST'])
def sso_logout():
result = defaultdict()
try:
vals = request.form
if vals:
# 获取用户票据
xml_document = parseString(vals['logoutRequest'])
for node in xml_document.getElementsByTagName('samlp:SessionIndex'):
for child in node.childNodes:
if child.nodeType == child.TEXT_NODE:
result['session_index'] = str(child.nodeValue.strip())
# 清除用户票据
if result['session_index']:
tm = time.strftime('%Y%m%d', time.localtime())
dingId = Redis.get('OP_dingid_ticket_%s' % result['session_index'])
if dingId:
# 清除用户页面菜单
Redis.hdel(f'op_menu_{tm}', f'menu_{dingId}')
# 清除用户计数
Redis.srem('op_active_users_%s' % tm, dingId)
Redis.delete('OP_logout_ticket_%s' % result['session_index'])
return jsonify({'results':'success'})
return jsonify({'results': 'fail'})
except Exception as e:
return jsonify({'results': e})