Skip to content

Commit

Permalink
Merge branch '5-oauth-authentication' into milestone-1-staging
Browse files Browse the repository at this point in the history
merging together auth and api branches for staging before merging into develop
  • Loading branch information
robinpdev committed Mar 14, 2024
2 parents 2d61839 + 8708c4f commit 86938f1
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 23 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ RUN chmod +x /usr/src/app/entrypoint.sh
# copy manage.py
COPY manage.py .

# copy scripts
COPY scripts ./scripts

# copy project
COPY backend ./backend

Expand Down
3 changes: 3 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ COPY ./backend/entrypoint.prod.sh .
RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.prod.sh
RUN chmod +x $APP_HOME/entrypoint.prod.sh

# copy scripts
COPY scripts ./scripts

# copy manage.py
COPY manage.py .

Expand Down
5 changes: 5 additions & 0 deletions backend/entrypoint.prod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ then
echo "PostgreSQL started"
fi

python manage.py migrate

python manage.py runscript push_site

python manage.py collectstatic --noinput


exec "$@"
2 changes: 2 additions & 0 deletions backend/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ fi
#python manage.py makemigrations
python manage.py migrate

python manage.py runscript push_site

python manage.py createsuperuser --noinput --email $DJANGO_SUPERUSER_EMAIL

exec "$@"
2 changes: 2 additions & 0 deletions backend/pigeonhole/apps/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class UserAdmin(BaseUserAdmin):
'password',
'first_name',
'last_name',
'course',
'role',
)}),
)

Expand Down
41 changes: 29 additions & 12 deletions backend/pigeonhole/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
# SECURITY WARNING: don't run with debug turned on in production!

DEBUG = int(os.environ.get("DEBUG", default=0))
FRONTEND_URL = os.environ.get("FRONTEND_URL", default="http://localhost:3000")

ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", default="127.0.0.1").split(" ")
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", default="127.0.0.1 example.com").split(" ")

if not DEBUG:
USE_X_FORWARDED_HOST = True
Expand All @@ -35,22 +36,35 @@
# Application definition

INSTALLED_APPS = [
"corsheaders",
'backend.pigeonhole.apps.courses',
'backend.pigeonhole.apps.groups',
'backend.pigeonhole.apps.projects',
'backend.pigeonhole.apps.submissions',
'backend.pigeonhole.apps.users',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.staticfiles',
'rest_framework',
"corsheaders",
'backend.pigeonhole.apps.users',
'backend.pigeonhole.apps.courses',
'backend.pigeonhole.apps.projects',
'backend.pigeonhole.apps.submissions',
'backend.pigeonhole.apps.groups',
'django_extensions',
'drf_yasg',
'microsoft_auth',
'rest_framework',
]

AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'microsoft_auth.backends.MicrosoftAuthenticationBackend',
]

MICROSOFT_AUTH_CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID")
MICROSOFT_AUTH_CLIENT_SECRET = os.environ.get("OAUTH_CLIENT_SECRET")
MICROSOFT_AUTH_TENANT_ID = os.environ.get("OAUTH_TENANT_ID")
MICROSOFT_AUTH_LOGIN_TYPE = 'ma'

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down Expand Up @@ -84,14 +98,15 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'microsoft_auth.context_processors.microsoft',
],
},
},
Expand Down Expand Up @@ -171,3 +186,5 @@
}
}
}

SITE_URL = "http://localhost:8000"
24 changes: 16 additions & 8 deletions backend/pigeonhole/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.urls import include, path as urlpath
from django.shortcuts import redirect
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import routers, permissions
Expand Down Expand Up @@ -33,16 +34,23 @@
router.register(r'groups', GroupViewSet)
router.register(r'submissions', SubmissionsViewset)

def to_frontend(request, path):
return redirect(f"{settings.FRONTEND_URL}/{path}")

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
path("admin/", admin.site.urls),
path('auth/login/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('auth/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
urlpath('', include(router.urls)),
urlpath('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
urlpath('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
urlpath('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
urlpath("admin/", admin.site.urls),
urlpath('auth/login/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
urlpath('auth/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
urlpath('microsoft/', include('microsoft_auth.urls', namespace='microsoft')),
urlpath('redirect/<path:path>', to_frontend, name='redirect'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)



urlpatterns += router.urls
6 changes: 4 additions & 2 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Django~=4.2.11
coverage~=6.3
Django~=5.0.2
django-cors-headers~=3.14.0
django-extensions==3.2.3
django_microsoft_auth==3.0.1
djangorestframework-simplejwt~=5.2.2
djangorestframework~=3.14.0
drf-yasg==1.21.7
flake8==7.0.0
psycopg2-binary~=2.9.5
pytz~=2022.7.1
pyyaml==6.0.1
uritemplate==4.1.1
drf-yasg==1.21.7
17 changes: 17 additions & 0 deletions backend/templates/microsoft/auth_callback.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% load i18n %}

<html>
<body>
<p>{% trans "This window should automatically close BUT ITs in microsoft. If it does not, it should be save to close after a few seconds." %}</p>
<pre>{{ message }}</pre>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
// post message to parent window
console.log("posting message to {{ base_url }}");
//console.log(window.parent.opener);
window.parent.postMessage({{ message }}, "{{ base_url }}");
window.close();
});
</script>
</body>
</html>
1 change: 1 addition & 0 deletions frontend/src/app/components/CASButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const CASButton = () => {
const handleCASLogin = (): void => {
// Implement CAS login logic here
console.log('Login with CAS');
window.location.href = "http://localhost:8000/microsoft/to-auth-redirect?next=/redirect/homepage"
};

return (
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/auth/auth-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AuthAgent {

login(username, password) {
return axios
.post("http://127.0.0.1:8000/auth/login/", {
.post("http://localhost:8000/auth/login/", {
username,
password
})
Expand Down
11 changes: 11 additions & 0 deletions scripts/push_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os

from django.contrib.sites.models import Site


def run():
debug = int(os.environ.get("DEBUG", default=0))
domain = 'localhost:8000' if debug else 'sel2-1.ugent.be'
print(domain)
Site.objects.all().delete()
Site.objects.create(pk=1, domain=domain, name='localhost')

0 comments on commit 86938f1

Please sign in to comment.