Move app directory, update docker files, attempt at enforcing a login policy.
This commit is contained in:
parent
0401944b09
commit
6869ff0cc1
32 changed files with 133 additions and 42 deletions
|
@ -17,10 +17,6 @@ RUN useradd -Ums /bin/bash -u 991 uwume
|
||||||
# when executing docker run or by specifying volumes in docker-compose.yml
|
# when executing docker run or by specifying volumes in docker-compose.yml
|
||||||
WORKDIR /usr/src/uwume
|
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
|
RUN chown -R uwume:uwume /usr/src/uwume
|
||||||
USER uwume
|
USER uwume
|
||||||
|
|
||||||
|
|
15
__init__.py
15
__init__.py
|
@ -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
|
|
|
@ -9,14 +9,11 @@ services:
|
||||||
user: uwume
|
user: uwume
|
||||||
container_name: uwu_me
|
container_name: uwu_me
|
||||||
environment:
|
environment:
|
||||||
- FLASK_APP=/usr/src/uwume/
|
- FLASK_APP=/usr/src/uwume/app
|
||||||
- FLASK_ENV=development
|
- FLASK_ENV=development
|
||||||
- FLASK_RUN_HOST=0.0.0.0
|
- FLASK_RUN_HOST=0.0.0.0
|
||||||
- FLASK_RUN_PORT=4444
|
- FLASK_RUN_PORT=4444
|
||||||
volumes:
|
volumes:
|
||||||
- ./assets:/usr/src/uwume/assets
|
- ./uwume:/usr/src/uwume/app
|
||||||
- ./lib:/usr/src/uwume/lib
|
|
||||||
- ./static:/usr/src/uwume/static
|
|
||||||
- ./templates:/usr/src/uwume/templates
|
|
||||||
ports:
|
ports:
|
||||||
- 4444:4444
|
- 4444:4444
|
|
@ -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')
|
|
|
@ -3,7 +3,7 @@
|
||||||
# You should use this if you want to run on bare-metal and not in a container.
|
# 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
|
# 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_ENV=development
|
||||||
export FLASK_RUN_HOST=0.0.0.0
|
export FLASK_RUN_HOST=0.0.0.0
|
||||||
export FLASK_RUN_PORT=4444
|
export FLASK_RUN_PORT=4444
|
||||||
|
|
8
uwume/__init__.py
Normal file
8
uwume/__init__.py
Normal file
|
@ -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
|
16
uwume/lib/classUser.py
Normal file
16
uwume/lib/classUser.py
Normal file
|
@ -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)
|
35
uwume/lib/helpers.py
Normal file
35
uwume/lib/helpers.py
Normal file
|
@ -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')
|
34
uwume/loginManager.py
Normal file
34
uwume/loginManager.py
Normal file
|
@ -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)
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 29 KiB |
|
@ -1,11 +1,35 @@
|
||||||
try:
|
try:
|
||||||
from .lib.helpers import get_static_paths, get_content_text
|
from .lib.helpers import get_static_paths, get_content_text
|
||||||
|
from .lib.classUser import User
|
||||||
except:
|
except:
|
||||||
|
from lib.classUser import User
|
||||||
from lib.helpers import get_static_paths, get_content_text
|
from lib.helpers import get_static_paths, get_content_text
|
||||||
from . import app
|
from . import app
|
||||||
from flask import Flask, render_template, redirect, url_for, request
|
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'])
|
@app.route('/home', methods=['GET'])
|
||||||
def home():
|
def home():
|
||||||
return render_template('home/index.html', user='admin', static_paths=get_static_paths(), content_text=get_content_text())
|
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():
|
def main():
|
||||||
app.run(host='0.0.0.0', debug=True)
|
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)
|
||||||
|
'''
|
Reference in a new issue