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.

116 lines
3.5 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
  4. from flask_simpleldap import LDAP
  5. from flask_bootstrap import Bootstrap
  6. import short_url
  7. import os
  8. import sqlite3
  9. app = Flask(__name__)
  10. Bootstrap(app)
  11. app.secret_key = 'asdf'
  12. app.debug = True
  13. # Base
  14. app.config['LDAP_REALM_NAME'] = 'OpenLDAP Authentication'
  15. app.config['LDAP_HOST'] = os.environ.get('LDAP_HOST')
  16. app.config['LDAP_BASE_DN'] = os.environ.get('LDAP_BASE_DN')
  17. app.config['LDAP_USERNAME'] = os.environ.get('LDAP_USERNAME')
  18. app.config['LDAP_PASSWORD'] = os.environ.get('LDAP_PASSWORD')
  19. # OpenLDAP
  20. app.config['LDAP_OBJECTS_DN'] = 'dn'
  21. app.config['LDAP_OPENLDAP'] = True
  22. app.config['LDAP_USER_OBJECT_FILTER'] = '(&(objectclass=posixAccount)(uid=%s))'
  23. short_domain = os.environ.get('SHORT_DOMAIN')
  24. ldap = LDAP(app)
  25. server = Server(app.config['LDAP_HOST'])
  26. conn = Connection(server, app.config['LDAP_USERNAME'], app.config['LDAP_PASSWORD'], auto_bind=True)
  27. @app.before_request
  28. def before_request():
  29. g.user = None
  30. if 'user_id' in session:
  31. # This is where you'd query your database to get the user info.
  32. g.user = {}
  33. @app.route('/')
  34. @ldap.login_required
  35. def index():
  36. user_dict = ldap.get_object_details(session['user_id'])
  37. if 'user_id' in session:
  38. user = {'dn': 'cn={},cn=usergroup,ou=users,dc=technicalincompetence,dc=club'.format(user_dict['cn'][0].decode('ascii')),
  39. 'firstName': user_dict['givenName'][0].decode('ascii'),
  40. 'lastName': user_dict['sn'][0].decode('ascii'),
  41. 'email': user_dict['mail'][0].decode('ascii'),
  42. 'userName': user_dict['uid'][0].decode('ascii'),
  43. }
  44. return render_template('profile.j2', user = user, short_domain = short_domain)
  45. @app.route('/login', methods=['GET', 'POST'])
  46. def login():
  47. if g.user:
  48. return redirect(url_for('index'))
  49. if request.method == 'POST':
  50. user = request.form['user']
  51. passwd = request.form['passwd']
  52. test = ldap.bind_user(user, passwd)
  53. if test is None or passwd == '':
  54. return render_template('login.j2', error='Invalid credentials')
  55. else:
  56. session['user_id'] = request.form['user']
  57. session['passwd'] = request.form['passwd']
  58. return redirect('/')
  59. return render_template('login.j2')
  60. @ldap.login_required
  61. @app.route('/shorten', methods=['POST'])
  62. def shorten_url():
  63. if request.method == 'POST':
  64. url = request.form['url']
  65. conn = sqlite3.connect('links/links.db')
  66. c = conn.cursor()
  67. if url is not None and len(url) > 0:
  68. c.execute("INSERT INTO links (url) VALUES (?)", (url,))
  69. c.execute("SELECT * FROM links WHERE url=?", (url,))
  70. row = c.fetchone()
  71. print(row[0])
  72. conn.commit()
  73. conn.close()
  74. url_fragment = short_url.encode_url(row[0])
  75. return "Your shortened link is <a href='{}/{}'>{}/{}</a>".format(short_domain, url_fragment, short_domain, url_fragment)
  76. conn.commit()
  77. conn.close()
  78. return 'Error'
  79. @app.route('/l/<url>')
  80. def expand_url(url):
  81. idx = short_url.decode_url(url)
  82. conn = sqlite3.connect('links/links.db')
  83. c = conn.cursor()
  84. c.execute("SELECT * FROM links WHERE id=?", (idx,))
  85. out_link = c.fetchone()[1]
  86. return redirect(out_link)
  87. @app.route('/logout')
  88. def logout():
  89. session.pop('user_id', None)
  90. return redirect(url_for('index'))
  91. if __name__ == '__main__':
  92. app.run()