From a26eb822c63d695df78943216f761e419a89ed83 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Wed, 17 Jun 2015 20:13:55 +0200 Subject: [PATCH] Use ldap/redis. Use external configuration file (example provided). Prepare login. --- README.md | 2 ++ config.cfg.example | 5 +++++ index.py | 21 +++++++++++++++++++-- templates/_helpers.html | 3 +++ templates/index.html | 3 +-- templates/login.html | 15 +++++++++++++++ 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 config.cfg.example create mode 100644 templates/_helpers.html create mode 100644 templates/login.html diff --git a/README.md b/README.md index 36e8ab6..dfb3902 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ TBA * py-flask >= 0.10 * py-flask-wtf >= 0.10 +* py-ldap >= 2.4.15 +* py-redis >= 2.10 ## Misc diff --git a/config.cfg.example b/config.cfg.example new file mode 100644 index 0000000..a8c361f --- /dev/null +++ b/config.cfg.example @@ -0,0 +1,5 @@ +DEBUG = True +SECRET_KEY = "CHANGE!ME" + +LDAP_URI = "ldaps://ldap.example.com" +LDAP_BASE = "ou=people,dc=example,dc=com" diff --git a/index.py b/index.py index f543b10..23d48a3 100755 --- a/index.py +++ b/index.py @@ -2,19 +2,36 @@ from flask import Flask, render_template, redirect, url_for, session from flask_wtf import Form +import ldap +from redis import Redis from wtforms.fields import PasswordField, SelectField, StringField, SubmitField from wtforms.validators import Required app = Flask(__name__) -app.config['SECRET_KEY'] = 'CHANGE!ME' +app.config.from_pyfile('index.cfg') app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True +rdb = Redis(host='127.0.0.1', password='foobared') + + +class LoginForm(Form): + user = StringField('Username', validators=[Required()]) + pswd = PasswordField('Password', validators=[Required()]) + submit = SubmitField('Login') + @app.route('/') def index(): return render_template('index.html') +@app.route('/login', methods=['GET', 'POST']) +def login(): + form = LoginForm() + if form.validate_on_submit(): + # TODO implement login with LDAP + return redirect(url_for('index')) + return render_template('login.html', form=form) if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000, debug=True) + app.run(host='0.0.0.0', port=5000) diff --git a/templates/_helpers.html b/templates/_helpers.html new file mode 100644 index 0000000..71a4f54 --- /dev/null +++ b/templates/_helpers.html @@ -0,0 +1,3 @@ +{% macro render_field(field) %} +
{{ field.label(class_="control-label") }}{{ field(class_="form-control") }}{% for error in field.errors %}[ {{ error }} ]{% endfor %}
+{%- endmacro %} diff --git a/templates/index.html b/templates/index.html index 3b8ff08..86329a5 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,7 +4,6 @@ {% endblock %} {% block navigation %} {% endblock %} diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..3b099e3 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,15 @@ +{% from "_helpers.html" import render_field %} +{% extends "base.html" %} +{% block content %} +
+ {{ form.hidden_tag() }} + {{ render_field(form.user) }} + {{ render_field(form.pswd) }} +
{{ form.submit(class_="btn btn-default") }}
+
+{% endblock %} +{% block navigation %} + +{% endblock %}