Skip to content

Quick code issue fixes #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"SQLALCHEMY_DATABASE_URI", "postgresql://postgres:password@database/qstackdb"
)

AUTH0_CLIENT_ID = os.environ.get("AUTH0_CLIENT_ID")
AUTH0_CLIENT_SECRET = os.environ.get("AUTH0_CLIENT_SECRET")
AUTH_USERNAME = os.environ.get("AUTH_USERNAME")
AUTH_PASSWORD = os.environ.get("AUTH_PASSWORD")
AUTH0_DOMAIN = os.environ.get("AUTH0_DOMAIN")
APP_SECRET_KEY = os.environ.get("APP_SECRET_KEY")
MENTOR_PASS = os.environ.get("MENTOR_PASS")
AUTH0_CLIENT_ID = os.environ["AUTH0_CLIENT_ID"]
AUTH0_CLIENT_SECRET = os.environ["AUTH0_CLIENT_SECRET"]
AUTH_USERNAME = os.environ["AUTH_USERNAME"]
AUTH_PASSWORD = os.environ["AUTH_PASSWORD"]
AUTH0_DOMAIN = os.environ["AUTH0_DOMAIN"]
APP_SECRET_KEY = os.environ["APP_SECRET_KEY"]
MENTOR_PASS = os.environ["MENTOR_PASS"]

ENV = os.environ.get("ENVIRONMENT", "development")

Expand Down
36 changes: 21 additions & 15 deletions server/controllers/auth.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from flask import current_app as app, redirect, session, request
from server import db
from authlib.integrations.flask_client import OAuth
from apiflask import APIBlueprint, abort
import functools
from urllib.parse import quote_plus, urlencode
from server.models import User

from apiflask import APIBlueprint, abort
from authlib.integrations.flask_client import OAuth
from flask import current_app as app
from flask import redirect, request, session

from server import db
from server.config import (
FRONTEND_URL,
MENTOR_PASS,
AUTH0_CLIENT_ID,
AUTH0_CLIENT_SECRET,
AUTH0_DOMAIN,
AUTH_USERNAME,
AUTH_PASSWORD
FRONTEND_URL,
MENTOR_PASS,
)
from server.models import User

auth = APIBlueprint("auth", __name__, url_prefix="/auth")
oauth = OAuth(app)
Expand All @@ -29,24 +31,28 @@
)


def is_user_valid(user, valid_roles):
if not user or not user.role:
return False
elif user.role not in valid_roles:
return False
return True


def auth_required_decorator(roles):
"""
middleware for protected routes
"""

def auth_required(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
email = session["user"]["userinfo"]["email"]
user = User.query.filter_by(email=email).first()
if not user or not user.role:
return abort(401)
elif user.role not in roles:
if not is_user_valid(user, roles):
return abort(401)
return func(*args, **kwargs)

wrapper.__name__ = (
func.__name__
) # avoid overwriting wrapper. something about scoping issues
return wrapper

return auth_required
Expand Down