Skip to content

Commit 41d812e

Browse files
committedFeb 25, 2019
Make session lifetime 12 hours. Make root page return 200.
1 parent ea55e82 commit 41d812e

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed
 

‎README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ location /saml/ {
2323
proxy_set_header X-Saml-Acs /saml/login;
2424
proxy_pass http://saml:5000/;
2525
}
26-
```
26+
27+
location @error401 {
28+
return 302 https://$http_host/saml/login?url=$request_uri;
29+
}
30+
```
31+
32+
## SECRET_KEY
33+
34+
This app wants an environment variable `SECRET_KEY`, which should be a secure,
35+
randomly-generated string. Otherwise, we generate one on the fly, which only
36+
works long as the app is running, and won't work in a distributed environment.
37+
SECRET_KEY is used to sign cookies, so setting a new key effectively
38+
invalidates all existing sessions.

‎app.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
from urllib.parse import urljoin
66
from datetime import timedelta
77
import os
8-
import uuid
8+
import secrets
99
app = Flask(__name__)
1010
app.wsgi_app = ProxyFix(app.wsgi_app)
1111
if os.environ.get('SECRET_KEY'):
1212
app.secret_key = os.environ['SECRET_KEY']
1313
else:
1414
app.logger.error('Generating burner SECRET_KEY for demo purposes')
15-
app.secret_key = str(uuid.uuid1())
15+
app.secret_key = secrets.token_urlsafe(32)
1616
app.config.update(
1717
SESSION_COOKIE_NAME='_saml_session',
1818
SESSION_COOKIE_HTTPONLY=True,
1919
SESSION_COOKIE_SECURE=True,
20-
PERMANENT_SESSION_LIFETIME=timedelta(minutes=10) # TODO: refine this
20+
PERMANENT_SESSION_LIFETIME=timedelta(hours=12)
2121
)
2222

23+
2324
@app.route('/status')
2425
@app.route('/status/group/<group>')
2526
def status(group=None):
@@ -35,7 +36,7 @@ def status(group=None):
3536
if not userid:
3637
abort(401)
3738
if group and group not in groups:
38-
abort(403)
39+
abort(403)
3940
headers = {'X-Saml-User': userid,
4041
'X-Saml-Groups': ':'.join(groups)}
4142
txt = f'Logged in as: {userid}\nGroups: {str(groups)}'
@@ -73,3 +74,11 @@ def login():
7374
def logout():
7475
session.clear()
7576
return 'Logged out'
77+
78+
79+
@app.route('/')
80+
def healthz():
81+
"""Return a 200 along with some useful links."""
82+
return '''
83+
<p><a href="login">Sign in</a></p><p><a href="logout">Logout</a></p>
84+
'''

0 commit comments

Comments
 (0)
Please sign in to comment.