Skip to content
Snippets Groups Projects
Commit f351f35e authored by Tamas Gal's avatar Tamas Gal :speech_balloon:
Browse files

Add authentication option. Closes #7

parent 82964e9f
No related branches found
No related tags found
No related merge requests found
from os.path import join
from flask import render_template, send_from_directory
from os.path import join, exists
from functools import wraps
import toml
from flask import render_template, send_from_directory, request, Response
from app import app
CONFIG_PATH = "pipeline.toml"
PLOTS_PATH = "../plots"
USERNAME = None
PASSWORD = None
app.config['FREEZER_DESTINATION'] = '../km3web'
PLOTS = [['dom_activity', 'dom_rates'], ['pmt_rates', 'pmt_hrv'],
......@@ -13,6 +18,42 @@ TRIGGER_PLOTS = [['trigger_rates'], ['trigger_rates_lin']]
K40_PLOTS = [['intradom'], ['angular_k40rate_distribution']]
RTTC_PLOTS = [['rttc']]
if exists(CONFIG_PATH):
config = toml.load(CONFIG_PATH)
if "WebServer" in config:
print("Reading authentication information from '%s'" % CONFIG_PATH)
USERNAME = config["WebServer"]["username"]
PASSWORD = config["WebServer"]["password"]
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
if USERNAME is not None and PASSWORD is not None:
return username == USERNAME and password == PASSWORD
else:
return True
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
@app.after_request
def add_header(r):
......@@ -28,16 +69,19 @@ def add_header(r):
@app.route('/')
@app.route('/index.html')
@requires_auth
def index():
return render_template('plots.html', plots=PLOTS)
@app.route('/ahrs.html')
@requires_auth
def ahrs():
return render_template('plots.html', plots=AHRS_PLOTS)
@app.route('/rttc.html')
@requires_auth
def rttc():
return render_template(
'plots.html',
......@@ -50,6 +94,7 @@ def rttc():
@app.route('/k40.html')
@requires_auth
def k40():
return render_template(
'plots.html',
......@@ -62,11 +107,13 @@ def k40():
@app.route('/trigger.html')
@requires_auth
def trigger():
return render_template('plots.html', plots=TRIGGER_PLOTS)
@app.route('/plots/<path:filename>')
@requires_auth
def custom_static(filename):
print(filename)
filepath = join(app.root_path, PLOTS_PATH)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment