├── .gitignore ├── django_social_project ├── django_social_app │ ├── __init__.py │ ├── admin.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── tests.py │ └── views.py ├── django_social_project │ ├── __init__.py │ ├── config_sample.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py └── templates │ ├── home.html │ └── login.html ├── readme.md └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | env 2 | *.pyc 3 | .DS_Store 4 | *.sqlite3 5 | config.py -------------------------------------------------------------------------------- /django_social_project/django_social_app/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/realpython/django-social-auth-example/f0b4c27a68fa25846e839318cf4f3487180eaa08/django_social_project/django_social_app/__init__.py -------------------------------------------------------------------------------- /django_social_project/django_social_app/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /django_social_project/django_social_app/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/realpython/django-social-auth-example/f0b4c27a68fa25846e839318cf4f3487180eaa08/django_social_project/django_social_app/migrations/__init__.py -------------------------------------------------------------------------------- /django_social_project/django_social_app/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /django_social_project/django_social_app/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /django_social_project/django_social_app/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render_to_response, redirect, render 2 | from django.contrib.auth import logout as auth_logout 3 | from django.contrib.auth.decorators import login_required 4 | # from django.template.context import RequestContext 5 | 6 | 7 | def login(request): 8 | # context = RequestContext(request, { 9 | # 'request': request, 'user': request.user}) 10 | # return render_to_response('login.html', context_instance=context) 11 | return render(request, 'login.html') 12 | 13 | 14 | @login_required(login_url='/') 15 | def home(request): 16 | return render_to_response('home.html') 17 | 18 | 19 | def logout(request): 20 | auth_logout(request) 21 | return redirect('/') 22 | -------------------------------------------------------------------------------- /django_social_project/django_social_project/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/realpython/django-social-auth-example/f0b4c27a68fa25846e839318cf4f3487180eaa08/django_social_project/django_social_project/__init__.py -------------------------------------------------------------------------------- /django_social_project/django_social_project/config_sample.py: -------------------------------------------------------------------------------- 1 | SOCIAL_AUTH_TWITTER_KEY = 'update me' 2 | SOCIAL_AUTH_TWITTER_SECRET = 'update me' 3 | SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/home/' 4 | SOCIAL_AUTH_LOGIN_URL = '/' 5 | -------------------------------------------------------------------------------- /django_social_project/django_social_project/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for django_social_project project. 3 | 4 | For more information on this file, see 5 | https://docs.djangoproject.com/en/1.7/topics/settings/ 6 | 7 | For the full list of settings and their values, see 8 | https://docs.djangoproject.com/en/1.7/ref/settings/ 9 | """ 10 | 11 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 12 | import os 13 | from config import * 14 | 15 | BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 16 | TEMPLATE_DIRS = ( 17 | os.path.join(BASE_DIR, 'templates'), 18 | ) 19 | 20 | 21 | # Quick-start development settings - unsuitable for production 22 | # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ 23 | 24 | # SECURITY WARNING: keep the secret key used in production secret! 25 | SECRET_KEY = 's#0_b8@gc5f8l3qn!=5k4y4e8d8f2-y^9#mg3=p))6eghor#i*' 26 | 27 | # SECURITY WARNING: don't run with debug turned on in production! 28 | DEBUG = True 29 | 30 | TEMPLATE_DEBUG = True 31 | 32 | ALLOWED_HOSTS = [] 33 | 34 | 35 | # Application definition 36 | 37 | INSTALLED_APPS = ( 38 | 'django.contrib.admin', 39 | 'django.contrib.auth', 40 | 'django.contrib.contenttypes', 41 | 'django.contrib.sessions', 42 | 'django.contrib.messages', 43 | 'django.contrib.staticfiles', 44 | 'django_social_project', 45 | 'social.apps.django_app.default', 46 | ) 47 | 48 | TEMPLATE_CONTEXT_PROCESSORS = ( 49 | 'django.contrib.auth.context_processors.auth', 50 | 'django.core.context_processors.debug', 51 | 'django.core.context_processors.i18n', 52 | 'django.core.context_processors.media', 53 | 'django.core.context_processors.static', 54 | 'django.core.context_processors.tz', 55 | 'django.contrib.messages.context_processors.messages', 56 | 'social.apps.django_app.context_processors.backends', 57 | 'social.apps.django_app.context_processors.login_redirect', 58 | ) 59 | 60 | AUTHENTICATION_BACKENDS = ( 61 | 'social.backends.facebook.FacebookOAuth2', 62 | 'social.backends.google.GoogleOAuth2', 63 | 'social.backends.twitter.TwitterOAuth', 64 | 'django.contrib.auth.backends.ModelBackend', 65 | ) 66 | 67 | MIDDLEWARE_CLASSES = ( 68 | 'django.contrib.sessions.middleware.SessionMiddleware', 69 | 'django.middleware.common.CommonMiddleware', 70 | 'django.middleware.csrf.CsrfViewMiddleware', 71 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 72 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 73 | 'django.contrib.messages.middleware.MessageMiddleware', 74 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 75 | ) 76 | 77 | ROOT_URLCONF = 'django_social_project.urls' 78 | 79 | WSGI_APPLICATION = 'django_social_project.wsgi.application' 80 | 81 | 82 | # Database 83 | # https://docs.djangoproject.com/en/1.7/ref/settings/#databases 84 | 85 | DATABASES = { 86 | 'default': { 87 | 'ENGINE': 'django.db.backends.sqlite3', 88 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 89 | } 90 | } 91 | 92 | # Internationalization 93 | # https://docs.djangoproject.com/en/1.7/topics/i18n/ 94 | 95 | LANGUAGE_CODE = 'en-us' 96 | 97 | TIME_ZONE = 'UTC' 98 | 99 | USE_I18N = True 100 | 101 | USE_L10N = True 102 | 103 | USE_TZ = True 104 | 105 | 106 | # Static files (CSS, JavaScript, Images) 107 | # https://docs.djangoproject.com/en/1.7/howto/static-files/ 108 | 109 | STATIC_URL = '/static/' 110 | -------------------------------------------------------------------------------- /django_social_project/django_social_project/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import patterns, include, url 2 | from django.contrib import admin 3 | 4 | urlpatterns = patterns( 5 | '', 6 | url(r'^admin/', include(admin.site.urls)), 7 | url('', include('social.apps.django_app.urls', namespace='social')), 8 | url(r'^$', 'django_social_app.views.login'), 9 | url(r'^home/$', 'django_social_app.views.home'), 10 | url(r'^logout/$', 'django_social_app.views.logout'), 11 | ) 12 | -------------------------------------------------------------------------------- /django_social_project/django_social_project/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for django_social_project project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_social_project.settings") 12 | 13 | from django.core.wsgi import get_wsgi_application 14 | application = get_wsgi_application() 15 | -------------------------------------------------------------------------------- /django_social_project/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_social_project.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | execute_from_command_line(sys.argv) 11 | -------------------------------------------------------------------------------- /django_social_project/templates/home.html: -------------------------------------------------------------------------------- 1 |

Welcome

2 |

Logout -------------------------------------------------------------------------------- /django_social_project/templates/login.html: -------------------------------------------------------------------------------- 1 | {% if user and not user.is_anonymous %} 2 | Hello, {{ user.get_full_name }}! 3 |
4 | Logout 5 | {% else %} 6 | Login with Twitter 7 | {% endif %} -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Adding Social Authentication to Django 2 | 3 | Check out the blog post - [https://realpython.com/blog/python/adding-social-authentication-to-django/](https://realpython.com/blog/python/adding-social-authentication-to-django/) 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==1.7.1 2 | PyJWT==0.3.0 3 | oauthlib==0.7.1 4 | python-openid==2.2.5 5 | python-social-auth==0.2.1 6 | requests==2.4.3 7 | requests-oauthlib==0.4.2 8 | six==1.8.0 9 | wsgiref==0.1.2 10 | --------------------------------------------------------------------------------