#!/usr/bin/env python
# Startdate: 2021-06-17
# goals:
# accept kerberos or ldap "authorization: basic gowinablz;nuiowekj==" auth, to create a cookie for a session that lasts for 15 minutes. use the cookie to get to protected URLs
# References:
# https://code.tutsplus.com/tutorials/flask-authentication-with-ldap--cms-23101
# https://www.techlifediary.com/python-web-development-tutorial-using-flask-session-cookies/
# delete cookie https://stackoverflow.com/a/14386413/3569534
# timeout sessions https://stackoverflow.com/a/11785722/3569534
# future: https://code.tutsplus.com/tutorials/flask-authentication-with-ldap--cms-23101
# better timeout session: https://stackoverflow.com/a/49891626/3569534
# Improve:
# purge sessions after 15 minutes?
# Run:
# FLASK_APP=session_app.py FLASK_DEBUG=1 flask run --host 0.0.0.0
# Dependencies:
# apt-get install python3-flask
# pip3 install Flask-kerberos kerberos
from flask import Flask, Response, redirect, url_for, render_template, request
from flask_kerberos import init_kerberos, requires_authentication, _unauthorized, _forbidden, _gssapi_authenticate
from flask import _request_ctx_stack as stack, make_response, session
#from flask.ext.login import LoginManager
import kerberos
from functools import wraps
from socket import gethostname
import binascii, datetime
from functools import wraps
import os
DEBUG=True
app = Flask(__name__)
app.config.from_object(__name__)
app.debug=True
secret_key_value = os.urandom(24)
secret_key_value_hex_encoded = binascii.hexlify(secret_key_value)
app.config['SECRET_KEY'] = secret_key_value_hex_encoded
#app.config['PERMANENT_SESSION_LIFETIME'] = datetime.timedelta(days=7)
#session.permanent = True
minutes = 2
app.permanent_session_lifetime=datetime.timedelta(minutes=minutes)
def requires_session(function):
'''
Requires a valid session, provided by cookie!
'''
@wraps(function)
def decorated(*args, **kwargs):
if not session:
return Response("requires session",401)
else:
if 'user' not in session:
return Response("User is not in this session.",401)
s_user = session['user']
c_user = request.cookies.get('user')
print(f"session user: {s_user}")
print(f"cookie user: {c_user}")
if session['user'] != c_user:
return Response("Wrong user for this session!.",401)
# otherwise, everything is good!
#return Response(f"session user: {s_user}
cookie user: {c_user}", 200)
# return to the passed function, from https://github.com/ArtemAngelchev/flask-basicauth-ldap/blob/master/flask_basicauth_ldap.py
return function(*args,**kwargs)
# catch-all
return Response("requires session",401)
return decorated
# imported from flask_kerberos and modified, because I want custom 401 message
def requires_authn_kerberos(function):
'''
Require that the wrapped view function only be called by users
authenticated with Kerberos. The view function will have the authenticated
users principal passed to it as its first argument.
:param function: flask view function
:type function: function
:returns: decorated function
:rtype: function
'''
@wraps(function)
def decorated(*args, **kwargs):
header = request.headers.get("Authorization")
if header:
ctx = stack.top
token = ''.join(header.split()[1:])
rc = _gssapi_authenticate(token)
if rc == kerberos.AUTH_GSS_COMPLETE:
response = function(ctx.kerberos_user, *args, **kwargs)
response = make_response(response)
if ctx.kerberos_token is not None:
response.headers['WWW-Authenticate'] = ' '.join(['negotiate', ctx.kerberos_token])
return response
elif rc != kerberos.AUTH_GSS_CONTINUE:
return _forbidden()
return _unauthorized_kerberos()
return decorated
def _unauthorized_kerberos():
'''
Indicate that authentication is required
'''
# from https://billstclair.com/html-redirect2.html
return Response(f'Unauthorized! No kerberos auth provided. Trying ldap automatically in a moment.', 401, {'WWW-Authenticate': 'Negotiate'})
@app.route("/")
def index():
return render_template('index.html')
@app.route("/open/")
def open():
header = request.headers.get("Authorization")
if header:
print("Header!")
token = ''.join(header.split()[1:])
print("token",token)
print("something")
return "