|
@ -1,12 +1,12 @@ |
|
|
import logging |
|
|
import logging |
|
|
import os |
|
|
import os |
|
|
import uuid |
|
|
import uuid |
|
|
|
|
|
from logging.config import dictConfig |
|
|
from urllib.parse import urlparse, urljoin |
|
|
from urllib.parse import urlparse, urljoin |
|
|
|
|
|
|
|
|
import flask |
|
|
import flask |
|
|
from flask import Flask, redirect, render_template, request, g, flash |
|
|
|
|
|
from flask_login import LoginManager, login_required, login_user, logout_user, UserMixin, \ |
|
|
|
|
|
current_user |
|
|
|
|
|
|
|
|
from flask import Flask, redirect, render_template, request, g, flash, url_for |
|
|
|
|
|
from flask_login import LoginManager, login_required, login_user, logout_user, UserMixin |
|
|
from flask_wtf import CSRFProtect |
|
|
from flask_wtf import CSRFProtect |
|
|
|
|
|
|
|
|
from db import get_db, get_registration_codes, add_registration_code, \ |
|
|
from db import get_db, get_registration_codes, add_registration_code, \ |
|
@ -14,15 +14,36 @@ from db import get_db, get_registration_codes, add_registration_code, \ |
|
|
add_registered_user |
|
|
add_registered_user |
|
|
from forms import RegistrationForm, LoginForm, RegistrationCodeForm, \ |
|
|
from forms import RegistrationForm, LoginForm, RegistrationCodeForm, \ |
|
|
ExpireRegistrationCodeForm |
|
|
ExpireRegistrationCodeForm |
|
|
|
|
|
from register_new_matrix_user import register_new_user |
|
|
|
|
|
|
|
|
csrf = CSRFProtect() |
|
|
csrf = CSRFProtect() |
|
|
login_manager = LoginManager() |
|
|
login_manager = LoginManager() |
|
|
|
|
|
|
|
|
|
|
|
dictConfig({ |
|
|
|
|
|
'version': 1, |
|
|
|
|
|
'disable_existing_loggers': False, |
|
|
|
|
|
'formatters': {'default': { |
|
|
|
|
|
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', |
|
|
|
|
|
}}, |
|
|
|
|
|
'handlers': { |
|
|
|
|
|
'wsgi': { |
|
|
|
|
|
'class': 'logging.StreamHandler', |
|
|
|
|
|
'stream': 'ext://flask.logging.wsgi_errors_stream', |
|
|
|
|
|
'formatter': 'default' |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
'root': { |
|
|
|
|
|
'level': 'INFO', |
|
|
|
|
|
'handlers': ['wsgi'] |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__) |
|
|
log = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_db(flask_app): |
|
|
def init_db(flask_app): |
|
|
with flask_app.app_context(): |
|
|
with flask_app.app_context(): |
|
|
|
|
|
log.info("Initializing DB") |
|
|
db = get_db() |
|
|
db = get_db() |
|
|
with flask_app.open_resource('schema.sql', mode='r') as f: |
|
|
with flask_app.open_resource('schema.sql', mode='r') as f: |
|
|
db.cursor().executescript(f.read()) |
|
|
db.cursor().executescript(f.read()) |
|
@ -34,12 +55,16 @@ def create_app(): |
|
|
flask_app = Flask(__name__) |
|
|
flask_app = Flask(__name__) |
|
|
|
|
|
|
|
|
flask_app.config.update(dict( |
|
|
flask_app.config.update(dict( |
|
|
|
|
|
APPLICATION_ROOT=os.getenv("APPLICATION_ROOT", "/"), |
|
|
ADMIN_TOKEN=os.getenv("ADMIN_TOKEN", uuid.uuid4().__str__()), |
|
|
ADMIN_TOKEN=os.getenv("ADMIN_TOKEN", uuid.uuid4().__str__()), |
|
|
SECRET_KEY=os.getenv("SECRET_KEY", "changeme"), |
|
|
SECRET_KEY=os.getenv("SECRET_KEY", "changeme"), |
|
|
WTF_CSRF_SECRET_KEY=os.getenv("CSRF_SECRET_KEY", "csrf_changeme") |
|
|
|
|
|
|
|
|
WTF_CSRF_SECRET_KEY=os.getenv("CSRF_SECRET_KEY", "csrf_changeme"), |
|
|
|
|
|
MATRIX_HOMESERVER=os.getenv("MATRIX_HOMESERVER"), |
|
|
|
|
|
MATRIX_SHARED_SECRET=os.getenv("MATRIX_SHARED_SECRET"), |
|
|
|
|
|
REGISTRATION_SUCCESS_REDIRECT=os.getenv("REGISTRATION_SUCCESS_REDIRECT") |
|
|
)) |
|
|
)) |
|
|
|
|
|
|
|
|
print("Admin Token: %s" % flask_app.config.get("ADMIN_TOKEN")) |
|
|
|
|
|
|
|
|
log.info("Admin Token: %s" % flask_app.config.get("ADMIN_TOKEN")) |
|
|
|
|
|
|
|
|
csrf.init_app(flask_app) |
|
|
csrf.init_app(flask_app) |
|
|
|
|
|
|
|
@ -48,11 +73,15 @@ def create_app(): |
|
|
|
|
|
|
|
|
init_db(flask_app) |
|
|
init_db(flask_app) |
|
|
|
|
|
|
|
|
|
|
|
log.info("Application ready") |
|
|
|
|
|
|
|
|
return flask_app |
|
|
return flask_app |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = create_app() |
|
|
app = create_app() |
|
|
|
|
|
|
|
|
|
|
|
log.info("Bound reverse proxy wsgi app") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def flash_form_errors(form): |
|
|
def flash_form_errors(form): |
|
|
if hasattr(form, 'errors') and len(form.errors) > 0: |
|
|
if hasattr(form, 'errors') and len(form.errors) > 0: |
|
@ -82,6 +111,13 @@ def is_safe_url(target): |
|
|
return test_url.scheme in ('http', 'https') and ref_url.netloc == test_url.netloc |
|
|
return test_url.scheme in ('http', 'https') and ref_url.netloc == test_url.netloc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_successful_registration_redirect(): |
|
|
|
|
|
target = app.config.get('REGISTRATION_SUCCESS_REDIRECT') |
|
|
|
|
|
if target is None or not target.startswith('http'): |
|
|
|
|
|
return url_for('index', _external=True) |
|
|
|
|
|
return target |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_manager.user_loader |
|
|
@login_manager.user_loader |
|
|
def load_user(user_id): |
|
|
def load_user(user_id): |
|
|
if user_id == "admin": |
|
|
if user_id == "admin": |
|
@ -94,15 +130,33 @@ def load_user(user_id): |
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
@app.route('/') |
|
|
def index(): |
|
|
def index(): |
|
|
return redirect('/register') |
|
|
|
|
|
|
|
|
return redirect(url_for('registration')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/register', methods=('GET', 'POST')) |
|
|
@app.route('/register', methods=('GET', 'POST')) |
|
|
def registration(): |
|
|
def registration(): |
|
|
form = RegistrationForm() |
|
|
form = RegistrationForm() |
|
|
if form.validate_on_submit(): |
|
|
if form.validate_on_submit(): |
|
|
add_registered_user(form.registration_code.data, form.username.data) |
|
|
|
|
|
return redirect('/success') |
|
|
|
|
|
|
|
|
if app.config.get("MATRIX_HOMESERVER") is None: |
|
|
|
|
|
flash("Matrix Homeserver Currently Unavailable. Please Try Again Later!") |
|
|
|
|
|
return render_template('register.html', form=form) |
|
|
|
|
|
else: |
|
|
|
|
|
if app.config.get("MATRIX_SHARED_SECRET") is None: |
|
|
|
|
|
flash("Registration Configuration Is Invalid. Contact Administrator!") |
|
|
|
|
|
return render_template('register.html', form=form) |
|
|
|
|
|
else: |
|
|
|
|
|
successful = register_new_user( |
|
|
|
|
|
form.username.data, |
|
|
|
|
|
form.password.data, |
|
|
|
|
|
app.config.get("MATRIX_HOMESERVER"), |
|
|
|
|
|
app.config.get("MATRIX_SHARED_SECRET") |
|
|
|
|
|
) |
|
|
|
|
|
if successful: |
|
|
|
|
|
add_registered_user(form.registration_code.data, form.username.data) |
|
|
|
|
|
return redirect(get_successful_registration_redirect()) |
|
|
|
|
|
else: |
|
|
|
|
|
flash("Registration Failure. Contact Administrator!") |
|
|
|
|
|
return render_template('register.html', form=form) |
|
|
|
|
|
|
|
|
flash_form_errors(form) |
|
|
flash_form_errors(form) |
|
|
|
|
|
|
|
@ -131,11 +185,11 @@ def admin_add_registration_code(): |
|
|
expiration_time = form.expiration_time.data |
|
|
expiration_time = form.expiration_time.data |
|
|
max_usages = form.max_usages.data |
|
|
max_usages = form.max_usages.data |
|
|
add_registration_code(expiration_time, max_usages) |
|
|
add_registration_code(expiration_time, max_usages) |
|
|
redirect('/admin') |
|
|
|
|
|
|
|
|
redirect(url_for('admin_index', _external=True)) |
|
|
|
|
|
|
|
|
flash_form_errors(form) |
|
|
flash_form_errors(form) |
|
|
|
|
|
|
|
|
return redirect('/admin') |
|
|
|
|
|
|
|
|
return redirect(url_for('admin_index', _external=True)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/admin/expire_registration_code', methods=['POST']) |
|
|
@app.route('/admin/expire_registration_code', methods=['POST']) |
|
@ -147,17 +201,16 @@ def admin_expire_registration_code(): |
|
|
expire_registration_code(form.registration_code.data) |
|
|
expire_registration_code(form.registration_code.data) |
|
|
elif form.delete.data: |
|
|
elif form.delete.data: |
|
|
delete_registration_code(form.registration_code.data) |
|
|
delete_registration_code(form.registration_code.data) |
|
|
redirect('/admin') |
|
|
|
|
|
|
|
|
redirect(url_for('admin_index', _external=True)) |
|
|
|
|
|
|
|
|
flash_form_errors(form) |
|
|
flash_form_errors(form) |
|
|
|
|
|
|
|
|
return redirect('/admin') |
|
|
|
|
|
|
|
|
return redirect(url_for('admin_index', _external=True)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/admin/login', methods=('GET', 'POST')) |
|
|
@app.route('/admin/login', methods=('GET', 'POST')) |
|
|
def admin_login(): |
|
|
def admin_login(): |
|
|
form = LoginForm() |
|
|
form = LoginForm() |
|
|
tmp = current_user |
|
|
|
|
|
if form.validate_on_submit(): |
|
|
if form.validate_on_submit(): |
|
|
user = load_user(form.username.data) |
|
|
user = load_user(form.username.data) |
|
|
if user is not None: |
|
|
if user is not None: |
|
@ -172,7 +225,7 @@ def admin_login(): |
|
|
if next_loc is not None: |
|
|
if next_loc is not None: |
|
|
return redirect(next_loc) |
|
|
return redirect(next_loc) |
|
|
else: |
|
|
else: |
|
|
return redirect('/admin') |
|
|
|
|
|
|
|
|
return redirect(url_for('admin_index', _external=True)) |
|
|
|
|
|
|
|
|
flash_form_errors(form) |
|
|
flash_form_errors(form) |
|
|
|
|
|
|
|
@ -184,7 +237,7 @@ def admin_login(): |
|
|
def admin_logout(): |
|
|
def admin_logout(): |
|
|
logout_user() |
|
|
logout_user() |
|
|
flask.flash('Logged out successfully.') |
|
|
flask.flash('Logged out successfully.') |
|
|
return redirect('/') |
|
|
|
|
|
|
|
|
return redirect(url_for('index', _external=True)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.teardown_appcontext |
|
|
@app.teardown_appcontext |
|
|