From a17a5b78940358e4e67f8f0de80d62c822bd248e Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Wed, 27 Dec 2017 15:00:49 -0500 Subject: [PATCH] Add social authentication with python-social-auth and social-auth-app-django. You will need to setup app id & secret in a ./local_settings.py file to use them --- .gitignore | 1 + events/templates/events/event_list.html | 10 ++++++++++ get_together/settings.py | 26 ++++++++++++++++++++++++- get_together/urls.py | 6 ++++-- requirements.txt | 13 +++++++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 3a4bf7e..ee9ae52 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.sqlite* *.pyc +*local_settings.* diff --git a/events/templates/events/event_list.html b/events/templates/events/event_list.html index 431a0f2..31e05d9 100644 --- a/events/templates/events/event_list.html +++ b/events/templates/events/event_list.html @@ -1,3 +1,13 @@ +{% if request.user.is_authenticated %} +Welcome {{ request.user.username }} +{% else %} +Login: +Google +Facebook +Twitter +GitHub +
+{% endif %}
{% if events_list %} {% for event in events_list %} diff --git a/get_together/settings.py b/get_together/settings.py index abcfa63..5b87ed4 100644 --- a/get_together/settings.py +++ b/get_together/settings.py @@ -11,6 +11,11 @@ https://docs.djangoproject.com/en/2.0/ref/settings/ """ import os +try: + from local_settings import * + +except: + print("WARNING: You should create a local_settings.py to store local and secret data.") # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -42,9 +47,24 @@ INSTALLED_APPS = [ 'django.contrib.sites', 'rest_framework', + 'social_django', + 'events', ] +LOGIN_URL = 'login' +LOGOUT_URL = 'logout' +LOGIN_REDIRECT_URL = 'home' +AUTHENTICATION_BACKENDS = ( + 'django.contrib.auth.backends.ModelBackend', + 'social_core.backends.open_id.OpenIdAuth', + 'social_core.backends.google.GoogleOAuth2', + 'social_core.backends.github.GithubOAuth2', + 'social_core.backends.twitter.TwitterOAuth', + 'social_core.backends.facebook.FacebookOAuth2', + 'social_core.backends.yahoo.YahooOpenId', +) + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', @@ -53,6 +73,8 @@ MIDDLEWARE = [ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + + 'social_django.middleware.SocialAuthExceptionMiddleware', ] ROOT_URLCONF = 'get_together.urls' @@ -68,7 +90,9 @@ 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', + ], }, }, ] diff --git a/get_together/urls.py b/get_together/urls.py index cae2cec..e589d2d 100644 --- a/get_together/urls.py +++ b/get_together/urls.py @@ -14,11 +14,13 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include from events import views urlpatterns = [ path('admin/', admin.site.urls), path('searchables/', views.searchable_list), - path('events/', views.events_list), + path('events/', views.events_list, name='home'), + + path('oauth/', include('social_django.urls', namespace='social')), ] diff --git a/requirements.txt b/requirements.txt index 813ccff..fb3282e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,17 @@ +certifi==2017.11.5 +chardet==3.0.4 +defusedxml==0.5.0 Django==2.0 djangorestframework==3.7.7 +idna==2.6 +oauthlib==2.0.6 +PyJWT==1.5.3 +python3-openid==3.1.0 pytz==2017.3 +requests==2.18.4 +requests-oauthlib==0.8.0 simplejson==3.13.2 +six==1.11.0 +social-auth-app-django==2.1.0 +social-auth-core==1.6.0 +urllib3==1.22