def test_get_missing_backend(self): with self.assertRaisesRegexp(MissingBackend, 'Missing backend "foobar" entry'): get_backend(('social.backends.github.GithubOAuth2', 'social.backends.facebook.FacebookOAuth2', 'social.backends.flickr.FlickrOAuth'), 'foobar')
def test_get_backend(self): backend = get_backend(( 'social.backends.github.GithubOAuth2', 'social.backends.facebook.FacebookOAuth2', 'social.backends.flickr.FlickrOAuth' ), 'github') expect(backend).to.equal(GithubOAuth2)
def test_get_missing_backend(self): backend = get_backend(( 'social.backends.github.GithubOAuth2', 'social.backends.facebook.FacebookOAuth2', 'social.backends.flickr.FlickrOAuth' ), 'foobar') expect(backend).to.equal(None)
def test_get_backend(self): backend = get_backend(( 'social.backends.github.GithubOAuth2', 'social.backends.facebook.FacebookOAuth2', 'social.backends.flickr.FlickrOAuth' ), 'github') self.assertEqual(backend, GithubOAuth2)
def get_backend(self, strategy): from social.backends.utils import get_backend from social.apps.pyramid_app.utils import get_helper if self.provider is None or self.provider == '': raise ValueError('Provider has not been set') backends = get_helper('AUTHENTICATION_BACKENDS') return get_backend(backends, self.provider)
def get_queryset(self): provider = self.request.query_params.get('provider', None) user_social_auth = self.request.user.social_auth.filter(provider=provider).first() if user_social_auth: # `.first()` doesn't fail, it just returns None backend = get_backend(settings.AUTHENTICATION_BACKENDS, provider) friends = backend.get_friends(user_social_auth) return friends else: raise AuthenticationFailed("User is not authenticated with {}".format(provider))
def get_strategy(backends, strategy, storage, request=None, backend=None, *args, **kwargs): if backend: Backend = get_backend(backends, backend) if not Backend: raise ValueError("Missing backend entry") else: Backend = None Strategy = module_member(strategy) Storage = module_member(storage) return Strategy(Backend, Storage, request, backends=backends, *args, **kwargs)
def get_queryset(self): provider = self.request.query_params.get('provider', None) user_social_auth = self.request.user.social_auth.filter( provider=provider).first() if user_social_auth: # `.first()` doesn't fail, it just returns None backend = get_backend(settings.AUTHENTICATION_BACKENDS, provider) friends = backend.get_friends(user_social_auth) return friends else: raise AuthenticationFailed( "User is not authenticated with {}".format(provider))
def is_valid(self, bundle, request=None): if not bundle.data: return {'__all__': 'Please add provider and access_token arguments'} errors = {} provider = bundle.data.get('provider') backend = get_backend(settings.AUTHENTICATION_BACKENDS, provider) if not backend: errors['provider'] = 'This provider is not supported' return errors
def oauth_discovery(request): from social.apps.django_app.utils import BACKENDS from social.backends.utils import get_backend fiware_auth_backend = get_backend(BACKENDS, 'fiware') endpoints = { 'flows': ["Authorization Code Grant", "Resource Owner Password Credentials Grant"], 'auth_endpoint': fiware_auth_backend.AUTHORIZATION_URL, 'token_endpoint': fiware_auth_backend.ACCESS_TOKEN_URL, 'default_redirect_uri': get_absolute_reverse_url('oauth.default_redirect_uri', request), 'version': '2.0', } return HttpResponse(json.dumps(endpoints), content_type='application/json; charset=UTF-8')
def oauth_discovery(request): from social.apps.django_app.utils import BACKENDS from social.backends.utils import get_backend fiware_auth_backend = get_backend(BACKENDS, 'fiware') endpoints = { 'flows': ["Authorization Code Grant", "Resource Owner Password Credentials Grant"], 'auth_endpoint': fiware_auth_backend.AUTHORIZATION_URL, 'token_endpoint': fiware_auth_backend.ACCESS_TOKEN_URL, 'default_redirect_uri': get_absolute_reverse_url('oauth.default_redirect_uri', request), 'version': '2.0', } return HttpResponse(json.dumps(endpoints, sort_keys=True), content_type='application/json; charset=UTF-8')
def hydrate(self, bundle): access_token = bundle.data.get('access_token') provider = bundle.data.get('provider') social_auth_backend = get_backend(settings.AUTHENTICATION_BACKENDS, provider) if social_auth_backend and access_token: try: social_auth = social_auth_backend(strategy=load_strategy( request=bundle.request, backend=provider, )) user = social_auth.do_auth(access_token) bundle.obj.user = user except Exception as e: logger.exception(e) raise ImmediateHttpResponse( response=http.HttpBadRequest('Invalid access token')) return bundle
def get_strategy(backends, strategy, storage, request=None, backend=None, *args, **kwargs): if backend: Backend = get_backend(backends, backend) if not Backend: raise ValueError('Missing backend entry') else: Backend = None Strategy = module_member(strategy) Storage = module_member(storage) return Strategy(Backend, Storage, request, backends=backends, *args, **kwargs)
def load_backend(strategy, name, redirect_uri): backends = get_helper('AUTHENTICATION_BACKENDS') Backend = get_backend(backends, name) return Backend(strategy, redirect_uri)
def load_backend(strategy, name, redirect_uri): Backend = get_backend(BACKENDS, name) return Backend(strategy, redirect_uri)
def get_backend(self, strategy): return get_backend(strategy.backends, self.provider)
from django.utils.translation import ugettext_lazy as _ from django.views.decorators.cache import cache_page from wirecloud.commons.utils.template import TemplateParser from wirecloud.platform.core.plugins import get_version_hash from wirecloud.platform.markets.utils import MarketManager from wirecloud.platform.plugins import WirecloudPlugin, build_url_template import wirecloud.fiware from wirecloud.fiware.marketAdaptor.views import get_market_adaptor, get_market_user_data from wirecloud.fiware.storeclient import UnexpectedResponse try: from social.apps.django_app.utils import BACKENDS from social.backends.utils import get_backend FIWARE_SOCIAL_AUTH_BACKEND = get_backend(BACKENDS, 'fiware') IDM_SUPPORT_ENABLED = 'wirecloud.fiware' in settings.INSTALLED_APPS and 'social.apps.django_app.default' in settings.INSTALLED_APPS except: IDM_SUPPORT_ENABLED = False def auth_fiware_token(auth_type, token): from social.apps.django_app.default.models import UserSocialAuth user_data = FIWARE_SOCIAL_AUTH_BACKEND._user_data(token) return UserSocialAuth.objects.get(provider='fiware', uid=user_data['username']).user class FiWareMarketManager(MarketManager):
def post_social_media(user_social_auth, social_obj): backend = get_backend(settings.AUTHENTICATION_BACKENDS, user_social_auth.provider) backend.post(user_social_auth, social_obj)
def load_backend(strategy, name, redirect_uri): backends = get_helper('AUTHENTICATION_BACKENDS') Backend = get_backend(backends, name) return Backend(strategy=strategy, redirect_uri=redirect_uri)
def load_backend(request_handler, strategy, name, redirect_uri): backends = get_helper(request_handler, 'AUTHENTICATION_BACKENDS') Backend = get_backend(backends, name) return Backend(strategy, redirect_uri)
from django.utils.translation import ugettext_lazy as _ from django.views.decorators.cache import cache_page from wirecloud.commons.utils.template import TemplateParser from wirecloud.platform.core.plugins import get_version_hash from wirecloud.platform.markets.utils import MarketManager from wirecloud.platform.plugins import WirecloudPlugin, build_url_template import wirecloud.fiware from wirecloud.fiware.marketAdaptor.views import get_market_adaptor, get_market_user_data from wirecloud.fiware.storeclient import UnexpectedResponse try: from social.apps.django_app.utils import BACKENDS from social.backends.utils import get_backend FIWARE_SOCIAL_AUTH_BACKEND = get_backend(BACKENDS, 'fiware') IDM_SUPPORT_ENABLED = 'wirecloud.fiware' in settings.INSTALLED_APPS and 'social.apps.django_app.default' in settings.INSTALLED_APPS except: IDM_SUPPORT_ENABLED = False def auth_fiware_token(auth_type, token): from social.apps.django_app.default.models import UserSocialAuth user_data = FIWARE_SOCIAL_AUTH_BACKEND._user_data(token) return UserSocialAuth.objects.get(provider='fiware', uid=user_data['username']).user class FiWareMarketManager(MarketManager): _user = None
def get_backend(self, strategy=None): strategy = strategy or get_current_strategy() if strategy: return get_backend(strategy.get_backends(), self.provider)
def test_get_backend(self): backend = get_backend(('social.backends.github.GithubOAuth2', 'social.backends.facebook.FacebookOAuth2', 'social.backends.flickr.FlickrOAuth'), 'github') self.assertEqual(backend, GithubOAuth2)
def load_proper_backend(self): backends = load_backends(settings.AUTHENTICATION_BACKENDS) backend_class_name = get_backend(backends, self.provider) self.strategy.backend = backend_class_name()