forked from omab/django-social-auth
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
259 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class StopPipeline(Exception): | ||
"""Stop pipeline process exception. | ||
Raise this exception to stop the rest of the pipeline process. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""Django-Social-Auth Pipeline. | ||
Pipelines must return a dictionary with values that will be passed as parameter | ||
to next pipeline item. Pipelines must take **kwargs parameters to avoid | ||
failure. At some point a pipeline entry must create a UserSocialAuth instance | ||
and load it to the output if the user logged in correctly. | ||
""" | ||
from social_auth.models import User | ||
|
||
|
||
USERNAME = 'username' | ||
USERNAME_MAX_LENGTH = User._meta.get_field(USERNAME).max_length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from django.conf import settings | ||
from django.core.exceptions import MultipleObjectsReturned | ||
|
||
from social_auth.models import User | ||
|
||
|
||
def associate_by_email(details, *args, **kwargs): | ||
"""Return user entry with same email address as one returned on details.""" | ||
email = details.get('email') | ||
|
||
if email and getattr(settings, 'SOCIAL_AUTH_ASSOCIATE_BY_MAIL', False): | ||
# try to associate accounts registered with the same email address, | ||
# only if it's a single object. ValueError is raised if multiple | ||
# objects are returned | ||
try: | ||
return {'user': User.objects.get(email=email)} | ||
except MultipleObjectsReturned: | ||
raise ValueError('Not unique email address.') | ||
except User.DoesNotExist: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from django.conf import settings | ||
from django.db.utils import IntegrityError | ||
|
||
from social_auth.models import User, UserSocialAuth | ||
|
||
|
||
def social_auth_user(backend, uid, user=None, *args, **kwargs): | ||
"""Return UserSocialAuth account for backend/uid pair or None if it | ||
doesn't exists. | ||
Raise ValueError if UserSocialAuth entry belongs to another user. | ||
""" | ||
try: | ||
social_user = UserSocialAuth.objects.select_related('user')\ | ||
.get(provider=backend.name, | ||
uid=uid) | ||
except UserSocialAuth.DoesNotExist: | ||
social_user = None | ||
|
||
if user and social_user and social_user.user != user: | ||
raise ValueError('Account already in use.', social_user) | ||
return {'social_user': social_user} | ||
|
||
|
||
def associate_user(backend, user, uid, social_user=None, *args, **kwargs): | ||
"""Associate user social account with user instance.""" | ||
if social_user: | ||
return None | ||
|
||
try: | ||
social = UserSocialAuth.objects.create(user=user, uid=uid, | ||
provider=backend.name) | ||
except IntegrityError: | ||
# Protect for possible race condition, those bastard with FTL | ||
# clicking capabilities, check issue #131: | ||
# https://github.com/omab/django-social-auth/issues/131 | ||
return social_auth_user(backend, uid, user, social_user=social_user, | ||
*args, **kwargs) | ||
else: | ||
return {'social_user': social} | ||
|
||
|
||
def load_extra_data(backend, details, response, social_user, uid, user, | ||
*args, **kwargs): | ||
"""Load extra data from provider and store it on current UserSocialAuth | ||
extra_data field. | ||
""" | ||
if getattr(settings, 'SOCIAL_AUTH_EXTRA_DATA', True): | ||
extra_data = backend.extra_data(user, uid, response, details) | ||
if extra_data and social_user.extra_data != extra_data: | ||
social_user.extra_data = extra_data | ||
social_user.save() |
Oops, something went wrong.