You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.8 KiB

4 years ago
  1. import ldap as l
  2. from ldap3 import Server, Connection, ALL, MODIFY_REPLACE
  3. from flask import Flask, g, request, session, redirect, url_for, render_template, send_from_directory
  4. from flask_simpleldap import LDAP
  5. from flask_bootstrap import Bootstrap
  6. import os
  7. from flask_cache_buster import CacheBuster
  8. app = Flask(__name__)
  9. Bootstrap(app)
  10. app.secret_key = 'asdf'
  11. app.debug = True
  12. # Base
  13. app.config['LDAP_REALM_NAME'] = 'OpenLDAP Authentication'
  14. app.config['LDAP_HOST'] = os.environ.get('LDAP_HOST')
  15. app.config['LDAP_BASE_DN'] = os.environ.get('LDAP_BASE_DN')
  16. app.config['LDAP_USERNAME'] = os.environ.get('LDAP_USERNAME')
  17. app.config['LDAP_PASSWORD'] = os.environ.get('LDAP_PASSWORD')
  18. # OpenLDAP
  19. app.config['LDAP_OBJECTS_DN'] = 'dn'
  20. app.config['LDAP_OPENLDAP'] = True
  21. app.config['LDAP_USER_OBJECT_FILTER'] = '(&(objectclass=posixAccount)(uid=%s))'
  22. ldap = LDAP(app)
  23. config = {
  24. 'extensions': ['.js', '.css', '.csv'],
  25. 'hash_size': 10
  26. }
  27. cache_buster = CacheBuster(config=config)
  28. cache_buster.register_cache_buster(app)
  29. server = Server(app.config['LDAP_HOST'])
  30. conn = Connection(server, app.config['LDAP_USERNAME'], app.config['LDAP_PASSWORD'], auto_bind=True)
  31. @app.before_request
  32. def before_request():
  33. g.user = None
  34. if 'user_id' in session:
  35. # This is where you'd query your database to get the user info.
  36. g.user = {}
  37. @app.route("/manifest.json")
  38. def manifest():
  39. return send_from_directory('./', 'manifest.json')
  40. @app.route('/')
  41. @ldap.login_required
  42. def index():
  43. user_dict = ldap.get_object_details(session['user_id'])
  44. if 'user_id' in session:
  45. user = {'dn': 'cn={},cn=usergroup,ou=users,dc=technicalincompetence,dc=club'.format(user_dict['cn'][0].decode('ascii')),
  46. 'firstName': user_dict['givenName'][0].decode('ascii'),
  47. 'lastName': user_dict['sn'][0].decode('ascii'),
  48. 'email': user_dict['mail'][0].decode('ascii'),
  49. 'userName': user_dict['uid'][0].decode('ascii'),
  50. }
  51. return render_template('home.j2')
  52. @app.route('/about')
  53. @ldap.login_required
  54. def about():
  55. return render_template('about.j2')
  56. @app.route('/login', methods=['GET', 'POST'])
  57. def login():
  58. if g.user:
  59. return redirect(url_for('index'))
  60. if request.method == 'POST':
  61. user = request.form['user']
  62. passwd = request.form['passwd']
  63. test = ldap.bind_user(user, passwd)
  64. if test is None or passwd == '':
  65. return render_template('login.j2', error='Invalid credentials')
  66. else:
  67. session['user_id'] = request.form['user']
  68. session['passwd'] = request.form['passwd']
  69. return redirect('/')
  70. return render_template('login.j2')
  71. @app.route('/logout')
  72. def logout():
  73. session.pop('user_id', None)
  74. return redirect(url_for('index'))
  75. if __name__ == '__main__':
  76. app.run()