Use environment vars to store config.
This commit is contained in:
23
app.py
23
app.py
@@ -9,7 +9,7 @@ import db
|
|||||||
import forms
|
import forms
|
||||||
import models
|
import models
|
||||||
import const
|
import const
|
||||||
from config import Config, roles
|
from config import Config
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(filename=const.LOG_PATH, level=logging.DEBUG)
|
logging.basicConfig(filename=const.LOG_PATH, level=logging.DEBUG)
|
||||||
@@ -44,7 +44,7 @@ def close_connection(exception):
|
|||||||
|
|
||||||
def set_user_role(data):
|
def set_user_role(data):
|
||||||
"""Set the users role in the flask g object for later usage"""
|
"""Set the users role in the flask g object for later usage"""
|
||||||
g.is_editor = data == "editor"
|
g.is_editor = data == "write"
|
||||||
|
|
||||||
|
|
||||||
def authorize(func):
|
def authorize(func):
|
||||||
@@ -59,17 +59,24 @@ def authorize(func):
|
|||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def get_role(password):
|
||||||
|
if password == Config.WRITE_PW:
|
||||||
|
return "write"
|
||||||
|
if password == Config.READ_PW:
|
||||||
|
return "read"
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
@app.route("/login", methods=["GET", "POST"])
|
@app.route("/login", methods=["GET", "POST"])
|
||||||
def login():
|
def login():
|
||||||
if request.method == "GET":
|
if request.method == "GET":
|
||||||
return render_template("login.html")
|
return render_template("login.html")
|
||||||
else:
|
else:
|
||||||
try:
|
role = get_role(request.form.get("password"))
|
||||||
password = request.form["password"]
|
if not role:
|
||||||
session["role"] = roles()[password]
|
return redirect("/login")
|
||||||
return redirect("/")
|
session["role"] = role
|
||||||
except KeyError:
|
return redirect("/")
|
||||||
return redirect("login")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/logout")
|
@app.route("/logout")
|
||||||
|
|||||||
4
conf.sh
Normal file
4
conf.sh
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
export READ_PW=""
|
||||||
|
export WRITE_PW=""
|
||||||
|
export SECRET_KEY=""
|
||||||
@@ -1,11 +1,7 @@
|
|||||||
import json
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
def roles():
|
|
||||||
with open("roles.json", "r") as f:
|
|
||||||
return json.load(f)
|
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
SECRET_KEY = os.environ.get("ES_SECRET_KEY")
|
SECRET_KEY = os.environ.get("ES_SECRET_KEY")
|
||||||
|
WRITE_PW = os.environ.get("ES_WRITE_PW")
|
||||||
|
READ_PW = os.environ.get("ES_READ_PW")
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"<editor-password>": "editor",
|
|
||||||
"<readonly-password": "readonly"
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user