From 6869ff0cc1c0fede8fa8b3e1af5c57e0b66384c6 Mon Sep 17 00:00:00 2001 From: Alex Huddleston Date: Mon, 2 Mar 2020 22:21:02 -0600 Subject: [PATCH] Move app directory, update docker files, attempt at enforcing a login policy. --- Dockerfile | 4 -- __init__.py | 15 ------- docker-compose.yaml | 7 +--- lib/helpers.py | 17 -------- start_app.sh | 2 +- uwume/__init__.py | 8 ++++ {assets => uwume/assets}/content.txt | 0 {assets => uwume/assets}/home/content.txt | 0 {assets => uwume/assets}/login/content.txt | 0 {lib => uwume/lib}/__init__.py | 0 uwume/lib/classUser.py | 16 ++++++++ uwume/lib/helpers.py | 35 +++++++++++++++++ uwume/loginManager.py | 34 ++++++++++++++++ {static => uwume/static}/assets/favicon.png | Bin {static => uwume/static}/css/home/index.css | 0 {static => uwume/static}/css/index.css | 0 {static => uwume/static}/css/login/index.css | 0 {static => uwume/static}/js/home/index.js | 0 {static => uwume/static}/js/index.js | 0 {static => uwume/static}/js/login/index.js | 0 {static => uwume/static}/materialize/LICENSE | 0 .../static}/materialize/README.md | 0 .../static}/materialize/css/materialize.css | 0 .../materialize/css/materialize.min.css | 0 .../static}/materialize/js/materialize.js | 0 .../static}/materialize/js/materialize.min.js | 0 {templates => uwume/templates}/__init__.py | 0 .../templates}/home/index.html | 0 {templates => uwume/templates}/homepage.html | 0 {templates => uwume/templates}/index.html | 0 .../templates}/login/index.html | 0 views.py => uwume/views.py | 37 ++++++++++++++++++ 32 files changed, 133 insertions(+), 42 deletions(-) delete mode 100644 __init__.py delete mode 100644 lib/helpers.py create mode 100644 uwume/__init__.py rename {assets => uwume/assets}/content.txt (100%) rename {assets => uwume/assets}/home/content.txt (100%) rename {assets => uwume/assets}/login/content.txt (100%) rename {lib => uwume/lib}/__init__.py (100%) create mode 100644 uwume/lib/classUser.py create mode 100644 uwume/lib/helpers.py create mode 100644 uwume/loginManager.py rename {static => uwume/static}/assets/favicon.png (100%) rename {static => uwume/static}/css/home/index.css (100%) rename {static => uwume/static}/css/index.css (100%) rename {static => uwume/static}/css/login/index.css (100%) rename {static => uwume/static}/js/home/index.js (100%) rename {static => uwume/static}/js/index.js (100%) rename {static => uwume/static}/js/login/index.js (100%) rename {static => uwume/static}/materialize/LICENSE (100%) rename {static => uwume/static}/materialize/README.md (100%) rename {static => uwume/static}/materialize/css/materialize.css (100%) rename {static => uwume/static}/materialize/css/materialize.min.css (100%) rename {static => uwume/static}/materialize/js/materialize.js (100%) rename {static => uwume/static}/materialize/js/materialize.min.js (100%) rename {templates => uwume/templates}/__init__.py (100%) rename {templates => uwume/templates}/home/index.html (100%) rename {templates => uwume/templates}/homepage.html (100%) rename {templates => uwume/templates}/index.html (100%) rename {templates => uwume/templates}/login/index.html (100%) rename views.py => uwume/views.py (53%) diff --git a/Dockerfile b/Dockerfile index dc8cff6..d43ca7c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,10 +17,6 @@ RUN useradd -Ums /bin/bash -u 991 uwume # when executing docker run or by specifying volumes in docker-compose.yml WORKDIR /usr/src/uwume -# Copy the __init__.py and views.py files in order to run -COPY views.py views.py -COPY __init__.py __init__.py - RUN chown -R uwume:uwume /usr/src/uwume USER uwume diff --git a/__init__.py b/__init__.py deleted file mode 100644 index ec1f986..0000000 --- a/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -from flask import Flask -from flask_login import LoginManager -from os import urandom -app = Flask(__name__) -app.secret_key = urandom(12).hex() -''' -login_manager = LoginManager() -login_manager.init_app(app) - -@login_manager.user_loader -def load_user(user_id): - return User.get(user_id) -''' - -from . import views \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index ef57041..c4508c4 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -9,14 +9,11 @@ services: user: uwume container_name: uwu_me environment: - - FLASK_APP=/usr/src/uwume/ + - FLASK_APP=/usr/src/uwume/app - FLASK_ENV=development - FLASK_RUN_HOST=0.0.0.0 - FLASK_RUN_PORT=4444 volumes: - - ./assets:/usr/src/uwume/assets - - ./lib:/usr/src/uwume/lib - - ./static:/usr/src/uwume/static - - ./templates:/usr/src/uwume/templates + - ./uwume:/usr/src/uwume/app ports: - 4444:4444 \ No newline at end of file diff --git a/lib/helpers.py b/lib/helpers.py deleted file mode 100644 index 1e997d4..0000000 --- a/lib/helpers.py +++ /dev/null @@ -1,17 +0,0 @@ -from flask import request - -def get_content_text(): - if(str(request.url_rule == '/')): - homepage_text_file = open('assets/content.txt') - homepage_text_file = open(f'assets{request.url_rule}/content.txt') - homepage_text = homepage_text_file.readlines() - try: - homepage_text.remove('\n') - except: - pass - return homepage_text - -def get_static_paths(): - if(str(request.url_rule) == '/'): - return (f'static/css/index.css', f'static/js/index.js') - return (f'static/css{request.url_rule}/index.css', f'static/js{request.url_rule}/index.js') \ No newline at end of file diff --git a/start_app.sh b/start_app.sh index de7f3b3..a0e1b5d 100755 --- a/start_app.sh +++ b/start_app.sh @@ -3,7 +3,7 @@ # You should use this if you want to run on bare-metal and not in a container. # These environment variables should be echoed in the docker-compose.yml -export FLASK_APP=$(pwd) +export FLASK_APP=$(pwd)/uwume export FLASK_ENV=development export FLASK_RUN_HOST=0.0.0.0 export FLASK_RUN_PORT=4444 diff --git a/uwume/__init__.py b/uwume/__init__.py new file mode 100644 index 0000000..dfcffe2 --- /dev/null +++ b/uwume/__init__.py @@ -0,0 +1,8 @@ +from flask import Flask, Response +from flask_login import LoginManager, login_required +from os import urandom + +app = Flask(__name__) +app.secret_key = urandom(12).hex() + +from . import views diff --git a/assets/content.txt b/uwume/assets/content.txt similarity index 100% rename from assets/content.txt rename to uwume/assets/content.txt diff --git a/assets/home/content.txt b/uwume/assets/home/content.txt similarity index 100% rename from assets/home/content.txt rename to uwume/assets/home/content.txt diff --git a/assets/login/content.txt b/uwume/assets/login/content.txt similarity index 100% rename from assets/login/content.txt rename to uwume/assets/login/content.txt diff --git a/lib/__init__.py b/uwume/lib/__init__.py similarity index 100% rename from lib/__init__.py rename to uwume/lib/__init__.py diff --git a/uwume/lib/classUser.py b/uwume/lib/classUser.py new file mode 100644 index 0000000..bbf4830 --- /dev/null +++ b/uwume/lib/classUser.py @@ -0,0 +1,16 @@ +from flask import Flask, Response +from flask_login import LoginManager, UserMixin, login_required + + +class User(UserMixin): + # proxy for a database of users + user_database = {"JohnDoe": ("JohnDoe", "John"), + "JaneDoe": ("JaneDoe", "Jane")} + + def __init__(self, username, password): + self.id = username + self.password = password + + @classmethod + def get(cls, id): + return cls.user_database.get(id) \ No newline at end of file diff --git a/uwume/lib/helpers.py b/uwume/lib/helpers.py new file mode 100644 index 0000000..ebeb318 --- /dev/null +++ b/uwume/lib/helpers.py @@ -0,0 +1,35 @@ +from flask import request +from os import getcwd +from os.path import exists + + +def is_docker(): + return not exists(f'{getcwd()}/app') + + +def get_content_text(): + if(str(request.url_rule == '/')): + if(is_docker()): + homepage_text_file = open(f'{getcwd()}/uwume/assets/content.txt') + else: + homepage_text_file = open(f'{getcwd()}/app/assets/content.txt') + else: + if(is_docker()): + homepage_text_file = open( + f'{getcwd()}/uwume/assets{request.url_rule}/content.txt') + else: + homepage_text_file = open( + f'{getcwd()}/app/assets{request.url_rule}/content.txt') + homepage_text = homepage_text_file.readlines() + try: + homepage_text.remove('\n') + except: + pass + return homepage_text + + +def get_static_paths(): + if(str(request.url_rule) == '/'): + return (f'static/css/index.css', f'static/js/index.js') + else: + return (f'static/css{request.url_rule}/index.css', f'static/js{request.url_rule}/index.js') diff --git a/uwume/loginManager.py b/uwume/loginManager.py new file mode 100644 index 0000000..3d2182d --- /dev/null +++ b/uwume/loginManager.py @@ -0,0 +1,34 @@ +from flask import Response +from flask_login import LoginManager, login_required +try: + from .lib.classUser import User +except: + from lib.classUser import User + + +@login_manager.request_loader +def load_user(request): + token = request.headers.get('Authorization') + if(token is None): + token = request.args.get('token') + + if(token is not None): + username, password = token.split(':') # naive token + user_entry = User.get(username) + if(user_entry is not None): + user = User(user_entry[0], user_entry[1]) + if(user.password == password): + return user + + return None + + +@app.route("/", methods=["GET"]) +def index(): + return Response(response="Hello World!", status=200) + + +@app.route("/protected/", methods=["GET"]) +@login_required +def protected(): + return Response(response="Hello Protected World!", status=200) diff --git a/static/assets/favicon.png b/uwume/static/assets/favicon.png similarity index 100% rename from static/assets/favicon.png rename to uwume/static/assets/favicon.png diff --git a/static/css/home/index.css b/uwume/static/css/home/index.css similarity index 100% rename from static/css/home/index.css rename to uwume/static/css/home/index.css diff --git a/static/css/index.css b/uwume/static/css/index.css similarity index 100% rename from static/css/index.css rename to uwume/static/css/index.css diff --git a/static/css/login/index.css b/uwume/static/css/login/index.css similarity index 100% rename from static/css/login/index.css rename to uwume/static/css/login/index.css diff --git a/static/js/home/index.js b/uwume/static/js/home/index.js similarity index 100% rename from static/js/home/index.js rename to uwume/static/js/home/index.js diff --git a/static/js/index.js b/uwume/static/js/index.js similarity index 100% rename from static/js/index.js rename to uwume/static/js/index.js diff --git a/static/js/login/index.js b/uwume/static/js/login/index.js similarity index 100% rename from static/js/login/index.js rename to uwume/static/js/login/index.js diff --git a/static/materialize/LICENSE b/uwume/static/materialize/LICENSE similarity index 100% rename from static/materialize/LICENSE rename to uwume/static/materialize/LICENSE diff --git a/static/materialize/README.md b/uwume/static/materialize/README.md similarity index 100% rename from static/materialize/README.md rename to uwume/static/materialize/README.md diff --git a/static/materialize/css/materialize.css b/uwume/static/materialize/css/materialize.css similarity index 100% rename from static/materialize/css/materialize.css rename to uwume/static/materialize/css/materialize.css diff --git a/static/materialize/css/materialize.min.css b/uwume/static/materialize/css/materialize.min.css similarity index 100% rename from static/materialize/css/materialize.min.css rename to uwume/static/materialize/css/materialize.min.css diff --git a/static/materialize/js/materialize.js b/uwume/static/materialize/js/materialize.js similarity index 100% rename from static/materialize/js/materialize.js rename to uwume/static/materialize/js/materialize.js diff --git a/static/materialize/js/materialize.min.js b/uwume/static/materialize/js/materialize.min.js similarity index 100% rename from static/materialize/js/materialize.min.js rename to uwume/static/materialize/js/materialize.min.js diff --git a/templates/__init__.py b/uwume/templates/__init__.py similarity index 100% rename from templates/__init__.py rename to uwume/templates/__init__.py diff --git a/templates/home/index.html b/uwume/templates/home/index.html similarity index 100% rename from templates/home/index.html rename to uwume/templates/home/index.html diff --git a/templates/homepage.html b/uwume/templates/homepage.html similarity index 100% rename from templates/homepage.html rename to uwume/templates/homepage.html diff --git a/templates/index.html b/uwume/templates/index.html similarity index 100% rename from templates/index.html rename to uwume/templates/index.html diff --git a/templates/login/index.html b/uwume/templates/login/index.html similarity index 100% rename from templates/login/index.html rename to uwume/templates/login/index.html diff --git a/views.py b/uwume/views.py similarity index 53% rename from views.py rename to uwume/views.py index 4981a8d..ceac974 100644 --- a/views.py +++ b/uwume/views.py @@ -1,11 +1,35 @@ try: from .lib.helpers import get_static_paths, get_content_text + from .lib.classUser import User except: + from lib.classUser import User from lib.helpers import get_static_paths, get_content_text from . import app from flask import Flask, render_template, redirect, url_for, request +from flask_login import LoginManager, UserMixin, login_required + +login_manager = LoginManager() +login_manager.init_app(app) +@login_manager.user_loader +def load_user(request): + token = request.headers.get('Authorization') + if(token is None): + token = request.args.get('token') + + if(token is not None): + username, password = token.split(':') # naive token + user_entry = User.get(username) + if(user_entry is not None): + user = User(user_entry[0], user_entry[1]) + if(user.password == password): + return user + + return None + + +@login_required @app.route('/home', methods=['GET']) def home(): return render_template('home/index.html', user='admin', static_paths=get_static_paths(), content_text=get_content_text()) @@ -30,3 +54,16 @@ def hello(): def main(): app.run(host='0.0.0.0', debug=True) + + +''' +@app.route("/", methods=["GET"]) +def index(): + return Response(response="Hello World!", status=200) + + +@app.route("/protected/", methods=["GET"]) +@login_required +def protected(): + return Response(response="Hello Protected World!", status=200) +'''