From 4ce9ac6a88d634b0dab920c1b55508d1e80e8602 Mon Sep 17 00:00:00 2001
From: Martin Peveri
Date: Sat, 16 May 2015 11:18:24 -0300
Subject: [PATCH 1/2] Migration to python social auth #141
---
nodeshot/community/profiles/models/profile.py | 7 +-
.../profiles/social_auth_extra/openwisp.py | 71 -----------
.../profiles/social_auth_extra/pipeline.py | 6 +-
nodeshot/conf/settings.py | 65 ++++++----
nodeshot/conf/urls.py | 6 +-
nodeshot/ui/default/settings.py | 8 +-
nodeshot/ui/default/templates/base.html | 112 +++++++++---------
.../ui/open311_demo/templates/open311.html | 12 +-
requirements.txt | 2 +-
tests/ci/settings.py | 12 +-
10 files changed, 123 insertions(+), 178 deletions(-)
delete mode 100755 nodeshot/community/profiles/social_auth_extra/openwisp.py
diff --git a/nodeshot/community/profiles/models/profile.py b/nodeshot/community/profiles/models/profile.py
index f26b1287..712db218 100755
--- a/nodeshot/community/profiles/models/profile.py
+++ b/nodeshot/community/profiles/models/profile.py
@@ -102,8 +102,11 @@ def save(self, *args, **kwargs):
and self.email_set.filter(email=self.email).count() < 1
):
self.email_set.add_email(self, email=self.email)
- self.email_set.last().set_as_primary()
-
+ try:
+ self.email_set.last().set_as_primary()
+ except Exception:
+ pass
+
def get_full_name(self):
"""
Returns the first_name plus the last_name, with a space in between.
diff --git a/nodeshot/community/profiles/social_auth_extra/openwisp.py b/nodeshot/community/profiles/social_auth_extra/openwisp.py
deleted file mode 100755
index 6e7af30b..00000000
--- a/nodeshot/community/profiles/social_auth_extra/openwisp.py
+++ /dev/null
@@ -1,71 +0,0 @@
-"""
-OpenWISP OAuth support.
-"""
-from urllib import urlencode
-import simplejson as json
-
-from django.core.exceptions import ImproperlyConfigured
-
-from social_auth.utils import dsa_urlopen
-from social_auth.backends import BaseOAuth2, OAuthBackend
-
-from ..settings import settings
-
-# OpenWISP configuration
-try:
- OPENWISP_BASE_URL = settings.OPENWISP_BASE_URL
-except AttributeError:
- raise ImproperlyConfigured('missing settings.OPENWISP_BASE_URL')
-
-OPENWISP_AUTHORIZATION_URL = '%s/oauth/authorize' % OPENWISP_BASE_URL
-OPENWISP_ACCESS_TOKEN_URL = '%s/oauth/access_token' % OPENWISP_BASE_URL
-OPENWISP_USER_DATA_URL = '%s/oauth/account_details.json' % OPENWISP_BASE_URL
-
-
-class OpenWISPBackend(OAuthBackend):
- """OpenWISP OAuth authentication backend"""
- name = 'openwisp'
- # Default extra data to store
- EXTRA_DATA = [
- ('id', 'id'),
- ('expires', 'expires')
- ]
-
- def get_user_details(self, response):
- """Return user details from OpenWISP account"""
- return {
- 'username': response.get('username'),
- 'email': response.get('email'),
- 'first_name': response.get('first_name'),
- 'last_name': response.get('last_name'),
- 'birth_date': response.get('birth_date'),
- }
-
-
-class OpenWISPAuth(BaseOAuth2):
- """OpenWISP OAuth2 mechanism"""
- AUTHORIZATION_URL = OPENWISP_AUTHORIZATION_URL
- ACCESS_TOKEN_URL = OPENWISP_ACCESS_TOKEN_URL
- AUTH_BACKEND = OpenWISPBackend
- SETTINGS_KEY_NAME = 'OPENWISP_APP_ID'
- SETTINGS_SECRET_NAME = 'OPENWISP_API_SECRET'
- SCOPE_SEPARATOR = ','
- SCOPE_VAR_NAME = 'OPENWISP_EXTENDED_PERMISSIONS'
-
- def user_data(self, access_token, *args, **kwargs):
- """Loads user data from service"""
- url = OPENWISP_USER_DATA_URL + '?' + urlencode({
- 'access_token': access_token
- })
-
- try:
- data = json.load(dsa_urlopen(url))
- except ValueError:
- data = None
-
- return data
-
-# Backend definition
-BACKENDS = {
- 'openwisp': OpenWISPAuth,
-}
diff --git a/nodeshot/community/profiles/social_auth_extra/pipeline.py b/nodeshot/community/profiles/social_auth_extra/pipeline.py
index 44601841..8a470fa2 100755
--- a/nodeshot/community/profiles/social_auth_extra/pipeline.py
+++ b/nodeshot/community/profiles/social_auth_extra/pipeline.py
@@ -4,8 +4,8 @@
from django.utils.translation import ugettext_lazy as _
-from social_auth.models import UserSocialAuth
-from social_auth.exceptions import AuthFailed
+from social.apps.django_app.default.models import UserSocialAuth
+from social.exceptions import AuthFailed
from ..settings import EMAIL_CONFIRMATION
@@ -26,7 +26,7 @@ def create_user(backend, details, response, uid, username, user=None,
message = _("""your social account needs to have a verified email address in order to proceed.""")
raise AuthFailed(backend, message)
- if email and UserSocialAuth.email_max_length() < len(email):
+ if email and len(email) <= 54:
original_email = email
email = ''
diff --git a/nodeshot/conf/settings.py b/nodeshot/conf/settings.py
index 815cf923..6f1e545e 100755
--- a/nodeshot/conf/settings.py
+++ b/nodeshot/conf/settings.py
@@ -77,7 +77,9 @@
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
- "django.contrib.messages.context_processors.messages"
+ "django.contrib.messages.context_processors.messages",
+ "social.apps.django_app.context_processors.backends",
+ "social.apps.django_app.context_processors.login_redirect",
)
INSTALLED_APPS = [
@@ -123,7 +125,7 @@
'smuggler',
'reversion',
'corsheaders',
- 'social_auth',
+ 'social.apps.django_app.default',
'rosetta',
# other utilities
'django_extensions',
@@ -320,8 +322,12 @@
# ------ SOCIAL AUTH SETTINGS ------ #
-if 'social_auth' in INSTALLED_APPS:
- MIDDLEWARE_CLASSES += ('social_auth.middleware.SocialAuthExceptionMiddleware',)
+if 'social.apps.django_app.default' in INSTALLED_APPS:
+ MIDDLEWARE_CLASSES += ('social.apps.django_app.middleware.SocialAuthExceptionMiddleware',)
+
+ SOUTH_MIGRATION_MODULES = {
+ 'default': 'social.apps.django_app.default.south_migrations'
+ }
# In Django 1.6, the default session serliazer has been switched to one based on JSON,
# rather than pickles, to improve security. Django-openid-auth does not support this
@@ -333,44 +339,51 @@
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'nodeshot.community.profiles.backends.EmailBackend',
- 'social_auth.backends.facebook.FacebookBackend',
- 'social_auth.backends.google.GoogleOAuth2Backend',
- 'nodeshot.community.profiles.social_auth_extra.github.GithubBackend',
+ 'social.backends.facebook.FacebookAppOAuth2',
+ 'social.backends.facebook.FacebookOAuth2',
+ 'social.backends.github.GithubOAuth2',
+ 'social.backends.google.GoogleOAuth',
+ 'social.backends.google.GoogleOAuth2',
)
SOCIAL_AUTH_PIPELINE = (
- 'social_auth.backends.pipeline.social.social_auth_user',
- 'social_auth.backends.pipeline.associate.associate_by_email',
- 'social_auth.backends.pipeline.user.get_username',
- 'nodeshot.community.profiles.social_auth_extra.pipeline.create_user',
- 'social_auth.backends.pipeline.social.associate_user',
+ 'social.pipeline.social_auth.social_details',
+ 'social.pipeline.social_auth.social_uid',
+ 'social.pipeline.social_auth.auth_allowed',
+ 'social.pipeline.social_auth.social_user',
+ 'social.pipeline.user.get_username',
+ 'social.pipeline.social_auth.associate_by_email',
+ 'nodeshot.community.profiles.social_auth_extra.pipeline.create_user',
+ 'social.pipeline.social_auth.associate_user',
'nodeshot.community.profiles.social_auth_extra.pipeline.load_extra_data',
- 'social_auth.backends.pipeline.user.update_user_details'
+ 'social.pipeline.user.user_details',
)
- SOCIAL_AUTH_ENABLED_BACKENDS = ('facebook', 'google', 'github')
-
# register a new app:
- FACEBOOK_APP_ID = '' # put your app id
- FACEBOOK_API_SECRET = ''
- FACEBOOK_EXTENDED_PERMISSIONS = ['email', 'user_about_me', 'user_birthday', 'user_hometown']
+ SOCIAL_AUTH_FACEBOOK_KEY = '' # put your app id
+ SOCIAL_AUTH_FACEBOOK_SECRET = ''
+ SOCIAL_AUTH_FACEBOOK_SCOPE = ['email', 'user_about_me', 'user_birthday', 'user_hometown']
- GOOGLE_OAUTH2_CLIENT_ID = ''
- GOOGLE_OAUTH2_CLIENT_SECRET = ''
+ SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
+ SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
# register a new app:
- GITHUB_APP_ID = ''
- GITHUB_API_SECRET = ''
- GITHUB_EXTENDED_PERMISSIONS = ['user:email']
+ SOCIAL_AUTH_GITHUB_KEY = ''
+ SOCIAL_AUTH_GITHUB_SECRET = ''
+ SOCIAL_AUTH_GITHUB_SCOPE = ['user:email']
+ SOCIAL_AUTH_URL_NAMESPACE = 'social'
+ SOCIAL_AUTH_USER_MODEL = AUTH_USER_MODEL
SOCIAL_AUTH_DEFAULT_USERNAME = 'new_social_auth_user'
SOCIAL_AUTH_UUID_LENGTH = 3
SOCIAL_AUTH_SESSION_EXPIRATION = False
SOCIAL_AUTH_ASSOCIATE_BY_MAIL = True
+ SOCIAL_AUTH_PROTECTED_USER_FIELDS = ['email']
+ SOCIAL_AUTH_FORCE_EMAIL_VALIDATION = True
- LOGIN_URL = '/'
- LOGIN_REDIRECT_URL = '/'
- LOGIN_ERROR_URL = '/'
+ SOCIAL_AUTH_LOGIN_URL = '/'
+ SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
+ SOCIAL_AUTH_LOGIN_ERROR_URL = '/'
# ------ DJANGO-LEAFLET SETTINGS ------ #
diff --git a/nodeshot/conf/urls.py b/nodeshot/conf/urls.py
index d4934371..b27a3959 100755
--- a/nodeshot/conf/urls.py
+++ b/nodeshot/conf/urls.py
@@ -40,11 +40,11 @@
url(r'^media/(?P.*)$', 'serve'),
)
-if 'social_auth' in settings.INSTALLED_APPS:
+if 'social.apps.django_app.default' in settings.INSTALLED_APPS:
urlpatterns += patterns('',
- url(r'', include('social_auth.urls')),
+ url(r'', include('social.apps.django_app.urls', namespace='social')),
)
-
+
if 'grappelli' in settings.INSTALLED_APPS:
urlpatterns += patterns('',
url(r'^grappelli/', include('grappelli.urls')),
diff --git a/nodeshot/ui/default/settings.py b/nodeshot/ui/default/settings.py
index d50b63ef..162ddc1a 100755
--- a/nodeshot/ui/default/settings.py
+++ b/nodeshot/ui/default/settings.py
@@ -61,10 +61,10 @@
else:
CONTACTING_ENABLED = False
-if 'social_auth' in settings.INSTALLED_APPS:
- FACEBOOK_ENABLED = bool(settings.FACEBOOK_APP_ID) and bool(settings.FACEBOOK_API_SECRET) # noqa
- GOOGLE_ENABLED = bool(settings.GOOGLE_OAUTH2_CLIENT_ID) and bool(settings.GOOGLE_OAUTH2_CLIENT_SECRET) # noqa
- GITHUB_ENABLED = bool(settings.GITHUB_APP_ID) and bool(settings.GITHUB_API_SECRET) # noqa
+if 'social.apps.django_app.default' in settings.INSTALLED_APPS:
+ FACEBOOK_ENABLED = bool(settings.SOCIAL_AUTH_FACEBOOK_KEY) and bool(settings.SOCIAL_AUTH_FACEBOOK_SECRET) # noqa
+ GOOGLE_ENABLED = bool(settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY) and bool(settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET) # noqa
+ GITHUB_ENABLED = bool(settings.SOCIAL_AUTH_GITHUB_KEY) and bool(settings.SOCIAL_AUTH_GITHUB_SECRET) # noqa
SOCIAL_AUTH_ENABLED = FACEBOOK_ENABLED or GOOGLE_ENABLED or GITHUB_ENABLED # noqa
else:
FACEBOOK_ENABLED = False
diff --git a/nodeshot/ui/default/templates/base.html b/nodeshot/ui/default/templates/base.html
index 15823e7c..e77fea0b 100755
--- a/nodeshot/ui/default/templates/base.html
+++ b/nodeshot/ui/default/templates/base.html
@@ -128,36 +128,36 @@
{% if SOCIAL_AUTH_ENABLED %}
-
- {% if FACEBOOK_ENABLED %}
-
-
- {% trans 'Sign up with Facebook' %}
-
-
- {% endif %}
+
+ {% if FACEBOOK_ENABLED %}
+
+
+ {% trans 'Sign up with Facebook' %}
+
+
+ {% endif %}
- {% if GOOGLE_ENABLED %}
-
-
- {% trans 'Sign up with Google' %}
-
-
- {% endif %}
+ {% if GOOGLE_ENABLED %}
+
+
+ {% trans 'Sign up with Google' %}
+
+
+ {% endif %}
- {% if GITHUB_ENABLED %}
-
-
- {% trans 'Sign up with Github' %}
-
-
- {% endif %}
-
-
-
-
- {% trans 'or' %}
-
+ {% if GITHUB_ENABLED %}
+
+
+ {% trans 'Sign up with Github' %}
+
+
+ {% endif %}
+
+
+
+
+ {% trans 'or' %}
+
{% endif %}