Skip to content

Commit

Permalink
fix: AMO auth timestamp and key issues (#6)
Browse files Browse the repository at this point in the history
fix AMO auth timestamp and key issues

Co-authored-by: Jarrad Whitaker <[email protected]>
  • Loading branch information
jelleschutter and akdor1154 authored Jan 27, 2022
1 parent 26e5ed5 commit 179c924
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
22 changes: 20 additions & 2 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
4 changes: 3 additions & 1 deletion publish_firefox.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from helper import readJson, generateToken
from helper import readJson, generateToken, checkResponse
import requests
import shutil

Expand All @@ -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']]

Expand All @@ -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:
Expand Down

0 comments on commit 179c924

Please sign in to comment.