Commit 017e8975 authored by root's avatar root

use pip

parent 086410f9
......@@ -116,7 +116,6 @@ MIDDLEWARE_CLASSES = (
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
from django.core.urlresolvers import reverse
from django.conf import settings
from social_auth.exceptions import WrongBackend
class WrongBackendExceptionMiddleware(object):
def process_exception(self, request, exception):
# Get the exception info now, in case another exception is thrown later.
if isinstance(exception, WrongBackend):
return self.handle_404(request, exception)
from django.core.urlresolvers import RegexURLResolver
def handle_404(self, request, exception):
if settings.DEBUG:
from django.views import debug
return debug.technical_404_response(request, exception)
callback, param_dict = resolver(request).resolve404()
return callback(request, **param_dict)
from django.core.urlresolvers import RegexURLResolver
def resolver(request):
Returns a RegexURLResolver for the request's urlconf.
Django-social-auth application, allows OpenId or OAuth user
registration/authentication just adding a few configurations.
version = (0, 7, 18)
__version__ = '.'.join(map(str, version))
"""Admin settings"""
from social_auth.utils import setting
if setting('SOCIAL_AUTH_MODELS') in (None, 'social_auth.db.django_models'):
from django.contrib import admin
from social_auth.models import UserSocialAuth, Nonce, Association
class UserSocialAuthOption(admin.ModelAdmin):
"""Social Auth user options"""
list_display = ('id', 'user', 'provider', 'uid')
search_fields = ('user__first_name', 'user__last_name', 'user__email',
list_filter = ('provider',)
raw_id_fields = ('user',)
list_select_related = True
class NonceOption(admin.ModelAdmin):
"""Nonce options"""
list_display = ('id', 'server_url', 'timestamp', 'salt')
search_fields = ('server_url',)
class AssociationOption(admin.ModelAdmin):
"""Association options"""
list_display = ('id', 'server_url', 'assoc_type')
list_filter = ('assoc_type',)
search_fields = ('server_url',), UserSocialAuthOption), NonceOption), AssociationOption)
Base backends structures.
This module defines base classes needed to define custom OpenID or OAuth
auth services from third parties. This customs must subclass an Auth and
and Backend class, check current implementation for examples.
Also the modules *must* define a BACKENDS dictionary with the backend name
(which is used for URLs matching) and Auth class, otherwise it won't be
from urllib2 import Request, HTTPError
from urllib import urlencode
from openid.consumer.consumer import Consumer, SUCCESS, CANCEL, FAILURE
from import DiscoveryFailure
from openid.extensions import sreg, ax, pape
from oauth2 import Consumer as OAuthConsumer, Token, Request as OAuthRequest
from django.contrib.auth import authenticate
from django.utils import simplejson
from django.utils.importlib import import_module
from social_auth.models import UserSocialAuth
from social_auth.utils import setting, model_to_ctype, ctype_to_model, \
clean_partial_pipeline, url_add_parameters, \
get_random_string, constant_time_compare, \
from import DjangoOpenIDStore
from social_auth.exceptions import StopPipeline, AuthException, AuthFailed, \
AuthCanceled, AuthUnknownError, \
AuthTokenError, AuthMissingParameter, \
AuthStateMissing, AuthStateForbidden, \
from social_auth.backends.utils import build_consumer_oauth_request
# OpenID configuration
('', 'old_email'),
('', 'old_fullname'),
('', 'old_nickname')
# Request both the full name and first/last components since some
# providers offer one but not the other.
('', 'email'),
('', 'fullname'),
('', 'first_name'),
('', 'last_name'),
('', 'nickname'),
('email', 'email'),
('fullname', 'fullname'),
('nickname', 'nickname')
OPENID_ID_FIELD = 'openid_identifier'
SESSION_NAME = 'openid'
# key for username in user details dict used around, see get_user_details
# method
USERNAME = 'username'
# Removed by default since it can be a dangerouse behavior that
# could lead to accounts take over.
class SocialAuthBackend(object):
"""A django.contrib.auth backend that authenticates the user based on
a authentication provider response"""
name = '' # provider name, it's stored in database
supports_inactive_user = False
def authenticate(self, *args, **kwargs):
"""Authenticate user using social credentials
Authentication is made if this is the correct backend, backend
verification is made by kwargs inspection for current backend
name presence.
# Validate backend and arguments. Require that the Social Auth
# response be passed in as a keyword argument, to make sure we
# don't match the username/password calling conventions of
# authenticate.
if not ( and kwargs.get( and 'response' in kwargs):
return None
response = kwargs.get('response')
pipeline = PIPELINE
kwargs = kwargs.copy()
kwargs['backend'] = self
if 'pipeline_index' in kwargs:
pipeline = pipeline[kwargs['pipeline_index']:]
kwargs['details'] = self.get_user_details(response)
kwargs['uid'] = self.get_user_id(kwargs['details'], response)
kwargs['is_new'] = False
out = self.pipeline(pipeline, *args, **kwargs)
if not isinstance(out, dict):
return out
social_user = out.get('social_user')
if social_user:
# define user.social_user attribute to track current social
# account
user = social_user.user
user.social_user = social_user
user.is_new = out.get('is_new')
return user
def pipeline(self, pipeline, *args, **kwargs):
out = kwargs.copy()
if 'pipeline_index' in kwargs:
base_index = int(kwargs['pipeline_index'])
base_index = 0
for idx, name in enumerate(pipeline):
out['pipeline_index'] = base_index + idx
mod_name, func_name = name.rsplit('.', 1)
mod = import_module(mod_name)
func = getattr(mod, func_name, None)
result = func(*args, **out) or {}
except StopPipeline:
# Clean partial pipeline on stop
if 'request' in kwargs:
if isinstance(result, dict):
return result
return out
def extra_data(self, user, uid, response, details):
"""Return default blank user extra data"""
return {}
def get_user_id(self, details, response):
"""Must return a unique ID from values returned on details"""
raise NotImplementedError('Implement in subclass')
def get_user_details(self, response):
"""Must return user details in a know internal struct:
{USERNAME: <username if any>,
'email': <user email if any>,
'fullname': <user full name if any>,
'first_name': <user first name if any>,
'last_name': <user last name if any>}
raise NotImplementedError('Implement in subclass')
def tokens(cls, instance):
"""Return the tokens needed to authenticate the access to any API the
service might provide. The return value will be a dictionary with the
token type name as key and the token value.
instance must be a UserSocialAuth instance.
if instance.extra_data and 'access_token' in instance.extra_data:
return {
'access_token': instance.extra_data['access_token']
return {}
def get_user(self, user_id):
Return user with given ID from the User model used by this backend.
This is called by django.contrib.auth.middleware.
return UserSocialAuth.get_user(user_id)
class OAuthBackend(SocialAuthBackend):
"""OAuth authentication backend base class.
EXTRA_DATA defines a set of name that will be stored in
extra_data field. It must be a list of tuples with
name and alias.
Also settings will be inspected to get more values names that should be
stored on extra_data field. Setting name is created from current backend
name (all uppercase) plus _EXTRA_DATA.
access_token is always stored.
ID_KEY = 'id'
def get_user_id(self, details, response):
"""OAuth providers return an unique user id in response"""
return response[self.ID_KEY]
def extra_data(cls, user, uid, response, details=None):
"""Return access_token and extra defined names to store in
extra_data field"""
data = {'access_token': response.get('access_token', '')}
name ='-', '_').upper()
names = (cls.EXTRA_DATA or []) + setting(name + '_EXTRA_DATA', [])
for entry in names:
if len(entry) == 2:
(name, alias), discard = entry, False
elif len(entry) == 3:
name, alias, discard = entry
elif len(entry) == 1:
name = alias = entry
else: # ???
value = response.get(name)
if discard and not value:
data[alias] = value
return data
class OpenIDBackend(SocialAuthBackend):
"""Generic OpenID authentication backend"""
name = 'openid'
def get_user_id(self, details, response):
"""Return user unique id provided by service"""
return response.identity_url
def values_from_response(self, response, sreg_names=None, ax_names=None):
"""Return values from SimpleRegistration response or
AttributeExchange response if present.
@sreg_names and @ax_names must be a list of name and aliases
for such name. The alias will be used as mapping key.
values = {}
# Use Simple Registration attributes if provided
if sreg_names:
resp = sreg.SRegResponse.fromSuccessResponse(response)
if resp:
values.update((alias, resp.get(name) or '')
for name, alias in sreg_names)
# Use Attribute Exchange attributes if provided
if ax_names:
resp = ax.FetchResponse.fromSuccessResponse(response)
if resp:
for src, alias in ax_names:
name = alias.replace('old_', '')
values[name] = resp.getSingle(src, '') or values.get(name)
return values
def get_user_details(self, response):
"""Return user details from an OpenID request"""
values = {USERNAME: '', 'email': '', 'fullname': '',
'first_name': '', 'last_name': ''}
# update values using SimpleRegistration or AttributeExchange
# values
fullname = values.get('fullname') or ''
first_name = values.get('first_name') or ''
last_name = values.get('last_name') or ''
if not fullname and first_name and last_name:
fullname = first_name + ' ' + last_name
elif fullname:
try: # Try to split name for django user storage
first_name, last_name = fullname.rsplit(' ', 1)
except ValueError:
last_name = fullname
values.update({'fullname': fullname, 'first_name': first_name,
'last_name': last_name,
USERNAME: values.get(USERNAME) or
(first_name.title() + last_name.title())})
return values
def extra_data(self, user, uid, response, details):
"""Return defined extra data names to store in extra_data field.
Settings will be inspected to get more values names that should be
stored on extra_data field. Setting name is created from current
backend name (all uppercase) plus _SREG_EXTRA_DATA and
_AX_EXTRA_DATA because values can be returned by SimpleRegistration
or AttributeExchange schemas.
Both list must be a value name and an alias mapping similar to
name ='-', '_').upper()
sreg_names = setting(name + '_SREG_EXTRA_DATA')
ax_names = setting(name + '_AX_EXTRA_DATA')
data = self.values_from_response(response, sreg_names, ax_names)
return data
class BaseAuth(object):
"""Base authentication class, new authenticators should subclass
and implement needed methods.
AUTH_BACKEND Authorization backend related with this service
def __init__(self, request, redirect):
self.request = request
# Use request because some auth providers use POST urls with needed
# GET parameters on it = request.REQUEST
self.redirect = redirect
def auth_url(self):
"""Must return redirect URL to auth provider"""
raise NotImplementedError('Implement in subclass')
def auth_html(self):
"""Must return login HTML content returned by provider"""
raise NotImplementedError('Implement in subclass')
def auth_complete(self, *args, **kwargs):
"""Completes loging process, must return user instance"""
raise NotImplementedError('Implement in subclass')
def to_session_dict(self, next_idx, *args, **kwargs):
"""Returns dict to store on session for partial pipeline."""
return {
'next': next_idx,
'args': tuple(map(model_to_ctype, args)),
'kwargs': dict((key, model_to_ctype(val))
for key, val in kwargs.iteritems())
def from_session_dict(self, session_data, *args, **kwargs):
"""Takes session saved data to continue pipeline and merges with any
new extra argument needed. Returns tuple with next pipeline index
entry, arguments and keyword arguments to continue the process."""
args = args[:] + tuple(map(ctype_to_model, session_data['args']))
kwargs = kwargs.copy()
saved_kwargs = dict((key, ctype_to_model(val))
for key, val in session_data['kwargs'].iteritems())
saved_kwargs.update((key, val)
for key, val in kwargs.iteritems())
return (session_data['next'], args, saved_kwargs)
def continue_pipeline(self, *args, **kwargs):
"""Continue previous halted pipeline"""
'auth': self, True
return authenticate(*args, **kwargs)
def request_token_extra_arguments(self):
"""Return extra arguments needed on request-token process,
setting is per backend and defined by:
<backend name in uppercase>_REQUEST_TOKEN_EXTRA_ARGUMENTS.
backend_name ='-', '_')
return setting(backend_name + '_REQUEST_TOKEN_EXTRA_ARGUMENTS', {})
def auth_extra_arguments(self):
"""Return extra arguments needed on auth process, setting is per
backend and defined by:
<backend name in uppercase>_AUTH_EXTRA_ARGUMENTS.
The defaults can be overriden by GET parameters.
backend_name ='-', '_')
extra_arguments = setting(backend_name + '_AUTH_EXTRA_ARGUMENTS', {})
for key, value in extra_arguments.iteritems():
if key in
extra_arguments[key] =[key]
elif value:
extra_arguments[key] = value
return extra_arguments
def uses_redirect(self):
"""Return True if this provider uses redirect url method,
otherwise return false."""
return True
def enabled(cls):
"""Return backend enabled status, all enabled by default"""
return True
def disconnect(self, user, association_id=None):
"""Deletes current backend from user if associated.
Override if extra operations are needed.
name =
if UserSocialAuth.allowed_to_disconnect(user, name, association_id):
if association_id:
raise NotAllowedToDisconnect()
def build_absolute_uri(self, path=None):
"""Build absolute URI for given path. Replace http:// schema with
https:// if SOCIAL_AUTH_REDIRECT_IS_HTTPS is defined.
uri = self.request.build_absolute_uri(path)
uri = uri.replace('http://', 'https://')
return uri
class OpenIdAuth(BaseAuth):
"""OpenId process handling"""
def auth_url(self):
"""Return auth URL returned by service"""
openid_request = self.setup_request(self.auth_extra_arguments())
# Construct completion URL, including page we should redirect to
return_to = self.build_absolute_uri(self.redirect)
return openid_request.redirectURL(self.trust_root(), return_to)
def auth_html(self):
"""Return auth HTML returned by service"""
openid_request = self.setup_request(self.auth_extra_arguments())
return_to = self.build_absolute_uri(self.redirect)
form_tag = {'id': 'openid_message'}
return openid_request.htmlMarkup(self.trust_root(), return_to,
def trust_root(self):
"""Return trust-root option"""
return setting('OPENID_TRUST_ROOT') or self.build_absolute_uri('/')
def continue_pipeline(self, *args, **kwargs):
"""Continue previous halted pipeline"""
response = self.consumer().complete(dict(,
'auth': self,
'response': response, True
return authenticate(*args, **kwargs)
def auth_complete(self, *args, **kwargs):
"""Complete auth process"""
response = self.consumer().complete(dict(,
if not response:
raise AuthException(self, 'OpenID relying party endpoint')
elif response.status == SUCCESS:
'auth': self,
'response': response, True
return authenticate(*args, **kwargs)
elif response.status == FAILURE:
raise AuthFailed(self, response.message)
elif response.status == CANCEL:
raise AuthCanceled(self)
raise AuthUnknownError(self, response.status)
def setup_request(self, extra_params=None):
"""Setup request"""
openid_request = self.openid_request(extra_params)
# Request some user details. Use attribute exchange if provider
# advertises support.
if openid_request.endpoint.supportsType(ax.AXMessage.ns_uri):
fetch_request = ax.FetchRequest()
# Mark all attributes as required, Google ignores optional ones