Skip to content

Commit

Permalink
Merge branch 'psa2' into psa2_master_merge
Browse files Browse the repository at this point in the history
Conflicts:
	social_auth/__init__.py
	social_auth/backends/__init__.py
	social_auth/backends/contrib/github.py
	social_auth/backends/contrib/linkedin.py
	social_auth/backends/facebook.py
	social_auth/backends/google.py
	social_auth/db/base.py
	social_auth/db/django_models.py
	social_auth/tests/facebook.py
  • Loading branch information
omab committed Aug 29, 2013
2 parents 8bbaff0 + 9c4029c commit eafa7b9
Show file tree
Hide file tree
Showing 108 changed files with 454 additions and 9,286 deletions.
6 changes: 6 additions & 0 deletions README.rst
@@ -1,3 +1,7 @@
**NOTE: THIS LIBRARY IS DEPRECATED IN FAVOR OF** `python-social-auth`_. **RIGHT NOW
THIS LIBRARY DEPENDS DIRECTLY ON** `python-social-auth`_ **AND SHOULD BE CONSIDERED
AS A MIGRATION STEP**

Django Social Auth
==================

Expand All @@ -10,6 +14,7 @@ third parties.

You can view this app's documentation on `Read the Docs`_ too.


.. contents:: Table of Contents


Expand Down Expand Up @@ -214,3 +219,4 @@ Some bits were derived from others' work and copyrighted by:
.. _Fedora OpenID: https://fedoraproject.org/wiki/OpenID
.. _Exacttarget HubExchange: http://code.exacttarget.com/
.. _Appsfuel OAuth2: http://docs.appsfuel.com/api_reference#api_reference
.. _python-social-auth: https://github.com/omab/python-social-auth
44 changes: 28 additions & 16 deletions example/app/facebook.py
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.auth import BACKEND_SESSION_KEY
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse
Expand All @@ -9,11 +10,15 @@

from social_auth.models import UserSocialAuth
from social_auth.views import complete as social_complete
from social_auth.utils import setting
from social_auth.backends.facebook import load_signed_request, FacebookBackend
from social_auth.backends.facebook import FacebookBackend


def is_complete_authentication(request):
return request.user.is_authenticated() and FacebookBackend.__name__ in request.session.get(BACKEND_SESSION_KEY, '')
return request.user.is_authenticated() and \
FacebookBackend.__name__ in request.session.get(
BACKEND_SESSION_KEY, ''
)


def get_access_token(user):
key = str(user.id)
Expand All @@ -23,41 +28,47 @@ def get_access_token(user):
if access_token is None:
try:
social_user = user.social_user if hasattr(user, 'social_user') \
else UserSocialAuth.objects.get(user=user.id, provider=FacebookBackend.name)
else UserSocialAuth.objects.get(
user=user.id, provider=FacebookBackend.name
)
except UserSocialAuth.DoesNotExist:
return None

if social_user.extra_data:
access_token = social_user.extra_data.get('access_token')
expires = social_user.extra_data.get('expires')

cache.set(key, access_token, int(expires) if expires is not None else 0)

cache.set(key, access_token, int(expires) if expires is not None
else 0)
return access_token


# Facebook decorator to setup environment
def facebook_decorator(func):
def wrapper(request, *args, **kwargs):
user = request.user

# User must me logged via FB backend in order to ensure we talk about the same person
# User must me logged via FB backend in order to ensure we talk about
# the same person
if not is_complete_authentication(request):
try:
user = social_complete(request, FacebookBackend.name)
except ValueError:
pass # no matter if failed
pass # no matter if failed

# Not recommended way for FB, but still something we need to be aware of
# Not recommended way for FB, but still something we need to be aware
# of
if isinstance(user, HttpResponse):
kwargs.update({'auth_response': user})
# Need to re-check the completion
else:
else: # Need to re-check the completion
if is_complete_authentication(request):
kwargs.update({'access_token': get_access_token(request.user)})
else:
request.user = AnonymousUser()

signed_request = load_signed_request(request.REQUEST.get('signed_request', ''))
signed_request = FacebookBackend().load_signed_request(
request.REQUEST.get('signed_request', '')
)
if signed_request:
kwargs.update({'signed_request': signed_request})

Expand All @@ -70,9 +81,10 @@ def wrapper(request, *args, **kwargs):
@facebook_decorator
def facebook_view(request, *args, **kwargs):
# If there is a ready response just return it. Not recommended though.
auth_response = kwargs.get('auth_response')
auth_response = kwargs.get('auth_response')
if auth_response:
return auth_response

return render_to_response('facebook.html', {'fb_app_id':setting('FACEBOOK_APP_ID'),
'warning': request.method == 'GET'}, RequestContext(request))
return render_to_response('facebook.html', {
'fb_app_id': getattr(settings, 'FACEBOOK_APP_ID', None),
'warning': request.method == 'GET'
}, RequestContext(request))
23 changes: 11 additions & 12 deletions example/app/pipeline.py
@@ -1,27 +1,26 @@
from django.http import HttpResponseRedirect


def redirect_to_form(*args, **kwargs):
if not kwargs['request'].session.get('saved_username') and \
kwargs.get('user') is None:
def redirect_to_form(strategy, user=None, *args, **kwargs):
if not strategy.session_get('saved_username') and user is None:
return HttpResponseRedirect('/form/')


def username(request, *args, **kwargs):
if kwargs.get('user'):
username = kwargs['user'].username
def username(strategy, user=None, *args, **kwargs):
if user:
username = user.username
else:
username = request.session.get('saved_username')
username = strategy.session_get('saved_username')
return {'username': username}


def redirect_to_form2(*args, **kwargs):
if not kwargs['request'].session.get('saved_first_name'):
def redirect_to_form2(strategy, *args, **kwargs):
if strategy.session_get('saved_first_name'):
return HttpResponseRedirect('/form2/')


def first_name(request, *args, **kwargs):
if 'saved_first_name' in request.session:
def first_name(strategy, *args, **kwargs):
if strategy.session_get('saved_first_name'):
user = kwargs['user']
user.first_name = request.session.get('saved_first_name')
user.first_name = strategy.session_get('saved_first_name')
user.save()
7 changes: 2 additions & 5 deletions example/app/views.py
Expand Up @@ -6,7 +6,6 @@
from django.contrib.messages.api import get_messages

from social_auth import __version__ as version
from social_auth.utils import setting


def home(request):
Expand Down Expand Up @@ -44,18 +43,16 @@ def logout(request):

def form(request):
if request.method == 'POST' and request.POST.get('username'):
name = setting('SOCIAL_AUTH_PARTIAL_PIPELINE_KEY', 'partial_pipeline')
request.session['saved_username'] = request.POST['username']
backend = request.session[name]['backend']
backend = request.session['partial_pipeline']['backend']
return redirect('socialauth_complete', backend=backend)
return render_to_response('form.html', {}, RequestContext(request))


def form2(request):
if request.method == 'POST' and request.POST.get('first_name'):
request.session['saved_first_name'] = request.POST['first_name']
name = setting('SOCIAL_AUTH_PARTIAL_PIPELINE_KEY', 'partial_pipeline')
backend = request.session[name]['backend']
backend = request.session['partial_pipeline']['backend']
return redirect('socialauth_complete', backend=backend)
return render_to_response('form2.html', {}, RequestContext(request))

Expand Down
47 changes: 29 additions & 18 deletions example/app/vkontakte.py
Expand Up @@ -9,11 +9,15 @@

from social_auth.models import UserSocialAuth
from social_auth.views import complete as social_complete
from social_auth.utils import setting
from social_auth.backends.contrib.vk import VKOAuth2Backend, vk_api
from social_auth.backends.contrib.vk import VKOAuth2Backend


def is_complete_authentication(request):
return request.user.is_authenticated() and VKOAuth2Backend.__name__ in request.session.get(BACKEND_SESSION_KEY, '')
return request.user.is_authenticated() and \
VKOAuth2Backend.__name__ in request.session.get(
BACKEND_SESSION_KEY, ''
)


def get_access_token(user):
key = str(user.id)
Expand All @@ -23,35 +27,40 @@ def get_access_token(user):
if access_token is None:
try:
social_user = user.social_user if hasattr(user, 'social_user') \
else UserSocialAuth.objects.get(user=user.id, provider=VKOAuth2Backend.name)
else UserSocialAuth.objects.get(
user=user.id,
provider=VKOAuth2Backend.name
)
except UserSocialAuth.DoesNotExist:
return None

if social_user.extra_data:
access_token = social_user.extra_data.get('access_token')
expires = social_user.extra_data.get('expires')

cache.set(key, access_token, int(expires) if expires is not None else 0)

cache.set(key, access_token, int(expires) if expires is not None
else 0)
return access_token


# VK decorator to setup environment
def vkontakte_decorator(func):
def wrapper(request, *args, **kwargs):
user = request.user

# User must me logged via VKontakte backend in order to ensure we talk about the same person
# User must me logged via VKontakte backend in order to ensure we talk
# about the same person
if not is_complete_authentication(request):
try:
user = social_complete(request, VKOAuth2Backend.name)
except (ValueError, AttributeError):
pass # no matter if failed
pass # no matter if failed

# Not recommended way for VK, but still something we need to be aware of
# Not recommended way for VK, but still something we need to be aware
# of
if isinstance(user, HttpResponse):
kwargs.update({'auth_response': user})
# Need to re-check the completion
else:
else: # Need to re-check the completion
if is_complete_authentication(request):
kwargs.update({'access_token': get_access_token(request.user)})
else:
Expand All @@ -61,18 +70,20 @@ def wrapper(request, *args, **kwargs):

return wrapper


@vkontakte_decorator
def vkontakte_view(request, *args, **kwargs):
# If there is a ready response just return it. Not recommended because pipeline redirects fail the normal workflow
# here.
# If there is a ready response just return it. Not recommended because
# pipeline redirects fail the normal workflow here.
auth_response = kwargs.get('auth_response')
if auth_response:
for item in auth_response.items():
if item[0] == 'Location' and 'form' in item[1]:
return auth_response

return render_to_response('vkontakte_app.html',
{'vk_app_id': settings.VKONTAKTE_APP_AUTH['id'] if hasattr(settings, 'VKONTAKTE_APP_AUTH') else None,
'app_scope': ','.join(settings.VKONTAKTE_OAUTH2_EXTRA_SCOPE),
'warning': not request.GET.get('user_id')},
RequestContext(request))
return render_to_response('vkontakte_app.html', {
'vk_app_id': settings.VKONTAKTE_APP_AUTH['id']
if hasattr(settings, 'VKONTAKTE_APP_AUTH') else None,
'app_scope': ','.join(settings.VKONTAKTE_OAUTH2_EXTRA_SCOPE),
'warning': not request.GET.get('user_id')
}, RequestContext(request))
42 changes: 24 additions & 18 deletions example/example/settings.py
@@ -1,11 +1,16 @@
import sys
from os.path import abspath, dirname, basename, join


try:
import social_auth
except ImportError:
import sys
sys.path.insert(0, '..')
#try:
#import social_auth
#social_auth # pyflakes
#except ImportError:
#import sys
# sys.path.insert(0, '..')

sys.path.insert(0, '..')
sys.path.insert(0, '../../python-social-auth')


DEBUG = True
Expand Down Expand Up @@ -44,15 +49,15 @@
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

SECRET_KEY = '_u6ym67ywnj0ugi2=6f-a_361i6o5elx91hftz$+klw)(*pqjw'

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
# 'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
Expand Down Expand Up @@ -81,7 +86,8 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'south',
# 'south',
'social.apps.django_app.default',
'social_auth',
'app',
)
Expand Down Expand Up @@ -111,44 +117,44 @@
}

AUTHENTICATION_BACKENDS = (
'social_auth.backends.OpenIDBackend',
'social_auth.backends.twitter.TwitterBackend',
'social_auth.backends.facebook.FacebookBackend',
'social_auth.backends.google.GoogleOAuthBackend',
'social_auth.backends.google.GoogleOAuth2Backend',
'social_auth.backends.google.GoogleBackend',
'social_auth.backends.yahoo.YahooBackend',
'social_auth.backends.stripe.StripeBackend',
'social_auth.backends.steam.SteamBackend',
'social_auth.backends.reddit.RedditBackend',
'social_auth.backends.amazon.AmazonBackend',
'social_auth.backends.browserid.BrowserIDBackend',
'social_auth.backends.contrib.linkedin.LinkedinBackend',
'social_auth.backends.contrib.skyrock.SkyrockBackend',
'social_auth.backends.contrib.flickr.FlickrBackend',
'social_auth.backends.contrib.instagram.InstagramBackend',
'social_auth.backends.contrib.github.GithubBackend',
'social_auth.backends.contrib.yandex.YandexBackend',
'social_auth.backends.contrib.yandex.YandexOAuth2Backend',
'social_auth.backends.contrib.yandex.YaruBackend',
'social_auth.backends.contrib.disqus.DisqusBackend',
'social_auth.backends.contrib.yahoo.YahooOAuthBackend',
'social_auth.backends.contrib.foursquare.FoursquareBackend',
'social_auth.backends.OpenIDBackend',
'social_auth.backends.contrib.live.LiveBackend',
'social_auth.backends.contrib.livejournal.LiveJournalBackend',
'social_auth.backends.contrib.douban.DoubanBackend',
'social_auth.backends.browserid.BrowserIDBackend',
'social_auth.backends.contrib.vk.VKOpenAPIBackend',
'social_auth.backends.contrib.yandex.YandexOAuth2Backend',
'social_auth.backends.contrib.yandex.YaruBackend',
'social_auth.backends.contrib.vk.VKOAuth2Backend',
'social_auth.backends.contrib.odnoklassniki.OdnoklassnikiBackend',
'social_auth.backends.contrib.odnoklassniki.OdnoklassnikiAppBackend',
'social_auth.backends.contrib.vk.VKOAuth2Backend',
'social_auth.backends.contrib.mailru.MailruBackend',
'social_auth.backends.contrib.dailymotion.DailymotionBackend',
'social_auth.backends.contrib.shopify.ShopifyBackend',
'social_auth.backends.contrib.exacttarget.ExactTargetBackend',
# 'social_auth.backends.contrib.shopify.ShopifyBackend',
# 'social_auth.backends.contrib.exacttarget.ExactTargetBackend',
'social_auth.backends.contrib.stocktwits.StocktwitsBackend',
'social_auth.backends.contrib.behance.BehanceBackend',
'social_auth.backends.contrib.readability.ReadabilityBackend',
'social_auth.backends.contrib.fedora.FedoraBackend',
'social_auth.backends.steam.SteamBackend',
'social_auth.backends.reddit.RedditBackend',
'social_auth.backends.amazon.AmazonBackend',
'django.contrib.auth.backends.ModelBackend',
)

Expand Down
5 changes: 1 addition & 4 deletions requirements.txt
@@ -1,5 +1,2 @@
django>=1.2.5
oauth2>=1.5.167
python_openid>=2.2
selenium>=2.29.0
mock==1.0.1
python-social-auth>=0.1.10

0 comments on commit eafa7b9

Please sign in to comment.