diff --git a/app/app/settings.py b/app/app/settings.py index 699f9dfc..00504986 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -50,6 +50,7 @@ INSTALLED_APPS = [ 'widget_tweaks', 'rest_framework', 'django_filters', + 'social_django', ] MIDDLEWARE = [ @@ -60,6 +61,7 @@ MIDDLEWARE = [ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'social_django.middleware.SocialAuthExceptionMiddleware', ] ROOT_URLCONF = 'app.urls' @@ -75,6 +77,8 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'social_django.context_processors.backends', + 'social_django.context_processors.login_redirect', ], }, }, @@ -86,6 +90,13 @@ STATICFILES_DIRS = [ WSGI_APPLICATION = 'app.wsgi.application' +AUTHENTICATION_BACKENDS = [ + 'social_core.backends.github.GithubOAuth2', + 'django.contrib.auth.backends.ModelBackend', +] + +SOCIAL_AUTH_GITHUB_KEY = os.getenv('OAUTH_GITHUB_KEY') +SOCIAL_AUTH_GITHUB_SECRET = os.getenv('OAUTH_GITHUB_SECRET') # Database # https://docs.djangoproject.com/en/2.0/ref/settings/#databases diff --git a/app/app/urls.py b/app/app/urls.py index 84cc3ac4..376d5939 100644 --- a/app/app/urls.py +++ b/app/app/urls.py @@ -15,15 +15,16 @@ Including another URLconf """ from django.contrib import admin from django.urls import path, include -from django.contrib.auth.views import LoginView, PasswordResetView, LogoutView +from django.contrib.auth.views import PasswordResetView, LogoutView +from server.views import LoginView from server.urls import router urlpatterns = [ path('', include('server.urls')), path('admin/', admin.site.urls), - path('login/', LoginView.as_view(template_name='login.html', - redirect_authenticated_user=True), name='login'), + path('social/', include('social_django.urls')), + path('login/', LoginView.as_view(), name='login'), path('logout/', LogoutView.as_view(), name='logout'), path('password_reset/', PasswordResetView.as_view(), name='password_reset'), path('api-auth/', include('rest_framework.urls')), diff --git a/app/server/templates/base.html b/app/server/templates/base.html index dbed3c99..a867ec04 100644 --- a/app/server/templates/base.html +++ b/app/server/templates/base.html @@ -20,6 +20,7 @@ + diff --git a/app/server/templates/login.html b/app/server/templates/login.html index bee0c3b6..cfb1c436 100644 --- a/app/server/templates/login.html +++ b/app/server/templates/login.html @@ -49,6 +49,15 @@ + {% if social_login_enabled %} +
+ {% endif %} + {% if github_login %} + + + Login with Github + + {% endif %} diff --git a/app/server/views.py b/app/server/views.py index bcb1d5ba..6e4f9377 100644 --- a/app/server/views.py +++ b/app/server/views.py @@ -4,6 +4,7 @@ from io import TextIOWrapper import itertools as it import logging +from django.contrib.auth.views import LoginView as BaseLoginView from django.urls import reverse from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404 @@ -179,6 +180,20 @@ class DataDownloadFile(SuperUserMixin, LoginRequiredMixin, View): return response +class LoginView(BaseLoginView): + template_name = 'login.html' + redirect_authenticated_user = True + extra_context = { + 'github_login': bool(settings.SOCIAL_AUTH_GITHUB_KEY), + } + + def get_context_data(self, **kwargs): + context = super(LoginView, self).get_context_data(**kwargs) + context['social_login_enabled'] = any(value for key, value in context.items() + if key.endswith('_login')) + return context + + class DemoTextClassification(TemplateView): template_name = 'demo/demo_text_classification.html' diff --git a/requirements.txt b/requirements.txt index 61264e6e..44b8b53e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,8 @@ psycopg2==2.7.5 python-dateutil==2.7.3 pytz==2018.4 six==1.11.0 +social-auth-app-django==3.1.0 +social-auth-core==3.0.0 text-unidecode==1.2 tornado==5.0.2 whitenoise==3.3.1