Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Integrate python-social-auth

Mira Szewczyk3f647e64e129
diff --git a/saleor/registration/backends/__init__.py b/saleor/registration/backends/__init__.py
new file mode 100644
index 0000000000..c00b68ab5e
--- /dev/null
+++ b/saleor/registration/backends/__init__.py
@@ -0,0 +1,9 @@
+from ...site.utils import get_authorization_key_for_backend
+
+
+class BaseBackend(object):
+ 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 get_authorization_key_for_backend(self.DB_NAME).key_and_secret()
diff --git a/saleor/registration/backends/facebook.py b/saleor/registration/backends/facebook.py
new file mode 100644
index 0000000000..5e9f96a22b
--- /dev/null
+++ b/saleor/registration/backends/facebook.py
@@ -0,0 +1,8 @@
+from social_core.backends.facebook import FacebookOAuth2
+
+from . import BaseBackend
+from ...site.models import FACEBOOK
+
+
+class CustomFacebookOAuth2(BaseBackend, FacebookOAuth2):
+ DB_NAME = FACEBOOK
diff --git a/saleor/registration/backends/google.py b/saleor/registration/backends/google.py
new file mode 100644
index 0000000000..9bfca049a0
--- /dev/null
+++ b/saleor/registration/backends/google.py
@@ -0,0 +1,8 @@
+from social_core.backends.google import GoogleOAuth2
+
+from . import BaseBackend
+from ...site.models import GOOGLE
+
+
+class CustomGoogleOAuth2(BaseBackend, GoogleOAuth2):
+ DB_NAME = GOOGLE
diff --git a/saleor/settings.py b/saleor/settings.py
index b3fb0c9df0..e3db4b2502 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -96,6 +96,8 @@ context_processors = [
'saleor.core.context_processors.search_enabled',
'saleor.site.context_processors.settings',
'saleor.core.context_processors.webpage_schema',
+ 'social_django.context_processors.backends',
+ 'social_django.context_processors.login_redirect',
]
loaders = [
@@ -131,6 +133,7 @@ MIDDLEWARE_CLASSES = [
'saleor.core.middleware.GoogleAnalytics',
'saleor.core.middleware.CountryMiddleware',
'saleor.core.middleware.CurrencyMiddleware',
+ 'social_django.middleware.SocialAuthExceptionMiddleware',
]
INSTALLED_APPS = [
@@ -374,3 +377,27 @@ GRAPHENE = {
}
SITE_SETTINGS_ID = 1
+
+AUTHENTICATION_BACKENDS = [
+ 'saleor.registration.backends.facebook.CustomFacebookOAuth2',
+ 'saleor.registration.backends.google.CustomGoogleOAuth2',
+ 'django.contrib.auth.backends.ModelBackend',
+]
+
+SOCIAL_AUTH_PIPELINE = [
+ 'social_core.pipeline.social_auth.social_details',
+ 'social_core.pipeline.social_auth.social_uid',
+ 'social_core.pipeline.social_auth.auth_allowed',
+ 'social_core.pipeline.social_auth.social_user',
+ 'social_core.pipeline.social_auth.associate_by_email',
+ 'social_core.pipeline.user.create_user',
+ 'social_core.pipeline.social_auth.associate_user',
+ 'social_core.pipeline.social_auth.load_extra_data',
+ 'social_core.pipeline.user.user_details',
+]
+
+SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = True
+SOCIAL_ATUTH_USER_MODEL = AUTH_USER_MODEL
+SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
+SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
+ 'fields': 'id,name,email', }
diff --git a/saleor/site/models.py b/saleor/site/models.py
index 2024cd71cc..0e6ec9caa4 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -7,7 +7,7 @@ from django.utils.translation import pgettext_lazy
GOOGLE = 'google-oauth2'
FACEBOOK = 'facebook'
-BACKENDS = [(FACEBOOK, 'Facebook'), (GOOGLE, 'Google')]
+BACKENDS = [(FACEBOOK, 'Facebook-Oauth2'), (GOOGLE, 'Google-Oauth2')]
@python_2_unicode_compatible
@@ -43,5 +43,5 @@ class AuthorizationKey(models.Model):
def __str__(self):
return self.name
- def get_secrets(self):
+ def key_and_secret(self):
return self.key, self.password
diff --git a/saleor/urls.py b/saleor/urls.py
index b37ec86680..4c1632a6b5 100644
--- a/saleor/urls.py
+++ b/saleor/urls.py
@@ -33,7 +33,8 @@ urlpatterns = [
url(r'^feeds/', include(feed_urls, namespace='data_feeds')),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
- url(r'', include('payments.urls'))
+ url(r'', include('payments.urls')),
+ url('', include('social_django.urls', namespace='social')),
]
if settings.DEBUG:
diff --git a/templates/account/partials/login_form.html b/templates/account/partials/login_form.html
index 98568afe00..c829d754b1 100644
--- a/templates/account/partials/login_form.html
+++ b/templates/account/partials/login_form.html
@@ -18,6 +18,8 @@
<a class="link--styled" href="{% url 'account_reset_password' %}">
{% trans "Forgot password?" context "Login form secondary link" %}
</a>
+ <a href="{% url 'social:begin' 'facebook' %}">{% trans 'Facebook' %}</a>
+ <a href="{% url 'social:begin' 'google-oauth2' %}">{% trans 'Google' %}</a>
</div>
</div>
</form>
diff --git a/tests/test_registration.py b/tests/test_registration.py
index a1a60b95b8..c616231acd 100644
--- a/tests/test_registration.py
+++ b/tests/test_registration.py
@@ -111,3 +111,11 @@ def test_password_reset_view_get(client, db):
response = client.get(url)
assert response.status_code == 200
assert response.template_name == 'account/password_reset.html'
+
+
+def test_base_backend(authorization_key):
+ base_backend = BaseBackend()
+ base_backend.DB_NAME = 'Backend'
+ key, secret = base_backend.get_key_and_secret()
+ assert key == 'Key'
+ assert secret == 'Password'