Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use local host in oauth callback
Krzysiek Szularzcde2dd2933bb
diff --git a/registration/utils.py b/registration/utils.pyindex 66e06f20c4..927609abe2 100644--- a/registration/utils.py+++ b/registration/utils.py@@ -1,4 +1,3 @@-from django.contrib.sites.models import get_current_sitefrom django.core.urlresolvers import reversefrom django.conf import settingsfrom django.template.loader import render_to_string@@ -9,69 +8,73 @@ import jsonfrom oauth2client.client import OAuth2WebServerFlow, FlowExchangeError-def get_callback_url(service):- # TODO: domain- return "http://localhost:8000" + reverse('registration:oauth_callback',- kwargs={'service': service})+def get_protocol_and_host(request):+ return 'http%(secure)s://%(host)s' % {+ 'secure': 's' if request.is_secure() else '',+ 'host': request.get_host()}-def get_google_flow(**kwargs):+def get_callback_url(host, **kwargs):+ return host + reverse('registration:oauth_callback', kwargs=kwargs)+++def get_google_flow(local_host, **kwargs):return OAuth2WebServerFlow(client_id=settings.GOOGLE_CLIENT_ID,client_secret=settings.GOOGLE_SECRET,- redirect_uri=get_callback_url('google'),+ redirect_uri=get_callback_url(host=local_host, service='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")+def get_google_userinfo_url():+ return 'https://www.googleapis.com/oauth2/v1/userinfo'-def get_google_login_url():+def get_google_login_url(local_host):return ('https://accounts.google.com/o/oauth2/auth?''scope=https://www.googleapis.com/auth/userinfo.email+''https://www.googleapis.com/auth/plus.me''&redirect_uri=%(callback)s''&response_type=code''&client_id=%(client_id)s' % {- 'callback': get_callback_url('google'),+ 'callback': get_callback_url(host=local_host, service='google'),'client_id': settings.GOOGLE_CLIENT_ID})-def google_callback(GET):- code = GET.get('code', '')+def google_callback(local_host, data):+ code = data.get('code', '')try:- credentials = get_google_flow().step2_exchange(code)+ credentials = get_google_flow(local_host).step2_exchange(code)except FlowExchangeError: # TODO: log thisreturn None, Nonehttp = credentials.authorize(httplib2.Http())# TODO: some smarter exception handling heretry:- _header, content = http.request(google_query_url())+ _header, content = http.request(get_google_userinfo_url())except Exception: # TODO: log thisreturn None, Nonegoogle_user_data = json.loads(content)- email = (google_user_data['email']- if google_user_data.get('verified_email')- else '')external_username = google_user_data['id']-- return email, external_username+ if not google_user_data.get('verified_email'):+ return '', external_username+ return google_user_data['email'], external_username-def get_facebook_login_url():+def get_facebook_login_url(local_host):return facebook.auth_url(settings.FACEBOOK_APP_ID,- get_callback_url('facebook'),+ get_callback_url(host=local_host,+ service='facebook'),['email'])-def facebook_callback(GET):+def facebook_callback(local_host, data):+ code = data.get('code', '')try:facebook_auth_data = facebook.get_access_token_from_code(- GET.get('code', ''), get_callback_url('facebook'),+ code, get_callback_url(host=local_host, service='facebook'),settings.FACEBOOK_APP_ID, settings.FACEBOOK_SECRET)except Exception: # TODO: log thisreturn None, None@@ -88,11 +91,10 @@ def facebook_callback(GET):return email, external_username-def get_email_confirmation_message(request, email_confirmation):- return render_to_string(- 'registration/email/confirm_email_ownership.txt',- {'confirmation_url':- unicode(get_current_site(request))- + reverse("registration:confirm_email", kwargs={- 'pk': email_confirmation.pk,- 'token': email_confirmation.token})})+def get_email_confirmation_message(local_host, email_confirmation):+ confirmation_url = local_host + reverse(+ 'registration:confirm_email',+ kwargs={'pk': email_confirmation.pk,+ 'token': email_confirmation.token})+ return render_to_string('registration/email/confirm_email_ownership.txt',+ {'confirmation_url': confirmation_url})diff --git a/registration/views.py b/registration/views.pyindex 028371623b..7882dd270f 100644--- a/registration/views.py+++ b/registration/views.py@@ -1,4 +1,4 @@-from datetime import datetime+from django.utils import timezonefrom django.contrib.auth.views import (login as django_login_view,@@ -24,15 +24,17 @@ from .utils import (get_google_login_url,get_facebook_login_url,get_email_confirmation_message,+ get_protocol_and_host,)User = get_user_model()-now = datetime.now+now = timezone.nowdef login(request):- ctx = {'facebook_login_url': get_facebook_login_url(),- 'google_login_url': get_google_login_url()}+ local_host = get_protocol_and_host(request)+ ctx = {'facebook_login_url': get_facebook_login_url(local_host),+ 'google_login_url': get_google_login_url(local_host)}return django_login_view(request, authentication_form=LoginForm,extra_context=ctx)@@ -42,10 +44,12 @@ def logout(request):def oauth_callback(request, service):+ local_host = get_protocol_and_host(request)+if service == 'facebook':- email, external_username = facebook_callback(request.GET)+ email, external_username = facebook_callback(local_host, request.GET)elif service == 'google':- email, external_username = google_callback(request.GET)+ email, external_username = google_callback(local_host, request.GET)else:return HttpResponseNotFound()@@ -60,19 +64,18 @@ def oauth_callback(request, service):external_username=external_username)if user:- auth_login(request, user)- msg = 'You have been successfully logged in.'- messages.success(request, msg)+ _login_user(request, user)return redirect('home')else:request.session['confirmed_email'] = emailrequest.session['external_service'] = servicerequest.session['external_username'] = external_username-return redirect('registration:register')def register(request):+ local_host = get_protocol_and_host(request)+if request.method == 'GET':email = request.session.get('confirmed_email', None)form = EmailForm({'email': email} if email else None)@@ -102,14 +105,12 @@ def register(request):external_user.user = userexternal_user.save()user = authenticate(user=user)- auth_login(request, user)- msg = 'You have been successfully logged in.'- messages.success(request, msg)+ _login_user(request, user)else:email_confirmation = EmailConfirmation.objects.create(email=submitted_email, external_user=external_user)message = get_email_confirmation_message(- request, email_confirmation)+ local_host, email_confirmation)subject = '[Saleor] Email confirmation'EmailMessage(subject, message, to=[submitted_email]).send()messages.warning(@@ -134,16 +135,20 @@ def confirm_email(request, pk, token):formset = EmailConfirmationFormset(email_confirmation=email_confirmation)- elif request.method == 'POST':+ if request.method == 'POST':formset = EmailConfirmationFormset(email_confirmation=email_confirmation, data=request.POST)if formset.is_valid():user = formset.save()user = authenticate(user=user)- auth_login(request, user)- msg = 'You have been successfully logged in.'- messages.success(request, msg)+ _login_user(request, user)return redirect('home')return TemplateResponse(request, 'registration/set_password.html', {'formset': formset})+++def _login_user(request, user):+ auth_login(request, user)+ msg = 'You have been successfully logged in.'+ messages.success(request, msg)