From 1eeba53f785d0d11e270ba3ed823c2be7ef93949 Mon Sep 17 00:00:00 2001 From: Daniel Muckerman Date: Fri, 17 Jul 2020 21:57:26 -0400 Subject: [PATCH] Initial commit --- .gitignore | 18 +++++++ Dockerfile | 13 +++++ app.py | 117 +++++++++++++++++++++++++++++++++++++++++++ links/links.db | Bin 0 -> 16384 bytes requirements.txt | 7 +++ static/style.css | 117 +++++++++++++++++++++++++++++++++++++++++++ templates/login.j2 | 37 ++++++++++++++ templates/profile.j2 | 89 ++++++++++++++++++++++++++++++++ 8 files changed, 398 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 links/links.db create mode 100644 requirements.txt create mode 100644 static/style.css create mode 100644 templates/login.j2 create mode 100644 templates/profile.j2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b235d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +.envrc +.DS_Store +.vscode/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a6d698f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.7 + +LABEL maintainer="Dan Muckerman " + +WORKDIR /project +ADD . /project +RUN rm /project/.envrc +RUN rm -rf /project/env + +RUN apt-get update && apt-get install -y python3-dev libldap2-dev libsasl2-dev libssl-dev + +RUN pip install -r requirements.txt +CMD ["flask","run","--host=0.0.0.0"] \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..8eaa594 --- /dev/null +++ b/app.py @@ -0,0 +1,117 @@ +import ldap as l +from ldap3 import Server, Connection, ALL, MODIFY_REPLACE +from flask import Flask, g, request, session, redirect, url_for, render_template +from flask_simpleldap import LDAP +from flask_bootstrap import Bootstrap +import short_url +import os +import sqlite3 + +app = Flask(__name__) +Bootstrap(app) +app.secret_key = 'asdf' +app.debug = True + +# Base +app.config['LDAP_REALM_NAME'] = 'OpenLDAP Authentication' +app.config['LDAP_HOST'] = os.environ.get('LDAP_HOST') +app.config['LDAP_BASE_DN'] = os.environ.get('LDAP_BASE_DN') +app.config['LDAP_USERNAME'] = os.environ.get('LDAP_USERNAME') +app.config['LDAP_PASSWORD'] = os.environ.get('LDAP_PASSWORD') + +# OpenLDAP +app.config['LDAP_OBJECTS_DN'] = 'dn' +app.config['LDAP_OPENLDAP'] = True +app.config['LDAP_USER_OBJECT_FILTER'] = '(&(objectclass=posixAccount)(uid=%s))' + +short_domain = os.environ.get('SHORT_DOMAIN') + +ldap = LDAP(app) + +server = Server(app.config['LDAP_HOST']) +conn = Connection(server, app.config['LDAP_USERNAME'], app.config['LDAP_PASSWORD'], auto_bind=True) + +@app.before_request +def before_request(): + g.user = None + if 'user_id' in session: + # This is where you'd query your database to get the user info. + g.user = {} + + +@app.route('/') +@ldap.login_required +def index(): + user_dict = ldap.get_object_details(session['user_id']) + + if 'user_id' in session: + user = {'dn': 'cn={},cn=usergroup,ou=users,dc=technicalincompetence,dc=club'.format(user_dict['cn'][0].decode('ascii')), + 'firstName': user_dict['givenName'][0].decode('ascii'), + 'lastName': user_dict['sn'][0].decode('ascii'), + 'email': user_dict['mail'][0].decode('ascii'), + 'userName': user_dict['uid'][0].decode('ascii'), + } + + + return render_template('profile.j2', user = user, short_domain = short_domain) + + +@app.route('/login', methods=['GET', 'POST']) +def login(): + if g.user: + return redirect(url_for('index')) + if request.method == 'POST': + user = request.form['user'] + passwd = request.form['passwd'] + test = ldap.bind_user(user, passwd) + if test is None or passwd == '': + return render_template('login.j2', error='Invalid credentials') + else: + session['user_id'] = request.form['user'] + session['passwd'] = request.form['passwd'] + return redirect('/') + return render_template('login.j2') + + +@ldap.login_required +@app.route('/shorten', methods=['POST']) +def shorten_url(): + if request.method == 'POST': + url = request.form['url'] + conn = sqlite3.connect('links/links.db') + c = conn.cursor() + + if url is not None and len(url) > 0: + c.execute("INSERT INTO links (url) VALUES (?)", (url,)) + + c.execute("SELECT * FROM links WHERE url=?", (url,)) + row = c.fetchone() + print(row[0]) + conn.commit() + conn.close() + url_fragment = short_url.encode_url(row[0]) + return "Your shortened link is {}/{}".format(short_domain, url_fragment, short_domain, url_fragment) + conn.commit() + conn.close() + return 'Error' + + +@app.route('/l/') +def expand_url(url): + idx = short_url.decode_url(url) + conn = sqlite3.connect('links/links.db') + c = conn.cursor() + c.execute("SELECT * FROM links WHERE id=?", (idx,)) + out_link = c.fetchone()[1] + + return redirect(out_link) + + +@app.route('/logout') +def logout(): + session.pop('user_id', None) + return redirect(url_for('index')) + + +if __name__ == '__main__': + app.run() \ No newline at end of file diff --git a/links/links.db b/links/links.db new file mode 100644 index 0000000000000000000000000000000000000000..56393c0f5fd666fb14831a531a451a535c836a3e GIT binary patch literal 16384 zcmeI(Z)?*)90%~bUR+IKp3egM-S-`ee`xl@uq*V&*Avhg>C;k{uxk zAyr)sU6+OF!H;vg4E&+5lGg5G-_r&oYu{*X=Ha7MkJ7&+a+$X?gsY!TH>*FJLf z$K_3rZ>9m3!7*Zip-pg;fu5P$##AOHafKmY;|fB*y_@Qc7TgOLY;QuD2LJMmSb zGVGEHW3BbCufkfcgZe{<=EKKgH@L9$RX zmW%Xeg^|HBmy&)EhrXxJ@;cvK>LtEx`H{{~8S$6P@;q<7Ofr>CbWqiJwSl~1Rwwb2tWV=5P$##AOHafK;R!1xWp(mjG|?% wXe$-QOtbj@Px(ipe<%=u00bZa0SG_<0uX=z1Rwwb2>ee1CS?{a{|8`v1H7ZC7ytkO literal 0 HcmV?d00001 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a01aff4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +Flask==1.1.2 +Flask-Bootstrap4==4.0.2 +Flask-Login==0.5.0 +Flask-SimpleLDAP==1.4.0 +python-ldap==3.2.0 +ldap3==2.7 +short_url==1.2.2 diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..b619e59 --- /dev/null +++ b/static/style.css @@ -0,0 +1,117 @@ +@media (max-width: 991.98px) { + #userNameRow, + #firstNameRow { + margin-bottom: 20px; + } + + #passwordButton { + margin-bottom: 50px; + } +} + +@media (prefers-color-scheme: dark) { + body { + background-color: #111 !important; + color: #eee; + } + + .jumbotron { + background-color: #333 !important; + } + + .modal-content { + background-color: #111 !important; + color: #eee; + } + + .modal-header { + border-bottom: 1px solid #555 !important; + } + + .modal-header .close { + color: #eee !important; + text-shadow: 0 1px 0 #555 !important; + } + + .modal-footer { + border-top: 1px solid #555 !important; + } + + .bg-light { + background-color: #333 !important; + } + + .bg-white { + background-color: #000 !important; + } + + .bg-black { + background-color: #eee !important; + } + + .form-control { + display: block; + width: 100%; + height: calc(1.5em + 0.75rem + 2px); + padding: 0.375rem 0.75rem; + font-size: 1rem; + font-weight: 400; + line-height: 1.5; + color: #dee2e6; + background-color: #000; + background-clip: padding-box; + border: 1px solid #6c757d; + border-radius: 0.25rem; + transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out; + } + + @media (prefers-reduced-motion: reduce) { + .form-control { + transition: none; + } + } + + .form-control::-ms-expand { + background-color: transparent; + border: 0; + } + + .form-control:-moz-focusring { + color: transparent; + text-shadow: 0 0 0 #dee2e6; + } + + .form-control:focus { + color: #dee2e6; + background-color: #191d21; + border-color: #b3d7ff; + outline: 0; + box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25); + } + + .form-control::-webkit-input-placeholder { + color: #6c757d; + opacity: 1; + } + + .form-control::-moz-placeholder { + color: #6c757d; + opacity: 1; + } + + .form-control::-ms-input-placeholder { + color: #6c757d; + opacity: 1; + } + + .form-control::placeholder { + color: #6c757d; + opacity: 1; + } + + .form-control:disabled, + .form-control[readonly] { + background-color: #343a40; + opacity: 1; + } +} diff --git a/templates/login.j2 b/templates/login.j2 new file mode 100644 index 0000000..e6919ce --- /dev/null +++ b/templates/login.j2 @@ -0,0 +1,37 @@ +{% extends "bootstrap/base.html" %} +{% block title %}Technical Incompetence Link Shortener{% endblock %} + +{% block styles %} +{{super()}} + +{% endblock %} + +{% block navbar %} + +{% endblock %} + +{% block content %} +
+
+

Sign in for our awesome service

+

Forgot your password? Too bad! We don't have emails working yet!

+
+
+ + {% if error is defined %} + + {% endif %} + +
+ +
+ +
+ +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/profile.j2 b/templates/profile.j2 new file mode 100644 index 0000000..95c1618 --- /dev/null +++ b/templates/profile.j2 @@ -0,0 +1,89 @@ +{% extends "bootstrap/base.html" %} +{% block title %}Technical Incompetence Link Shortener{% endblock %} + +{% block styles %} +{{super()}} + +{% endblock %} + +{% block navbar %} + +{% endblock %} + +{% block content %} +
+ + +
+
+
+ + +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} +{{ super() }} + +{% endblock %} \ No newline at end of file