Skip to content

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

Add google plus sign in

Marek Brzóskab6a815ab5ce5
diff --git a/registration/templates/registration/login.html b/registration/templates/registration/login.html
index fa12ea7a2b..5388267775 100644
--- a/registration/templates/registration/login.html
+++ b/registration/templates/registration/login.html
@@ -33,6 +33,14 @@
OR
</div>
+<div>
+ <a href="{{ google_login_url }}">Login via Google</a>
+</div>
+
+<div>
+ OR
+</div>
+
<p>
<a href="{% url 'registration:register' %}">Create an account</a>
</p>
diff --git a/registration/urls.py b/registration/urls.py
index dd3a1b383a..8aea477873 100644
--- a/registration/urls.py
+++ b/registration/urls.py
@@ -6,7 +6,7 @@ urlpatterns = patterns(
url(r'^login/$', 'registration.views.login', name='login'),
url(r'^logout/$', 'registration.views.logout', name='logout'),
url(r'^register/$', 'registration.views.register', name='register'),
- url(r'^oauth_callback/(?P<service>\w+)$', 'registration.views.oauth_callback',
+ url(r'^oauth_callback/(?P<service>\w+)/$', 'registration.views.oauth_callback',
name='oauth_callback'),
url(r'^confirm_email/$', 'registration.views.confirm_email',
name='confirm_email'),
diff --git a/registration/utils.py b/registration/utils.py
index 871f980303..0057d74c71 100644
--- a/registration/utils.py
+++ b/registration/utils.py
@@ -1,6 +1,24 @@
from django.core.urlresolvers import reverse
+from django.conf import settings
+
+from oauth2client.client import OAuth2WebServerFlow
def callback_url(service):
+ # TODO: domain
return "http://localhost:8000" + reverse('registration:oauth_callback',
kwargs={'service': service})
+
+
+def get_google_flow(**kwargs):
+ return OAuth2WebServerFlow(
+ client_id=settings.GOOGLE_CLIENT_ID,
+ client_secret=settings.GOOGLE_SECRET,
+ redirect_uri=callback_url('google'),
+ scope='https://www.googleapis.com/auth/plus.me '
+ 'https://www.googleapis.com/auth/userinfo.email',
+ **kwargs)
+
+
+def google_query_url():
+ return ("https://www.googleapis.com/oauth2/v1/userinfo")
diff --git a/registration/views.py b/registration/views.py
index d28b0f8107..efb7843e32 100644
--- a/registration/views.py
+++ b/registration/views.py
@@ -11,10 +11,12 @@ from django.contrib.auth import login as auth_login, authenticate
from django.http import HttpResponseNotFound
import facebook
+import httplib2
+import json
from .forms import LoginForm, RegisterForm, EmailForm
from .models import ExternalUserID
-from .utils import callback_url
+from .utils import callback_url, get_google_flow, google_query_url
User = get_user_model()
@@ -24,7 +26,13 @@ def login(request):
'facebook_login_url': facebook.auth_url(
settings.FACEBOOK_APP_ID,
callback_url('facebook'),
- ['email'])
+ ['email']),
+ 'google_login_url':
+ 'https://accounts.google.com/o/oauth2/auth?'
+ 'scope=https://www.googleapis.com/auth/userinfo.email+'
+ 'https://www.googleapis.com/auth/plus.me&'
+ 'redirect_uri=http://localhost:8000/account/oauth_callback/google/'
+ '&response_type=code&client_id=656911639082.apps.googleusercontent.com'
}
return django_login_view(request, authentication_form=LoginForm,
extra_context=ctx)
@@ -69,6 +77,19 @@ def oauth_callback(request, service):
user = authenticate(external_service='facebook',
external_username=external_username)
+ elif service == 'google':
+ code = request.GET['code']
+ credentials = get_google_flow().step2_exchange(code)
+ http = credentials.authorize(httplib2.Http())
+ _header, content = http.request(google_query_url())
+ google_user_data = json.loads(content)
+ email = (google_user_data['email']
+ if google_user_data.get('verified_email')
+ else '')
+ external_username = google_user_data['id']
+ user = authenticate(external_service='google',
+ external_username=external_username)
+
else:
return HttpResponseNotFound()