Skip to content

Commit

Permalink
chore: optional basic auth refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoriev committed Sep 2, 2024
1 parent 840152f commit d04b947
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
###### Requirements without Version Specifiers ######
flask
gevent

flask~=3.0.3
gevent~=24.2.1
Werkzeug~=3.0.3
Flask-HTTPAuth~=4.8.0
26 changes: 16 additions & 10 deletions table-cell-auto-height-setter/table_cell_auto_height_setter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@
import re

from flask import Flask, Response, request, json, abort
from flask_httpauth import HTTPBasicAuth
from gevent.pywsgi import WSGIServer
from werkzeug.security import generate_password_hash, check_password_hash

app = Flask(__name__)
auth = HTTPBasicAuth()
users = {
"username1": generate_password_hash("userpass1"),
"username2": generate_password_hash("userpass2")
}


@auth.verify_password
def verify_password(username, password):
if username in users and check_password_hash(users.get(username), password):
return username
return None


logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(levelname)s] - %(message)s')


@app.route("/td-height/<height>", methods=["POST"])
@auth.login_required
def process_html(height):
authorize()
export_params, html = validate_request()

logging.info(f"Changing HTML table cell height to '{height}'")
Expand All @@ -31,15 +46,6 @@ def change_height(html, height):
return re.sub(pattern, lambda m: m.group(1) + height, html)


def authorize():
if args.username and args.password:
auth = request.authorization
if not auth:
abort(Response('No authorization data', 401))
if auth.username != args.username or auth.password != args.password:
abort(Response('Invalid username/password provided', 401))


def validate_request():
export_params_json = request.form.get('exportParams')
if export_params_json is None:
Expand Down

0 comments on commit d04b947

Please sign in to comment.