Add some basic logging around auth

This commit is contained in:
Tim Van Baak 2021-02-18 19:03:45 -08:00
parent c89afe5b94
commit 6a92249ca2
2 changed files with 24 additions and 1 deletions

View File

@ -1,2 +1,14 @@
import logging
import redstring.parser import redstring.parser
import redstring.server import redstring.server
formatter = logging.Formatter('[{asctime}] [{process}] [{levelname}] {message}', '%Y-%m-%d %H:%M:%S %z', style='{')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)

View File

@ -3,6 +3,7 @@ Logic for serving a collection of documents through a web frontend.
""" """
import argparse import argparse
import json import json
import logging
import os import os
import random import random
import string import string
@ -47,6 +48,8 @@ class LoginForm(FlaskForm):
CONFIG_ENVVAR = 'REDSTRING_CONFIG' CONFIG_ENVVAR = 'REDSTRING_CONFIG'
logger = logging.getLogger(__name__)
app = Flask(__name__) app = Flask(__name__)
app.secret_key = ''.join(random.choices(string.ascii_uppercase + string.digits, k=32)) app.secret_key = ''.join(random.choices(string.ascii_uppercase + string.digits, k=32))
@ -60,10 +63,16 @@ def check_password(app, password):
Checks if a password is correct Checks if a password is correct
""" """
password_file = app.config['password_file'] password_file = app.config['password_file']
if not os.path.isfile(password_file):
logger.debug('Authentication failed: no password file found')
with open(password_file) as f: with open(password_file) as f:
real_password = f.read().strip() real_password = f.read().strip()
correct = password == real_password correct = password == real_password
del real_password del real_password
if correct:
logger.debug('Authentication successful')
else:
logger.debug('Authentication failed: password incorrect')
return correct return correct
@ -223,4 +232,6 @@ def cli():
def wsgi(): def wsgi():
config_path = os.environ.get(CONFIG_ENVVAR) or '/etc/redstring.conf' config_path = os.environ.get(CONFIG_ENVVAR) or '/etc/redstring.conf'
config = read_config(app, config_path) config = read_config(app, config_path)
logger.setLevel(logging.DEBUG)
logger.debug(f'Loaded config from {config_path}: {config}')
return app return app