Compare commits

..

2 Commits

Author SHA1 Message Date
Tim Van Baak 6a92249ca2 Add some basic logging around auth 2021-02-18 19:03:45 -08:00
Tim Van Baak c89afe5b94 Change gunicorn to an optional dependency 2021-02-18 19:03:21 -08:00
4 changed files with 31 additions and 2 deletions

5
poetry.lock generated
View File

@ -164,8 +164,11 @@ email = ["email-validator"]
ipaddress = ["ipaddress"]
locale = ["Babel (>=1.3)"]
[extras]
dev = ["gunicorn"]
[metadata]
content-hash = "899cd435824d023104e5842da0f85bf3d82cf4a83137abfe19b7d817e1a12974"
content-hash = "e4930aaf62df0a261424129ab0d89af96ea4e19f80b76c7c6fbcb25c491a213d"
lock-version = "1.0"
python-versions = "^3.8"

View File

@ -14,6 +14,9 @@ gunicorn = {version = "^20.0.4", optional = true}
[tool.poetry.dev-dependencies]
mypy = "^0.800"
[tool.poetry.extras]
dev = ["gunicorn"]
[tool.poetry.scripts]
redstring-check = "redstring.parser:main"
redstring-server = "redstring.server:cli"

View File

@ -1,2 +1,14 @@
import logging
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 json
import logging
import os
import random
import string
@ -47,6 +48,8 @@ class LoginForm(FlaskForm):
CONFIG_ENVVAR = 'REDSTRING_CONFIG'
logger = logging.getLogger(__name__)
app = Flask(__name__)
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
"""
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:
real_password = f.read().strip()
correct = password == real_password
del real_password
if correct:
logger.debug('Authentication successful')
else:
logger.debug('Authentication failed: password incorrect')
return correct
@ -223,4 +232,6 @@ def cli():
def wsgi():
config_path = os.environ.get(CONFIG_ENVVAR) or '/etc/redstring.conf'
config = read_config(app, config_path)
logger.setLevel(logging.DEBUG)
logger.debug(f'Loaded config from {config_path}: {config}')
return app