-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
92 lines (73 loc) · 2.74 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from flask import Flask, request, session, redirect, url_for
from cas import CASClient
app = Flask(__name__)
app.secret_key = 'V7nlCN90LPHOTA9PGGyf'
cas_client = CASClient(
version=3,
service_url='http://localhost:5000/login?next=%2Fprofile',
server_url='https://django-cas-ng-demo-server.herokuapp.com/cas/'
)
@app.route('/')
def index():
body = """<!DOCTYPE html>
<html>
<head>
<title>python-cas Flask example demo</title>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0">
</head>
<body>
<h1>Welcome to python-cas Flask example demo</h1>
<p><a href="/login">CAS Login</a></p>
<p>Related post:</p>
<ul>
<li><a href="https://djangocas.dev/blog/python-cas-flask-example/">python-cas Flask example</a></li>
</ul>
<hr>
<p><a href="https://djangocas.dev/">Project homepage</a></p>
</body>
</html>
"""
return body
@app.route('/profile')
def profile(method=['GET']):
if 'username' in session:
return 'Logged in as %s. <a href="/logout">Logout</a>' % session['username']
return 'Login required. <a href="/login">Login</a>', 403
@app.route('/login')
def login():
if 'username' in session:
# Already logged in
return redirect(url_for('profile'))
next = request.args.get('next')
ticket = request.args.get('ticket')
if not ticket:
# No ticket, the request come from end user, send to CAS login
cas_login_url = cas_client.get_login_url()
app.logger.debug('CAS login URL: %s', cas_login_url)
return redirect(cas_login_url)
# There is a ticket, the request come from CAS as callback.
# need call `verify_ticket()` to validate ticket and get user profile.
app.logger.debug('ticket: %s', ticket)
app.logger.debug('next: %s', next)
user, attributes, pgtiou = cas_client.verify_ticket(ticket)
app.logger.debug(
'CAS verify ticket response: user: %s, attributes: %s, pgtiou: %s', user, attributes, pgtiou)
if not user:
return 'Failed to verify ticket. <a href="/login">Login</a>'
else: # Login successfully, redirect according `next` query parameter.
session['username'] = user
return redirect(next)
@app.route('/logout')
def logout():
redirect_url = url_for('logout_callback', _external=True)
cas_logout_url = cas_client.get_logout_url(redirect_url)
app.logger.debug('CAS logout URL: %s', cas_logout_url)
return redirect(cas_logout_url)
@app.route('/logout_callback')
def logout_callback():
# redirect from CAS logout request after CAS logout successfully
session.pop('username', None)
return 'Logged out from CAS. <a href="/login">Login</a>'
@app.route('/ping')
def ping():
return 'pong'