Youen
2 years ago
9 changed files with 90 additions and 4 deletions
@ -1,3 +1,3 @@ |
|||||||
/venv |
/venv |
||||||
__pycache__ |
__pycache__ |
||||||
/data/doc |
/data |
||||||
|
@ -0,0 +1,16 @@ |
|||||||
|
<!DOCTYPE html> |
||||||
|
<html lang="fr"> |
||||||
|
|
||||||
|
<head> |
||||||
|
<meta charset="utf-8"> |
||||||
|
<title>Documentation (admin)</title> |
||||||
|
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> |
||||||
|
</head> |
||||||
|
|
||||||
|
<body> |
||||||
|
<form method="POST"> |
||||||
|
<label for="password">Mot de passe admin :</label> <input type="password" id="password" name="password"><br/> |
||||||
|
|
||||||
|
<input type="submit"/> |
||||||
|
</form> |
||||||
|
</body> |
@ -1,9 +1,49 @@ |
|||||||
from flask import Blueprint, render_template |
import os |
||||||
|
import random |
||||||
|
import string |
||||||
|
from flask import Blueprint, render_template, session, redirect, url_for, request |
||||||
|
|
||||||
from data.document import Document |
from data.document import Document |
||||||
|
import app_globals |
||||||
|
|
||||||
bp = Blueprint('admin', __name__, url_prefix='/admin') |
bp = Blueprint('admin', __name__, url_prefix='/admin') |
||||||
|
|
||||||
|
def get_admin_password(): |
||||||
|
password_path = app_globals.data_root_dir + '/admin-password' |
||||||
|
if not os.path.isfile(password_path): |
||||||
|
new_password = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(12)) |
||||||
|
with open(password_path, 'wb') as f: |
||||||
|
f.write(new_password.encode()) |
||||||
|
with open(password_path, 'rb') as f: |
||||||
|
result = f.read().decode().replace('\n', '').replace('\r', '') |
||||||
|
if len(result) < 12: |
||||||
|
raise Exception("Internal error: insecure password") |
||||||
|
return result |
||||||
|
|
||||||
|
@bp.before_app_request |
||||||
|
def authenticate(): |
||||||
|
print(request.path) |
||||||
|
if request.path == '/admin/login' or request.path.startswith('/doc/') or request.path == '/api/doc/build': |
||||||
|
return |
||||||
|
|
||||||
|
authenticated = session.get('authenticated') |
||||||
|
if not authenticated: |
||||||
|
return redirect(url_for('admin.login'), code=302) |
||||||
|
|
||||||
@bp.route('/') |
@bp.route('/') |
||||||
def index(): |
def index(): |
||||||
return render_template("admin/index.html", documents=Document.list()) |
return render_template("admin/index.html", documents=Document.list()) |
||||||
|
|
||||||
|
@bp.route('/login', methods=['GET', 'POST']) |
||||||
|
def login(): |
||||||
|
correct_password = get_admin_password() |
||||||
|
if request.method == 'POST': |
||||||
|
password = request.form.get('password') |
||||||
|
if password == correct_password: |
||||||
|
session.clear() |
||||||
|
session['authenticated'] = True |
||||||
|
return redirect(url_for('admin.index'), code=302) |
||||||
|
else: |
||||||
|
raise Exception("Incorrect password") |
||||||
|
else: |
||||||
|
return render_template("admin/login.html") |
Loading…
Reference in new issue