2015-06-16 21:57:44 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from flask import Flask, render_template, redirect, url_for, session
|
|
|
|
from flask_wtf import Form
|
2015-06-17 20:13:55 +02:00
|
|
|
import ldap
|
|
|
|
from redis import Redis
|
2015-06-17 20:22:52 +02:00
|
|
|
import uuid
|
2016-02-10 17:03:09 +01:00
|
|
|
from wtforms.fields import IntegerField, PasswordField, SelectField, StringField, SubmitField
|
2016-02-10 17:00:40 +01:00
|
|
|
from wtforms.validators import EqualTo, Required
|
2015-06-16 21:57:44 +02:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2015-06-17 20:22:52 +02:00
|
|
|
app.config.from_pyfile('config.cfg')
|
2015-06-16 21:57:44 +02:00
|
|
|
app.jinja_env.trim_blocks = True
|
|
|
|
app.jinja_env.lstrip_blocks = True
|
|
|
|
|
2015-06-17 20:22:52 +02:00
|
|
|
rdb = Redis(host=app.config.get('REDIS_HOST', '127.0.0.1'), password=app.config.get('REDIS_PSWD'))
|
2015-06-17 20:13:55 +02:00
|
|
|
|
|
|
|
|
2015-06-18 19:14:24 +02:00
|
|
|
class ReadonlyStringField(StringField):
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
kwargs.setdefault('readonly', True)
|
|
|
|
return super(ReadonlyStringField, self).__call__(*args, **kwargs)
|
|
|
|
|
2016-02-10 17:03:09 +01:00
|
|
|
class CreateForm(Form):
|
|
|
|
user = StringField('Username', validators = [Required()])
|
|
|
|
uid = IntegerField('User ID', validators = [Required()])
|
|
|
|
gn = StringField('Given Name', validators = [Required()])
|
|
|
|
sn = StringField('Family Name', validators = [Required()])
|
|
|
|
pwd1 = PasswordField('Password', validators = [Required()])
|
|
|
|
pwd2 = PasswordField('Password (repeat)', validators = [Required(), EqualTo('pwd1', "Passwords must match")])
|
|
|
|
submit = SubmitField('Submit')
|
|
|
|
|
2015-06-18 19:14:24 +02:00
|
|
|
class EditForm(Form):
|
|
|
|
user = ReadonlyStringField('Username')
|
2016-02-10 16:56:54 +01:00
|
|
|
pwd1 = PasswordField('New Password', validators = [Required()])
|
|
|
|
pwd2 = PasswordField('New Password (repeat)', validators = [Required(), EqualTo('pwd1', "Passwords must match")])
|
2015-06-18 19:14:24 +02:00
|
|
|
submit = SubmitField('Submit')
|
|
|
|
|
2015-06-17 20:13:55 +02:00
|
|
|
class LoginForm(Form):
|
|
|
|
user = StringField('Username', validators=[Required()])
|
|
|
|
pswd = PasswordField('Password', validators=[Required()])
|
|
|
|
submit = SubmitField('Login')
|
|
|
|
|
2015-06-16 21:57:44 +02:00
|
|
|
|
2016-02-10 17:03:09 +01:00
|
|
|
def isAdmin():
|
|
|
|
return isLoggedin() and rdb.hget(session['uuid'], 'user') in app.config.get('ADMINS', [])
|
|
|
|
|
2015-10-01 16:39:19 +02:00
|
|
|
def isLoggedin():
|
|
|
|
return 'uuid' in session and rdb.exists(session['uuid'])
|
|
|
|
|
|
|
|
|
2016-02-10 17:00:40 +01:00
|
|
|
def buildNav():
|
|
|
|
nav = []
|
2015-10-01 16:39:19 +02:00
|
|
|
if isLoggedin():
|
2016-02-10 17:00:40 +01:00
|
|
|
nav.append('edit')
|
2016-02-10 17:03:09 +01:00
|
|
|
if isAdmin():
|
|
|
|
nav.append('create')
|
2016-02-10 17:00:40 +01:00
|
|
|
nav.append('logout')
|
2015-06-17 20:34:30 +02:00
|
|
|
else:
|
2016-02-10 17:00:40 +01:00
|
|
|
nav.append('login')
|
|
|
|
return nav
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
2015-06-17 20:34:30 +02:00
|
|
|
|
2016-02-10 17:00:40 +01:00
|
|
|
return render_template('index.html', nav=buildNav())
|
2015-06-16 21:57:44 +02:00
|
|
|
|
2015-06-17 20:22:52 +02:00
|
|
|
|
2016-02-10 17:03:09 +01:00
|
|
|
@app.route('/create', methods=['GET', 'POST'])
|
|
|
|
def create():
|
|
|
|
if not isLoggedin():
|
|
|
|
return render_template('error.html', message="You are not logged in. Please log in first.", nav=buildNav())
|
|
|
|
|
|
|
|
form = CreateForm()
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
|
|
|
l = ldap.initialize(app.config.get('LDAP_URI', 'ldaps://127.0.0.1'))
|
|
|
|
try:
|
|
|
|
l.simple_bind_s(rdb.hget(session['uuid'], 'user'), rdb.hget(session['uuid'], 'pswd'))
|
|
|
|
# TODO implement
|
|
|
|
#l.add_s()
|
|
|
|
except:
|
|
|
|
l.unbind_s()
|
|
|
|
else:
|
|
|
|
# TODO display success message
|
|
|
|
l.unbind_s()
|
|
|
|
pass
|
|
|
|
|
|
|
|
return render_template('create.html', form=form, nav=buildNav())
|
|
|
|
|
|
|
|
|
2015-06-18 19:14:24 +02:00
|
|
|
@app.route('/edit', methods=['GET', 'POST'])
|
|
|
|
def edit():
|
2015-10-01 16:39:19 +02:00
|
|
|
if not isLoggedin():
|
2016-02-10 17:00:40 +01:00
|
|
|
return render_template('error.html', message="You are not logged in. Please log in first.", nav=buildNav())
|
2015-06-18 19:14:24 +02:00
|
|
|
|
|
|
|
form = EditForm()
|
2015-09-28 21:27:56 +02:00
|
|
|
user = rdb.hget(session['uuid'], 'user')
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2016-02-10 16:56:54 +01:00
|
|
|
opwd = rdb.hget(session['uuid'], 'pswd')
|
|
|
|
npwd = form.pwd1.data
|
|
|
|
l = ldap.initialize(app.config.get('LDAP_URI', 'ldaps://127.0.0.1'))
|
|
|
|
try:
|
|
|
|
l.simple_bind_s(user, opwd)
|
|
|
|
l.passwd_s(user, opwd, npwd)
|
|
|
|
except ldap.INVALID_CREDENTIALS as e:
|
|
|
|
form.user.errors.append(e.message['desc'])
|
|
|
|
l.unbind_s()
|
2016-02-10 17:00:40 +01:00
|
|
|
return render_template('edit.html', form=form, nav=buildNav())
|
2015-09-28 21:27:56 +02:00
|
|
|
else:
|
2016-02-10 16:56:54 +01:00
|
|
|
# TODO display success message
|
|
|
|
rdb.hset(session['uuid'], 'pswd', npwd)
|
|
|
|
l.unbind_s()
|
|
|
|
return redirect(url_for('index'))
|
2015-09-28 21:27:56 +02:00
|
|
|
|
2015-06-18 19:14:24 +02:00
|
|
|
form.user.data = user
|
2016-02-10 17:00:40 +01:00
|
|
|
return render_template('edit.html', form=form, nav=buildNav())
|
2015-06-18 19:14:24 +02:00
|
|
|
|
2015-09-28 21:27:56 +02:00
|
|
|
|
2015-06-17 20:13:55 +02:00
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
|
|
def login():
|
|
|
|
form = LoginForm()
|
2015-06-17 20:22:52 +02:00
|
|
|
|
2015-06-17 20:13:55 +02:00
|
|
|
if form.validate_on_submit():
|
2015-06-17 20:22:52 +02:00
|
|
|
user = 'cn=' + form.user.data + ',' + app.config.get('LDAP_BASE','')
|
|
|
|
pswd = form.pswd.data
|
|
|
|
l = ldap.initialize(app.config.get('LDAP_URI', 'ldaps://127.0.0.1'))
|
|
|
|
try:
|
|
|
|
l.simple_bind_s(user, pswd)
|
|
|
|
except ldap.INVALID_CREDENTIALS as e:
|
|
|
|
form.pswd.errors.append(e.message['desc'])
|
|
|
|
l.unbind_s()
|
2016-02-10 17:00:40 +01:00
|
|
|
return render_template('login.html', form=form, nav=buildNav())
|
2015-06-17 20:22:52 +02:00
|
|
|
l.unbind_s()
|
|
|
|
|
|
|
|
session['uuid'] = str(uuid.uuid4())
|
|
|
|
credentials = { 'user': user, 'pswd': pswd }
|
|
|
|
rdb.hmset(session['uuid'], credentials)
|
2015-09-28 21:27:56 +02:00
|
|
|
# TODO refactor this and reuse
|
|
|
|
rdb.expire(session['uuid'], app.config.get('SESSION_TIMEOUT', 3600))
|
2015-06-17 20:22:52 +02:00
|
|
|
|
2015-06-17 20:13:55 +02:00
|
|
|
return redirect(url_for('index'))
|
2016-02-10 17:00:40 +01:00
|
|
|
return render_template('login.html', form=form, nav=buildNav())
|
2015-06-16 21:57:44 +02:00
|
|
|
|
2015-06-17 20:22:52 +02:00
|
|
|
|
|
|
|
@app.route('/logout')
|
|
|
|
def logout():
|
2015-06-17 20:34:30 +02:00
|
|
|
if 'uuid' in session:
|
|
|
|
rdb.delete(session['uuid'])
|
2015-06-18 19:14:24 +02:00
|
|
|
del session['uuid']
|
2015-06-17 20:22:52 +02:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
|
2015-06-16 21:57:44 +02:00
|
|
|
if __name__ == '__main__':
|
2015-06-17 20:13:55 +02:00
|
|
|
app.run(host='0.0.0.0', port=5000)
|