Skip to content

Commit d34c0b8

Browse files
authored
Merge pull request #31 from bookernath/jwt
Move callbacks to use new signed_payload_jwt
2 parents 0300504 + f4307a6 commit d34c0b8

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ Thumbs.db
4444
# Environment #
4545
###############
4646
*.env
47+
venv/

app.py

+23-17
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ def bad_request(e):
109109
return content, 400
110110

111111

112+
def jwt_error(e):
113+
print(f"JWT verification failed: {e}")
114+
return "Payload verification failed!", 401
115+
116+
112117
# Helper for template rendering
113118
def render(template, context):
114119
return flask.render_template(template, **context)
@@ -188,14 +193,15 @@ def auth_callback():
188193
@app.route('/bigcommerce/load')
189194
def load():
190195
# Decode and verify payload
191-
payload = flask.request.args['signed_payload']
192-
user_data = BigcommerceApi.oauth_verify_payload(payload, client_secret())
193-
if user_data is False:
194-
return "Payload verification failed!", 401
196+
payload = flask.request.args['signed_payload_jwt']
197+
try:
198+
user_data = BigcommerceApi.oauth_verify_payload_jwt(payload, client_secret(), client_id())
199+
except Exception as e:
200+
return jwt_error(e)
195201

196202
bc_user_id = user_data['user']['id']
197203
email = user_data['user']['email']
198-
store_hash = user_data['store_hash']
204+
store_hash = user_data['sub'].split('stores/')[1]
199205

200206
# Lookup store
201207
store = Store.query.filter_by(store_hash=store_hash).first()
@@ -224,13 +230,14 @@ def load():
224230
@app.route('/bigcommerce/uninstall')
225231
def uninstall():
226232
# Decode and verify payload
227-
payload = flask.request.args['signed_payload']
228-
user_data = BigcommerceApi.oauth_verify_payload(payload, client_secret())
229-
if user_data is False:
230-
return "Payload verification failed!", 401
233+
payload = flask.request.args['signed_payload_jwt']
234+
try:
235+
user_data = BigcommerceApi.oauth_verify_payload_jwt(payload, client_secret(), client_id())
236+
except Exception as e:
237+
return jwt_error(e)
231238

232239
# Lookup store
233-
store_hash = user_data['store_hash']
240+
store_hash = user_data['sub'].split('stores/')[1]
234241
store = Store.query.filter_by(store_hash=store_hash).first()
235242
if store is None:
236243
return "Store not found!", 401
@@ -250,14 +257,13 @@ def uninstall():
250257
# The Remove User Callback URL.
251258
@app.route('/bigcommerce/remove-user')
252259
def remove_user():
253-
# Decode and verify payload
254-
payload = flask.request.args['signed_payload']
255-
user_data = BigcommerceApi.oauth_verify_payload(payload, client_secret())
256-
if user_data is False:
257-
return "Payload verification failed!", 401
260+
payload = flask.request.args['signed_payload_jwt']
261+
try:
262+
user_data = BigcommerceApi.oauth_verify_payload_jwt(payload, client_secret(), client_id())
263+
except Exception as e:
264+
return jwt_error(e)
258265

259-
# Lookup store
260-
store_hash = user_data['store_hash']
266+
store_hash = user_data['sub'].split('stores/')[1]
261267
store = Store.query.filter_by(store_hash=store_hash).first()
262268
if store is None:
263269
return "Store not found!", 401

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Werkzeug==2.0.1
44
itsdangerous==2.0.1
55
requests==2.25.1
66
python-dotenv==0.17.1
7-
bigcommerce==0.22.0
7+
bigcommerce==0.22.2
88
gunicorn==20.1.0
99
psycopg2==2.8.6
1010
Flask-SQLAlchemy==2.5.1

0 commit comments

Comments
 (0)