Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make session lifetime 12 hours. Make root page return 200. #3

Merged
merged 1 commit into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ location /saml/ {
proxy_set_header X-Saml-Acs /saml/login;
proxy_pass http://saml:5000/;
}
```

location @error401 {
return 302 https://$http_host/saml/login?url=$request_uri;
}
```

## SECRET_KEY

This app wants an environment variable `SECRET_KEY`, which should be a secure,
randomly-generated string. Otherwise, we generate one on the fly, which only
works long as the app is running, and won't work in a distributed environment.
SECRET_KEY is used to sign cookies, so setting a new key effectively
invalidates all existing sessions.
17 changes: 13 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
from urllib.parse import urljoin
from datetime import timedelta
import os
import uuid
import secrets
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
if os.environ.get('SECRET_KEY'):
app.secret_key = os.environ['SECRET_KEY']
else:
app.logger.error('Generating burner SECRET_KEY for demo purposes')
app.secret_key = str(uuid.uuid1())
app.secret_key = secrets.token_urlsafe(32)
app.config.update(
SESSION_COOKIE_NAME='_saml_session',
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SECURE=True,
PERMANENT_SESSION_LIFETIME=timedelta(minutes=10) # TODO: refine this
PERMANENT_SESSION_LIFETIME=timedelta(hours=12)
)


@app.route('/status')
@app.route('/status/group/<group>')
def status(group=None):
Expand All @@ -35,7 +36,7 @@ def status(group=None):
if not userid:
abort(401)
if group and group not in groups:
abort(403)
abort(403)
headers = {'X-Saml-User': userid,
'X-Saml-Groups': ':'.join(groups)}
txt = f'Logged in as: {userid}\nGroups: {str(groups)}'
Expand Down Expand Up @@ -73,3 +74,11 @@ def login():
def logout():
session.clear()
return 'Logged out'


@app.route('/')
def healthz():
"""Return a 200 along with some useful links."""
return '''
<p><a href="login">Sign in</a></p><p><a href="logout">Logout</a></p>
'''