Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to python social auth #141 #227

Merged
merged 2 commits into from May 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions nodeshot/community/profiles/models/profile.py
Expand Up @@ -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.
Expand Down
71 changes: 0 additions & 71 deletions nodeshot/community/profiles/social_auth_extra/openwisp.py

This file was deleted.

23 changes: 12 additions & 11 deletions nodeshot/community/profiles/social_auth_extra/pipeline.py
Expand Up @@ -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

Expand All @@ -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 = ''

Expand All @@ -46,14 +46,15 @@ def load_extra_data(backend, details, response, uid, user, social_user=None,
social_user = social_user or UserSocialAuth.get_social_auth(backend.name, uid)

if kwargs['is_new'] and EMAIL_CONFIRMATION:
from ..models import EmailAddress
emailaddress = EmailAddress(**{
'user': user,
'email': user.email,
'verified': True,
'primary': True
})
emailaddress.save()
if user.email:
from ..models import EmailAddress
emailaddress = EmailAddress(**{
'user': user,
'email': user.email,
'verified': True,
'primary': True
})
emailaddress.save()

if social_user:
extra_data = backend.extra_data(user, uid, response, details)
Expand Down
65 changes: 39 additions & 26 deletions nodeshot/conf/settings.py
Expand Up @@ -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 = [
Expand Down Expand Up @@ -123,7 +125,7 @@
'smuggler',
'reversion',
'corsheaders',
'social_auth',
'social.apps.django_app.default',
'rosetta',
# other utilities
'django_extensions',
Expand Down Expand Up @@ -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
Expand All @@ -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 ------ #

Expand Down
6 changes: 3 additions & 3 deletions nodeshot/conf/urls.py
Expand Up @@ -40,11 +40,11 @@
url(r'^media/(?P<path>.*)$', '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')),
Expand Down
8 changes: 4 additions & 4 deletions nodeshot/ui/default/settings.py
Expand Up @@ -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
Expand Down
112 changes: 56 additions & 56 deletions nodeshot/ui/default/templates/base.html
Expand Up @@ -128,36 +128,36 @@ <h1><a href="#">{{ SITE_NAME }}</a></h1>
</p>

{% if SOCIAL_AUTH_ENABLED %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to put this back

<p class="social-buttons">
{% if FACEBOOK_ENABLED %}
<a href="{% url "socialauth_begin" "facebook" %}" class="btn btn-social btn-facebook">
<span class="icon icon-facebook"></span>
<span class="text">{% trans 'Sign up with Facebook' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}
<p class="social-buttons">
{% if FACEBOOK_ENABLED %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to put this back

<a href="{% url 'social:begin' 'facebook' %}" class="btn btn-social btn-facebook">
<span class="icon icon-facebook"></span>
<span class="text">{% trans 'Sign up with Facebook' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}

{% if GOOGLE_ENABLED %}
<a href="{% url "socialauth_begin" "google-oauth2" %}" class="btn btn-social btn-google">
<span class="icon icon-google"></span>
<span class="text">{% trans 'Sign up with Google' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}
{% if GOOGLE_ENABLED %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to put this back

<a href="{% url 'social:begin' 'google-oauth2' %}" class="btn btn-social btn-google">
<span class="icon icon-google"></span>
<span class="text">{% trans 'Sign up with Google' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}

{% if GITHUB_ENABLED %}
<a href="{% url "socialauth_begin" "github" %}" class="btn btn-social btn-github">
<span class="icon icon-github"></span>
<span class="text">{% trans 'Sign up with Github' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}
</p>

<p class="separator">
<span class="line"></span>
<span class="text">{% trans 'or' %}</span>
</p>
{% if GITHUB_ENABLED %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to put this back

<a href="{% url 'social:begin' 'github' %}" class="btn btn-social btn-github">
<span class="icon icon-github"></span>
<span class="text">{% trans 'Sign up with Github' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}
</p>
<p class="separator">
<span class="line"></span>
<span class="text">{% trans 'or' %}</span>
</p>
{% endif %}

<form action="" method="post" id="js-signup-form">
Expand Down Expand Up @@ -205,36 +205,36 @@ <h1><a href="#">{{ SITE_NAME }}</a></h1>
<div class="modal-dialog modal-content modal-body inverse user-dialog">

{% if SOCIAL_AUTH_ENABLED %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to put this back

<p class="social-buttons">
{% if FACEBOOK_ENABLED %}
<a href="{% url "socialauth_begin" "facebook" %}" class="btn btn-social btn-facebook">
<span class="icon icon-facebook"></span>
<span class="text">{% trans 'Sign in with Facebook' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}
<p class="social-buttons">
{% if FACEBOOK_ENABLED %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to put this back

<a href="{% url 'social:begin' 'facebook' %}" class="btn btn-social btn-facebook">
<span class="icon icon-facebook"></span>
<span class="text">{% trans 'Sign in with Facebook' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}

{% if GOOGLE_ENABLED %}
<a href="{% url "socialauth_begin" "google-oauth2" %}" class="btn btn-social btn-google">
<span class="icon icon-google"></span>
<span class="text">{% trans 'Sign in with Google' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}
{% if GOOGLE_ENABLED %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to put this back

<a href="{% url 'social:begin' 'google-oauth2' %}" class="btn btn-social btn-google">
<span class="icon icon-google"></span>
<span class="text">{% trans 'Sign in with Google' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}

{% if GITHUB_ENABLED %}
<a href="{% url "socialauth_begin" "github" %}" class="btn btn-social btn-github">
<span class="icon icon-github"></span>
<span class="text">{% trans 'Sign in with Github' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}
</p>

<p class="separator">
<span class="line"></span>
<span class="text">{% trans 'or' %}</span>
</p>
{% if GITHUB_ENABLED %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to put this back

<a href="{% url 'social:begin' 'github' %}" class="btn btn-social btn-github">
<span class="icon icon-github"></span>
<span class="text">{% trans 'Sign in with Github' %}</span>
<span class="hover">&nbsp;</span>
</a>
{% endif %}
</p>
<p class="separator">
<span class="line"></span>
<span class="text">{% trans 'or' %}</span>
</p>
{% endif %}

<form action="" method="post" id="js-signin-form">
Expand Down