Skip to content

Commit

Permalink
add in social auth, with yahoo, google and openid
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeman committed Jun 28, 2012
1 parent cbf6404 commit 4d45344
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 1 deletion.
1 change: 1 addition & 0 deletions requirements/project.txt
Expand Up @@ -9,6 +9,7 @@
PIL==1.1.7 PIL==1.1.7
easy-thumbnails==1.0-alpha-21 easy-thumbnails==1.0-alpha-21
html5lib==0.95 html5lib==0.95
django-social-auth==0.7.0


gondor==1.1 gondor==1.1
psycopg2==2.4.5 psycopg2==2.4.5
Expand Down
27 changes: 26 additions & 1 deletion symposion/settings.py
Expand Up @@ -4,6 +4,9 @@
import os.path import os.path
import posixpath import posixpath


from django.core.urlresolvers import reverse_lazy


PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__)) PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__))


Expand Down Expand Up @@ -125,6 +128,7 @@
"django.core.context_processors.tz", "django.core.context_processors.tz",
"django.core.context_processors.request", "django.core.context_processors.request",
"django.contrib.messages.context_processors.messages", "django.contrib.messages.context_processors.messages",
"social_auth.context_processors.social_auth_backends",
"pinax_utils.context_processors.settings", "pinax_utils.context_processors.settings",
"account.context_processors.account", "account.context_processors.account",
] ]
Expand Down Expand Up @@ -159,6 +163,7 @@
"taggit", "taggit",
"reversion", "reversion",
"biblion", "biblion",
"social_auth",


# symposion # symposion
"symposion.about", "symposion.about",
Expand Down Expand Up @@ -189,16 +194,36 @@
ACCOUNT_CREATE_ON_SAVE = True ACCOUNT_CREATE_ON_SAVE = True


AUTHENTICATION_BACKENDS = [ AUTHENTICATION_BACKENDS = [

# Social Auth Backends
"social_auth.backends.google.GoogleBackend",
"social_auth.backends.yahoo.YahooBackend",
"social_auth.backends.OpenIDBackend",

# Django User Accounts
"account.auth_backends.EmailAuthenticationBackend", "account.auth_backends.EmailAuthenticationBackend",
] ]


LOGIN_URL = "/account/login/" # @@@ any way this can be a url name? SOCIAL_AUTH_PIPELINE = [
"social_auth.backends.pipeline.social.social_auth_user",
"social_auth.backends.pipeline.user.get_username",
"symposion.social_auth.pipeline.user.create_user",
"social_auth.backends.pipeline.social.associate_user",
"social_auth.backends.pipeline.social.load_extra_data",
"social_auth.backends.pipeline.user.update_user_details",
]

LOGIN_URL = reverse_lazy("account_login")


ACCOUNT_SIGNUP_REDIRECT_URL = "dashboard" ACCOUNT_SIGNUP_REDIRECT_URL = "dashboard"
ACCOUNT_LOGIN_REDIRECT_URL = "dashboard" ACCOUNT_LOGIN_REDIRECT_URL = "dashboard"
ACCOUNT_LOGOUT_REDIRECT_URL = "home" ACCOUNT_LOGOUT_REDIRECT_URL = "home"
ACCOUNT_USER_DISPLAY = lambda user: user.email ACCOUNT_USER_DISPLAY = lambda user: user.email


SOCIAL_AUTH_LOGIN_REDIRECT_URL = "/dashboard/"
SOCIAL_AUTH_NEW_USER_REDIRECT_URL = "/dashboard/"
SOCIAL_AUTH_ASSOCIATE_BY_MAIL = False

EMAIL_CONFIRMATION_DAYS = 2 EMAIL_CONFIRMATION_DAYS = 2
EMAIL_DEBUG = DEBUG EMAIL_DEBUG = DEBUG


Expand Down
Empty file.
Empty file.
41 changes: 41 additions & 0 deletions symposion/social_auth/pipeline/user.py
@@ -0,0 +1,41 @@
from account.models import Account, EmailAddress

from social_auth.models import User
from social_auth.backends.exceptions import AuthException
from social_auth.backends.pipeline import warn_setting
from social_auth.utils import setting
from social_auth.signals import socialauth_not_registered


def create_user(backend, details, response, uid, username, user=None, *args, **kwargs):
"""Create user. Depends on get_username pipeline."""
if user:
return {"user": user}
if not username:
return None

warn_setting("SOCIAL_AUTH_CREATE_USERS", "create_user")

if not setting("SOCIAL_AUTH_CREATE_USERS", True):
# Send signal for cases where tracking failed registering is useful.
socialauth_not_registered.send(sender=backend.__class__, uid=uid, response=response, details=details)
return None

email = details.get("email")

if EmailAddress.objects.filter(email=email):
# TODO - Make this fail gracefully back to sign up
message = (
"The email address provided by the external "
"service is already associated with another account. Please "
"log in to that account first and associate your account."
)
raise AuthException(backend, message)
else:
user = User.objects.create_user(username=username, email=email)
EmailAddress.objects.add_email(user, user.email, primary=True)

return {
"user": user,
"is_new": True
}
7 changes: 7 additions & 0 deletions symposion/social_auth/urls.py
@@ -0,0 +1,7 @@
from django.conf.urls import patterns, url

from symposion.social_auth.views import SocialAuths

urlpatterns = patterns("",
url(r"^associations/$", SocialAuths.as_view(), name="social_auth_associations"),
)
44 changes: 44 additions & 0 deletions symposion/social_auth/views.py
@@ -0,0 +1,44 @@
from django.http import HttpResponseRedirect
from django.views.generic.list import ListView
from django.utils.translation import ugettext as _

from django.contrib import messages
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import login_required

from account.mixins import LoginRequiredMixin
from social_auth.decorators import dsa_view
from social_auth.models import UserSocialAuth
from social_auth.utils import backend_setting
from social_auth.views import DEFAULT_REDIRECT


class SocialAuths(LoginRequiredMixin, ListView):

model = UserSocialAuth

def get_queryset(self):
qs = super(SocialAuths, self).get_queryset()
qs = qs.filter(user=self.request.user)
return qs


@login_required
@dsa_view()
def disconnect(request, backend, association_id=None):
associated = request.user.social_auth.count()
url = request.REQUEST.get(REDIRECT_FIELD_NAME, '') or backend_setting(backend, 'SOCIAL_AUTH_DISCONNECT_REDIRECT_URL') or DEFAULT_REDIRECT

if not request.user.has_usable_password() and associated <= 1:
messages.error(request, _("Cannot remove the only Social Account without first setting a Password or adding another Social Account."))
return HttpResponseRedirect(url)

usa = request.user.social_auth.get(pk=association_id)

backend.disconnect(request.user, association_id)
messages.success(request, _("Removed the %(provider)s account '%(uid)s'.") % {
"provider": usa.provider,
"uid": usa.extra_data.get("display", usa.uid) if usa.extra_data is not None else usa.uid,
})

return HttpResponseRedirect(url)
19 changes: 19 additions & 0 deletions symposion/templates/account/_openid_sidebar.html
@@ -0,0 +1,19 @@
{% load i18n %}
{% load url from future %}

<h4>{% trans "Sign in with another account:" %}</h4>
<ul>
<li>
<a href="{% url "socialauth_begin" "google" %}">Sign-in using Google</a>
</li>
<li>
<a href="{% url "socialauth_begin" "yahoo" %}">Sign-in using Yahoo!</a>
</li>
</ul>

<h4>{% trans "Sign in with OpenID" %}</h4>
<form action="{% url "socialauth_begin" "openid" %}" method="POST" autocapitalize="off" class"form-inline">
{% csrf_token %}
<input class="openid" type="text" name="openid_identifier" placeholder="Your OpenID Identifier" />
<input type="submit" value="{% trans "Sign in" %}" class="btn btn-primary" />
</form>
27 changes: 27 additions & 0 deletions symposion/templates/account/login.html
@@ -0,0 +1,27 @@
{% extends "site_base.html" %}

{% load url from future %}
{% load i18n %}
{% load bootstrap_tags %}

{% block head_title %}{% trans "Log in" %}{% endblock %}

{% block body %}

<form method="POST" action="{% url "account_login" %}" autocapitalize="off" class="form-horizontal"{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>
<legend>{% trans "Log in to an existing account" %}</legend>
{% csrf_token %}
{{ form|as_bootstrap }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<div class="form-actions">
<a href="{% url "account_password_reset" %}" class="btn">{% trans "Forgot your password?" %}</a>
<button type="submit" class="btn btn-primary">{% trans "Log in" %}</button>
</div>
</form>
{% endblock %}

{% block sidebar %}
{% include "account/_openid_sidebar.html" %}
{% endblock %}
27 changes: 27 additions & 0 deletions symposion/templates/account/signup.html
@@ -0,0 +1,27 @@
{% extends "site_base.html" %}

{% load url from future %}
{% load i18n %}
{% load bootstrap_tags %}

{% block head_title %}{% trans "Create an account" %}{% endblock %}

{% block body %}
<form id="signup_form" method="post" action="{% url "account_signup" %}" autocapitalize="off" class="form-horizontal"{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>
<legend>{% trans "Create a new account" %}</legend>
<fieldset>
{% csrf_token %}
{{ form|as_bootstrap }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<div class="form-actions">
<button type="submit" class="btn btn-primary">{% trans "Create Account" %}</button>
</div>
</fieldset>
</form>
{% endblock %}

{% block sidebar %}
{% include "account/_openid_sidebar.html" %}
{% endblock %}
2 changes: 2 additions & 0 deletions symposion/urls.py
Expand Up @@ -20,6 +20,8 @@
url(r"^about/", include("symposion.about.urls")), url(r"^about/", include("symposion.about.urls")),
url(r"^account/signup/$", symposion.views.SignupView.as_view(), name="account_signup"), url(r"^account/signup/$", symposion.views.SignupView.as_view(), name="account_signup"),
url(r"^account/login/$", symposion.views.LoginView.as_view(), name="account_login"), url(r"^account/login/$", symposion.views.LoginView.as_view(), name="account_login"),
url(r"^account/social/", include("social_auth.urls")),
url(r"^account/associations/", include("symposion.social_auth.urls")),
url(r"^account/", include("account.urls")), url(r"^account/", include("account.urls")),
url(r"^dashboard/", symposion.views.dashboard, name="dashboard"), url(r"^dashboard/", symposion.views.dashboard, name="dashboard"),
url(r"^markitup/", include("markitup.urls")), url(r"^markitup/", include("markitup.urls")),
Expand Down

0 comments on commit 4d45344

Please sign in to comment.