Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Grouped settings on top of module

  • Loading branch information...
commit 6593791f7563e9d578a1d283d9a553da6d6957f8 1 parent 45d5d5a
@omab omab authored
Showing with 36 additions and 24 deletions.
  1. +36 −24 social_auth/backends/__init__.py
View
60 social_auth/backends/__init__.py
@@ -67,6 +67,17 @@
# uuid hex characters to keep while generating unique usernames
UUID_MAX_LENGTH = 16
+# a few settings values
+def _setting(name, default=None):
+ return getattr(settings, name, default)
+
+CREATE_USERS = _setting('SOCIAL_AUTH_CREATE_USERS', True)
+LOAD_EXTRA_DATA = _setting('SOCIAL_AUTH_EXTRA_DATA', True)
+FORCE_RANDOM_USERNAME = _setting('SOCIAL_AUTH_FORCE_RANDOM_USERNAME', False)
+USERNAME_FIXER = _setting('SOCIAL_AUTH_USERNAME_FIXER', lambda u: u)
+DEFAULT_USERNAME = _setting('SOCIAL_AUTH_DEFAULT_USERNAME')
+CHANGE_SIGNAL_ONLY = _setting('SOCIAL_AUTH_CHANGE_SIGNAL_ONLY', False)
+
class SocialAuthBackend(ModelBackend):
"""A django.contrib.auth backend that authenticates the user based on
@@ -98,7 +109,7 @@ def authenticate(self, *args, **kwargs):
except UserSocialAuth.DoesNotExist:
user = kwargs.get('user')
if user is None: # new user
- if not getattr(settings, 'SOCIAL_AUTH_CREATE_USERS', True):
+ if not CREATE_USERS:
return None
username = self.username(details)
email = details.get('email')
@@ -119,7 +130,7 @@ def authenticate(self, *args, **kwargs):
self.update_user_details(user, response, details, is_new)
# Update extra_data storage, unless disabled by setting
- if getattr(settings, 'SOCIAL_AUTH_EXTRA_DATA', True):
+ if LOAD_EXTRA_DATA:
extra_data = self.extra_data(user, uid, response, details)
if extra_data and social_user.extra_data != extra_data:
social_user.extra_data = extra_data
@@ -136,26 +147,24 @@ def get_random_username():
"""Return hash from unique string cut at username max length"""
return uuid4().get_hex()[:USERNAME_MAX_LENGTH]
- if getattr(settings, 'SOCIAL_AUTH_FORCE_RANDOM_USERNAME', False):
+ if FORCE_RANDOM_USERNAME:
username = get_random_username()
elif USERNAME in details:
username = details[USERNAME]
- elif hasattr(settings, 'SOCIAL_AUTH_DEFAULT_USERNAME'):
- username = settings.SOCIAL_AUTH_DEFAULT_USERNAME
+ elif DEFAULT_USERNAME:
+ username = DEFAULT_USERNAME
if callable(username):
username = username()
else:
username = None
username = username or get_random_username()
- fixer = getattr(settings, 'SOCIAL_AUTH_USERNAME_FIXER', lambda u: u)
-
name = username
final_username = None
while not final_username:
try:
- fixed_name = fixer(name)
+ fixed_name = USERNAME_FIXER(name)
User.objects.get(username=fixed_name)
except User.DoesNotExist:
final_username = fixed_name
@@ -189,7 +198,7 @@ def update_user_details(self, user, response, details, is_new=False):
changed = False # flag to track changes
# check if values update should be left to signals handlers only
- if not getattr(settings, 'SOCIAL_AUTH_CHANGE_SIGNAL_ONLY', False):
+ if not CHANGE_SIGNAL_ONLY:
for name, value in details.iteritems():
# do not update username, it was already generated by
# self.username(...) and loaded in given instance
@@ -268,8 +277,7 @@ def extra_data(self, user, uid, response, details):
extra_data field"""
data = {'access_token': response.get('access_token', '')}
name = self.name.replace('-', '_').upper()
- names = self.EXTRA_DATA or [] + \
- getattr(settings, name + '_EXTRA_DATA', [])
+ names = self.EXTRA_DATA or [] + _setting(name + '_EXTRA_DATA', [])
data.update((alias, response.get(name)) for name, alias in names)
return data
@@ -348,8 +356,8 @@ def extra_data(self, user, uid, response, details):
SREG_ATTR, OLD_AX_ATTRS or AX_SCHEMA_ATTRS
"""
name = self.name.replace('-', '_').upper()
- sreg_names = getattr(settings, name + '_SREG_EXTRA_DATA', None)
- ax_names = getattr(settings, name + '_AX_EXTRA_DATA', None)
+ sreg_names = _setting(name + '_SREG_EXTRA_DATA')
+ ax_names = _setting(name + '_AX_EXTRA_DATA')
data = self.values_from_response(response, ax_names, sreg_names)
return data
@@ -402,23 +410,27 @@ class OpenIdAuth(BaseAuth):
AUTH_BACKEND = OpenIDBackend
def auth_url(self):
+ """Return auth URL returned by service"""
openid_request = self.setup_request()
# Construct completion URL, including page we should redirect to
return_to = self.request.build_absolute_uri(self.redirect)
- trust_root = getattr(settings, 'OPENID_TRUST_ROOT',
- self.request.build_absolute_uri('/'))
- return openid_request.redirectURL(trust_root, return_to)
+ return openid_request.redirectURL(self.trust_root(), return_to)
def auth_html(self):
+ """Return auth HTML returned by service"""
openid_request = self.setup_request()
return_to = self.request.build_absolute_uri(self.redirect)
- trust_root = getattr(settings, 'OPENID_TRUST_ROOT',
- self.request.build_absolute_uri('/'))
form_tag = {'id': 'openid_message'}
- return openid_request.htmlMarkup(trust_root, return_to,
+ return openid_request.htmlMarkup(self.trust_root(), return_to,
form_tag_attrs=form_tag)
+ def trust_root(self):
+ """Return trust-root option"""
+ return _setting('OPENID_TRUST_ROOT',
+ self.request.build_absolute_uri('/'))
+
def auth_complete(self, *args, **kwargs):
+ """Complete auth process"""
response = self.consumer().complete(dict(self.data.items()),
self.request.build_absolute_uri())
if not response:
@@ -587,8 +599,8 @@ def get_key_and_secret(self):
"""Return tuple with Consumer Key and Consumer Secret for current
service provider. Must return (key, secret), order *must* be respected.
"""
- return getattr(settings, self.SETTINGS_KEY_NAME), \
- getattr(settings, self.SETTINGS_SECRET_NAME)
+ return _setting(self.SETTINGS_KEY_NAME), \
+ _setting(self.SETTINGS_SECRET_NAME)
@classmethod
def enabled(cls):
@@ -652,15 +664,15 @@ def get_key_and_secret(self):
"""Return tuple with Consumer Key and Consumer Secret for current
service provider. Must return (key, secret), order *must* be respected.
"""
- return getattr(settings, self.SETTINGS_KEY_NAME), \
- getattr(settings, self.SETTINGS_SECRET_NAME)
+ return _setting(self.SETTINGS_KEY_NAME), \
+ _setting(self.SETTINGS_SECRET_NAME)
# import sources from where check for auth backends
SOCIAL_AUTH_IMPORT_SOURCES = (
'social_auth.backends',
'social_auth.backends.contrib',
-) + getattr(settings, 'SOCIAL_AUTH_IMPORT_BACKENDS', ())
+) + _setting('SOCIAL_AUTH_IMPORT_BACKENDS', ())
def get_backends():
backends = {}
Please sign in to comment.
Something went wrong with that request. Please try again.