Skip to content

Commit

Permalink
Merged krvss work. Small improvements over livejournal support
Browse files Browse the repository at this point in the history
  • Loading branch information
omab committed Jan 14, 2011
2 parents e883f07 + d4f58d3 commit 55ec3cd
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 11 deletions.
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ credentials, some features are:
* `Google OpenID`_
* `Google OAuth`_
* `Yahoo OpenID`_
* `LiveJournal OpenID`_
* OpenId_ like myOpenID_
* `Twitter OAuth`_
* `Facebook OAuth`_
Expand Down Expand Up @@ -100,6 +101,7 @@ Configuration
'social_auth.backends.GoogleOAuthBackend',
'social_auth.backends.GoogleBackend',
'social_auth.backends.YahooBackend',
'social_auth.backends.LiveJournalBackend',
'social_auth.backends.OpenIDBackend',
'django.contrib.auth.backends.ModelBackend',
)
Expand Down Expand Up @@ -202,6 +204,13 @@ for example, to store user gender, location, etc. Example::
user.gender = response.get('gender')
return True

New data updating is made automatically but could be disabled and left only to
signal handler if this setting value::

SOCIAL_AUTH_CHANGE_SIGNAL_ONLY = False

is set to True.


------
OpenId
Expand Down Expand Up @@ -389,3 +398,4 @@ Base work is copyrighted by:
.. _caioariede: https://github.com/caioariede
.. _krvss: https://github.com/krvss
.. _jezdez: https://github.com/jezdez
.. _LiveJournal OpenID: http://www.livejournal.com/support/faqbrowse.bml?faqid=283
1 change: 1 addition & 0 deletions example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'social_auth.backends.GoogleBackend',
'social_auth.backends.YahooBackend',
'social_auth.backends.OpenIDBackend',
'social_auth.backends.LiveJournalBackend',
'django.contrib.auth.backends.ModelBackend',
)

Expand Down
9 changes: 9 additions & 0 deletions example/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ <h3>Login using <a href="http://openid.net/" title="OpenId">OpenId</a> from:</h3
<ul>
<li><a rel="nofollow" href="/login/google/">Google</a></li>
<li><a rel="nofollow" href="/login/yahoo/">Yahoo</a></li>
<li>
<form action="/login/livejournal/" method="post">{% csrf_token %}
<div>
<label for="openid_lj_user">LiveJournal user:</label>
<input id="openid_lj_user" type="text" value="" name="openid_lj_user" />
<input type="submit" value="Login"/>
</div>
</form>
</li>
<li>
<form action="/login/openid/" method="post">{% csrf_token %}
<div>
Expand Down
21 changes: 20 additions & 1 deletion social_auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
from .store import DjangoOpenIDStore
from .backends import TwitterBackend, OrkutBackend, FacebookBackend, \
OpenIDBackend, GoogleBackend, YahooBackend, \
GoogleOAuthBackend
GoogleOAuthBackend, LiveJournalBackend
from .conf import AX_ATTRS, SREG_ATTR, OPENID_ID_FIELD, SESSION_NAME, \
OPENID_GOOGLE_URL, OPENID_YAHOO_URL, TWITTER_SERVER, \
OPENID_LIVEJOURNAL_URL, OPENID_LIVEJOURNAL_USER_FIELD, \
TWITTER_REQUEST_TOKEN_URL, TWITTER_ACCESS_TOKEN_URL, \
TWITTER_AUTHORIZATION_URL, TWITTER_CHECK_AUTH, \
FACEBOOK_CHECK_AUTH, FACEBOOK_AUTHORIZATION_URL, \
Expand Down Expand Up @@ -168,6 +169,23 @@ def openid_url(self):
return OPENID_YAHOO_URL


class LiveJournalAuth(OpenIdAuth):
"""LiveJournal OpenID authentication"""
AUTH_BACKEND = LiveJournalBackend

def uses_redirect(self):
"""LiveJournal uses redirect"""
return True

def openid_url(self):
"""Returns LiveJournal authentication URL"""
if self.request.method != 'POST' or \
not self.request.POST.get(OPENID_LIVEJOURNAL_USER_FIELD):
raise ValueError, 'Missing LiveJournal user identifier'
return OPENID_LIVEJOURNAL_URL % \
self.request.POST[OPENID_LIVEJOURNAL_USER_FIELD]


class BaseOAuth(BaseAuth):
"""OAuth base class"""
def __init__(self, request, redirect):
Expand Down Expand Up @@ -446,6 +464,7 @@ def user_data(self, access_token):
'google': GoogleAuth,
'google-oauth': GoogleOAuth,
'yahoo': YahooAuth,
'livejournal': LiveJournalAuth,
'orkut': OrkutAuth,
'openid': OpenIdAuth,
}
Expand Down
42 changes: 33 additions & 9 deletions social_auth/backends.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Authentication backeds for django.contrib.auth AUTHENTICATION_BACKENDS setting
"""
import urlparse
from os import urandom

from openid.extensions import ax, sreg
Expand Down Expand Up @@ -108,14 +109,17 @@ def update_user_details(self, user, response, details, new_user=False):
"""Update user details with (maybe) new data. Username is not
changed if associating a new credential."""
changed = False
for name, value in details.iteritems():
# not update username if user already exists
if not new_user and name == USERNAME:
continue
if value and value != getattr(user, name, value):
setattr(user, name, value)
changed = True


# check if values update should be left to signals handlers only
if not getattr(settings, 'SOCIAL_AUTH_CHANGE_SIGNAL_ONLY', False):
for name, value in details.iteritems():
# not update username if user already exists
if not new_user and name == USERNAME:
continue
if value and value != getattr(user, name, value):
setattr(user, name, value)
changed = True

# Fire a pre-update signal sending current backend instance,
# user instance (created or retrieved from database), service
# response and processed details, signal handlers must return
Expand All @@ -124,7 +128,14 @@ def update_user_details(self, user, response, details, new_user=False):
user=user,
response=response,
details=details))
if changed or len(updated) > 0:
# Looking for at least one update
has_update = False
for result in updated:
if result[1]:
has_update = True
break

if changed or has_update:
user.save()

def get_user_id(self, details, response):
Expand Down Expand Up @@ -268,3 +279,16 @@ class GoogleBackend(OpenIDBackend):
class YahooBackend(OpenIDBackend):
"""Yahoo OpenID authentication backend"""
name = 'yahoo'


class LiveJournalBackend(OpenIDBackend):
"""LiveJournal OpenID authentication backend"""
name = 'livejournal'

def get_user_details(self, response):
"""Generate username from identity url"""
values = super(LiveJournalBackend, self).get_user_details(response)
if not values.get(USERNAME):
values[USERNAME] = urlparse.urlsplit(response.identity_url)\
.netloc.split('.', 1)[0]
return values
4 changes: 3 additions & 1 deletion social_auth/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@
SREG_ATTR = ['email', 'fullname', 'nickname']
OPENID_ID_FIELD = 'openid_identifier'
SESSION_NAME = 'openid'
OPENID_GOOGLE_URL = 'https://www.google.com/accounts/o8/id'
OPENID_GOOGLE_URL = 'https://www.google.com/accounts/o8/id'
OPENID_YAHOO_URL = 'http://yahoo.com'
OPENID_LIVEJOURNAL_URL = 'http://%s.livejournal.com'
OPENID_LIVEJOURNAL_USER_FIELD = 'openid_lj_user'

0 comments on commit 55ec3cd

Please sign in to comment.