diff --git a/.gitpod.Dockerfile b/.gitpod.Dockerfile index 6f59353a..da6fcec8 100644 --- a/.gitpod.Dockerfile +++ b/.gitpod.Dockerfile @@ -1,5 +1,12 @@ FROM gitpod/workspace-full - -USER root -RUN sudo apt-get update -RUN sudo apt-get install -y libgbm-dev gconf-service libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxss1 libxtst6 libappindicator1 libnss3 libasound2 libatk1.0-0 libc6 ca-certificates fonts-liberation lsb-release xdg-utils wget + +USER gitpod + +# Copy the .python-version file into the Docker image to use it during the build +COPY .python-version /home/gitpod/.python-version + +# Install the specific Python version from .python-version and upgrade pip +RUN pyenv install $(cat /home/gitpod/.python-version) && \ + pyenv global $(cat /home/gitpod/.python-version) && \ + eval "$(pyenv init -)" && \ + pip install --upgrade pip \ No newline at end of file diff --git a/.gitpod.yml b/.gitpod.yml index 612085a0..482bfe1c 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -1,5 +1,5 @@ tasks: - - init: pip install -r requirements.txt & pyenv install + - init: pip install -r requirements.txt command: flask init & flask run ports: diff --git a/App/controllers/auth.py b/App/controllers/auth.py index b580b132..a4556d0b 100644 --- a/App/controllers/auth.py +++ b/App/controllers/auth.py @@ -1,43 +1,42 @@ -from flask_login import login_user, login_manager, logout_user, LoginManager -from flask_jwt_extended import create_access_token, jwt_required, JWTManager +from flask_jwt_extended import create_access_token, jwt_required, JWTManager, get_jwt_identity, verify_jwt_in_request from App.models import User -def jwt_authenticate(username, password): +def login(username, password): user = User.query.filter_by(username=username).first() if user and user.check_password(password): return create_access_token(identity=username) return None -def login(username, password): - user = User.query.filter_by(username=username).first() - if user and user.check_password(password): - return user - return None - -def setup_flask_login(app): - login_manager = LoginManager() - login_manager.init_app(app) - - @login_manager.user_loader - def load_user(user_id): - return User.query.get(user_id) - - return login_manager def setup_jwt(app): - jwt = JWTManager(app) - - @jwt.user_identity_loader - def user_identity_lookup(identity): - user = User.query.filter_by(username=identity).one_or_none() - if user: - return user.id - return None - - @jwt.user_lookup_loader - def user_lookup_callback(_jwt_header, jwt_data): - identity = jwt_data["sub"] - return User.query.get(identity) + jwt = JWTManager(app) + + # configure's flask jwt to resolve get_current_identity() to the corresponding user's ID + @jwt.user_identity_loader + def user_identity_lookup(identity): + user = User.query.filter_by(username=identity).one_or_none() + if user: + return user.id + return None - return jwt \ No newline at end of file + @jwt.user_lookup_loader + def user_lookup_callback(_jwt_header, jwt_data): + identity = jwt_data["sub"] + return User.query.get(identity) + return jwt + +# Context processor to make 'is_authenticated' available to all templates +def add_auth_context(app): + @app.context_processor + def inject_user(): + try: + verify_jwt_in_request() + user_id = get_jwt_identity() + current_user = User.query.get(user_id) + is_authenticated = True + except Exception as e: + print(e) + is_authenticated = False + current_user = None + return dict(is_authenticated=is_authenticated, current_user=current_user) \ No newline at end of file diff --git a/App/main.py b/App/main.py index 9cffaf93..d9ebf764 100644 --- a/App/main.py +++ b/App/main.py @@ -12,7 +12,7 @@ from App.controllers import ( setup_jwt, - setup_flask_login + add_auth_context ) from App.views import views @@ -36,12 +36,18 @@ def create_app(config_overrides={}): app.config['SEVER_NAME'] = '0.0.0.0' app.config['PREFERRED_URL_SCHEME'] = 'https' app.config['UPLOADED_PHOTOS_DEST'] = "App/uploads" + app.config['JWT_ACCESS_COOKIE_NAME'] = 'access_token' + app.config["JWT_TOKEN_LOCATION"] = ["cookies", "headers"] + app.config["JWT_COOKIE_SECURE"] = True + app.config["JWT_SECRET_KEY"] = "super-secret" + app.config["JWT_COOKIE_CSRF_PROTECT"] = False CORS(app) + add_auth_context(app) photos = UploadSet('photos', TEXT + DOCUMENTS + IMAGES) configure_uploads(app, photos) add_views(app) init_db(app) setup_jwt(app) - setup_flask_login(app) + app.app_context().push() return app \ No newline at end of file diff --git a/App/models/user.py b/App/models/user.py index 40caf3f4..8efe083a 100644 --- a/App/models/user.py +++ b/App/models/user.py @@ -1,8 +1,7 @@ from werkzeug.security import check_password_hash, generate_password_hash -from flask_login import UserMixin from App.database import db -class User(db.Model, UserMixin): +class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String, nullable=False, unique=True) password = db.Column(db.String(120), nullable=False) @@ -19,7 +18,7 @@ def get_json(self): def set_password(self, password): """Create hashed password.""" - self.password = generate_password_hash(password, method='sha256') + self.password = generate_password_hash(password) def check_password(self, password): """Check hashed password.""" diff --git a/App/templates/index.html b/App/templates/index.html index c4fb60e0..96ec1967 100644 --- a/App/templates/index.html +++ b/App/templates/index.html @@ -6,5 +6,8 @@ {% block content %}
Welcome {{current_user.username}}
+ {% endif %}This is a boileplate flask application which follows the MVC pattern for structuring the project.
{% endblock %} \ No newline at end of file diff --git a/App/templates/layout.html b/App/templates/layout.html index 4f859c1f..0920c5fd 100644 --- a/App/templates/layout.html +++ b/App/templates/layout.html @@ -13,18 +13,42 @@ - + + + + {% with messages = get_flashed_messages() %} {% if messages %} diff --git a/App/templates/message.html b/App/templates/message.html new file mode 100644 index 00000000..e535fad3 --- /dev/null +++ b/App/templates/message.html @@ -0,0 +1,13 @@ +{% extends "layout.html" %} +{% block title %}{{title}}{% endblock %} +{% block page %}{{title}}{% endblock %} + +{{ super() }} + +{% block content %} +