forked from Shopify/sample-django-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
226 lines (164 loc) · 6.74 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from django.shortcuts import render, redirect
from django.conf import settings
from django.contrib import messages
from django.urls import reverse
from django.http import HttpResponse
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.apps import apps
from .models import Shop
from shopify.utils import shop_url
from django.utils import timezone
from django.http import HttpResponseRedirect
from django.http import Http404
from datetime import timedelta
from shopify_app.partnerjam import PartnerJamClient
import binascii
import json
import os
import re
import shopify
import requests
class LoginView(View):
def get(self, request, *args, **kwargs):
if request.GET.get("shop"):
return authenticate(request)
return render(
request, "shopify_app/login.html", {"app_name": "Sample Django app"}
)
def post(self, request):
return authenticate(request)
def callback(request):
params = request.GET.dict()
shop = params.get("shop")
partnerjam_token = request.COOKIES.get("partner_jam_token")
try:
validate_params(request, params)
access_token, access_scopes = exchange_code_for_access_token(request, shop)
shop_record = store_shop_information(access_token, access_scopes, partnerjam_token, shop)
after_authenticate_jobs(shop, access_token)
notify_partnerjam(shop_record)
except ValueError as exception:
messages.error(request, str(exception))
return redirect(reverse("login"))
redirect_uri = build_callback_redirect_uri(request, params)
return redirect(redirect_uri)
def notify_partnerjam(shop_record):
if not shop_record.partnerjam_token:
return
with shopify.Session.temp(shop_record.shopify_domain, 'unstable', shop_record.shopify_token):
shopify_shop = shopify.Shop.current()
data = {
'myshopify_domain': shopify_shop.myshopify_domain,
"shopify_id": shopify_shop.id,
'shop_name': shopify_shop.name,
'token': shop_record.partnerjam_token,
'secret': settings.PARTNERJAM_SECRET,
'test': False,
}
PartnerJamClient.send_webhook(**data)
@csrf_exempt
def uninstall(request):
uninstall_data = json.loads(request.body)
shop = uninstall_data.get("domain")
Shop.objects.filter(shopify_domain=shop).delete()
return HttpResponse(status=204)
# Login helper methods
def authenticate(request):
try:
shop = get_sanitized_shop_param(request)
scopes, redirect_uri, state = build_auth_params(request)
store_state_param(request, state)
permission_url = _new_session(shop).create_permission_url(
scopes, redirect_uri, state
)
return redirect(permission_url)
except ValueError as exception:
messages.error(request, str(exception))
return redirect(reverse("login"))
def get_sanitized_shop_param(request):
sanitized_shop_domain = shop_url.sanitize_shop_domain(
request.GET.get("shop", request.POST.get("shop"))
)
if not sanitized_shop_domain:
raise ValueError("Shop must match 'example.myshopify.com'")
return sanitized_shop_domain
def build_auth_params(request):
scopes = get_configured_scopes()
redirect_uri = build_redirect_uri()
state = build_state_param()
return scopes, redirect_uri, state
def get_configured_scopes():
return apps.get_app_config("shopify_app").SHOPIFY_API_SCOPES.split(",")
def build_redirect_uri():
app_url = apps.get_app_config("shopify_app").APP_URL
callback_path = reverse("callback")
return "https://{app_url}{callback_path}".format(
app_url=app_url, callback_path=callback_path
)
def build_state_param():
return binascii.b2a_hex(os.urandom(15)).decode("utf-8")
def store_state_param(request, state):
request.session["shopify_oauth_state_param"] = state
def _new_session(shop_url):
shopify_api_version = apps.get_app_config("shopify_app").SHOPIFY_API_VERSION
shopify_api_key = apps.get_app_config("shopify_app").SHOPIFY_API_KEY
shopify_api_secret = apps.get_app_config("shopify_app").SHOPIFY_API_SECRET
shopify.Session.setup(api_key=shopify_api_key, secret=shopify_api_secret)
return shopify.Session(shop_url, shopify_api_version)
# Callback helper methods
def validate_params(request, params):
validate_state_param(request, params.get("state"))
if not shopify.Session.validate_params(params): # Validates HMAC
raise ValueError("Invalid callback parameters")
def validate_state_param(request, state):
if request.session.get("shopify_oauth_state_param") != state:
raise ValueError("Anti-forgery state parameter does not match")
request.session.pop("shopify_oauth_state_param", None)
def exchange_code_for_access_token(request, shop):
session = _new_session(shop)
access_token = session.request_token(request.GET)
access_scopes = session.access_scopes
return access_token, access_scopes
def store_shop_information(access_token, access_scopes, partnerjam_token, shop):
shop_record = Shop.objects.get_or_create(shopify_domain=shop)[0]
shop_record.shopify_token = access_token
shop_record.access_scopes = access_scopes
shop_record.partnerjam_token = partnerjam_token
shop_record.save()
return shop_record
def build_callback_redirect_uri(request, params):
base = request.session.get("return_to", reverse("root_path"))
return "{base}?shop={shop}".format(base=base, shop=params.get("shop"))
# callback after_authenticate_jobs helper methods
def after_authenticate_jobs(shop, access_token):
create_uninstall_webhook(shop, access_token)
def create_uninstall_webhook(shop, access_token):
with shopify_session(shop, access_token):
app_url = apps.get_app_config("shopify_app").APP_URL
webhook = shopify.Webhook()
webhook.topic = "app/uninstalled"
webhook.address = "https://{host}/uninstall".format(host=app_url)
webhook.format = "json"
webhook.save()
def shopify_session(shopify_domain, access_token):
api_version = apps.get_app_config("shopify_app").SHOPIFY_API_VERSION
return shopify.Session.temp(shopify_domain, api_version, access_token)
def partnerjam_init(request):
partner_jam_token = request.GET.get("token")
app_store_url = settings.SHOPIFY_APP_STORE_URL # replace with your app store url
expiration = timezone.now() + timedelta(
days=60,
)
if not partner_jam_token:
raise Http404
response = HttpResponseRedirect(app_store_url)
response.set_cookie(
"partner_jam_token",
partner_jam_token,
expires=expiration,
httponly=True,
secure=True,
samesite="Lax",
)
return response