From 179c9246e86349e3b93c238e20b61c79b81899a4 Mon Sep 17 00:00:00 2001 From: Jelle Schutter Date: Thu, 27 Jan 2022 19:17:57 +0100 Subject: [PATCH] fix: AMO auth timestamp and key issues (#6) fix AMO auth timestamp and key issues Co-authored-by: Jarrad Whitaker --- helper.py | 22 ++++++++++++++++++++-- publish_firefox.py | 4 +++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/helper.py b/helper.py index c0051ff..7ad5b1d 100644 --- a/helper.py +++ b/helper.py @@ -4,7 +4,10 @@ from datetime import datetime from jwcrypto.jwt import JWT from jwcrypto.jwk import JWK +import base64 +from math import floor import uuid +import sys def rmDir(path): if os.path.exists(path): @@ -30,14 +33,29 @@ def saveJson(path, data): json.dump(data, f, indent=3) def generateToken(issuer, key): - timestamp = round(datetime.utcnow().timestamp()) + timestamp = floor(datetime.utcnow().timestamp()) data = { 'iss': issuer, 'iat': timestamp, 'exp': timestamp + 300, 'jti': str(uuid.uuid4()) } - jwk = JWK(k=key, kty='oct') + key_b64 = ( + base64 + .urlsafe_b64encode(key.encode('ascii')) + .decode() + ) + jwk = JWK( + k=key_b64, + kty='oct' + ) token = JWT(header={'alg': 'HS256'}, claims=data) token.make_signed_token(jwk) return token.serialize() + +def checkResponse(response): + try: + response.raise_for_status() + except: + print(response.json(), file=sys.stderr) + raise diff --git a/publish_firefox.py b/publish_firefox.py index 801cc98..e76644e 100644 --- a/publish_firefox.py +++ b/publish_firefox.py @@ -1,5 +1,5 @@ import os -from helper import readJson, generateToken +from helper import readJson, generateToken, checkResponse import requests import shutil @@ -25,6 +25,7 @@ # check if version exists print('Checking if version exists...') r = requests.get(f'https://addons.mozilla.org/api/v5/addons/addon/{uuid}/versions/', headers={'Authorization': f'JWT {token}'}) +checkResponse(r) version_result = r.json() previous_versions = [v['version'] for v in version_result['results']] @@ -35,6 +36,7 @@ print('Uploading new version...') files = {'upload': open(tmp_dir + '/firefox_addon.zip', 'rb')} r = requests.post(f'https://addons.mozilla.org/api/v5/addons/{uuid}/versions/{version}', headers={'Authorization': f'JWT {token}'}, files=files) + checkResponse(r) print('Uploaded new version:') print(r.text) else: