-
Notifications
You must be signed in to change notification settings - Fork 0
Oauth
Daniel Valverde edited this page Jan 3, 2014
·
2 revisions
This module provides the base views to authenticate a shop through the Oauth Services provided by Shopify.
This involves 3 basic steps:
LOGIN VIEW - SHOPIFY REDIRECT - FINALIZE VIEW
Initial login action which ask user for their ${shop}.myshopify.com address and redirect to shopify auth page.
class LoginView(BaseOauthView):
"""
Initial login action which ask user for their ${shop}.myshopify.com address and redirect user to shopify auth page
"""
def get(self, *args, **kwargs):
#If the ${shop}.myshopify.com address is already provided in the URL, just skip to authenticate
if self.request.REQUEST.get('shop'):
shop = self.request.REQUEST.get('shop').strip()
permission_url = shopify.Session.create_permission_url(shop, settings.SHOPIFY_API_SCOPE)
return redirect(permission_url)
return super(BaseOauthView, self).get(*args, **kwargs)This is on Shopify Store side, and involves the user acceptance window (to install the app) in case its first time login. Otherwise the user is redirected to the Finalize View
Finalize login action which receives the request from shopify, initialize the shopify session and login the user to our app.
class FinalizeView(BaseOauthView):
"""
Finalize login action which receives the request from shopify and login the user to our app.
"""
def get(self, *args, **kwargs):
shop_url = self.request.REQUEST.get('shop')
# Checking if the user has a previous valid session initialized, not need to initialize again
if hasattr(self.request, 'session') and 'shopify' in self.request.session and self.request.session["shopify"]["shop_url"] == shop_url:
return redirect("/shop")
# Initializing shopify session
try:
shopify_session = shopify.Session(shop_url)
shopify_session.request_token(self.request.GET["code"])
except:
#Shopify session fails, redirect to login initial step
return redirect("%s?shop=%s"%("/oauth/login", shop_url))
# Store shopify sesssion data in our session
self.request.session['shopify'] = {
"shop_url": shop_url,
"access_token": shopify_session.token,
}
response = redirect(self._return_address(self.request))
return response