ResourcePackUpdater/src/main.py

62 lines
1.9 KiB
Python
Raw Normal View History

2021-05-25 02:24:56 +00:00
from aiohttp import web
import os
import hmac
2021-06-20 03:04:41 +00:00
from pack import Pack
2021-05-25 02:24:56 +00:00
class App:
def __init__(self):
2021-06-21 09:26:11 +00:00
self.pack = Pack()
2021-06-20 03:04:41 +00:00
print("Done")
2021-05-25 02:24:56 +00:00
self.app = web.Application()
self.app.add_routes([web.post('/update', self.updatePack)])
self.app.router.add_static('/files', "./files")
2021-06-20 03:04:41 +00:00
print("Started")
2021-05-25 02:24:56 +00:00
web.run_app(self.app)
async def updatePack(self, request):
2021-06-20 03:04:41 +00:00
try:
json = await request.json()
text = await request.read()
except:
return web.Response(status=500, text="Invalid json or text")
2021-05-25 02:24:56 +00:00
header_signature = request.headers.get('X-Hub-Signature')
2021-05-25 03:23:27 +00:00
print("Hook recieved")
if "ref" in json and json["ref"] == 'refs/heads/release':
if (status := self.verify_signature(text, header_signature)) == 200:
self.pack.pull()
self.pack.collate()
self.pack.compress()
self.pack.hash()
2021-06-20 03:04:41 +00:00
self.pack.upload()
else:
status = 404
print(status)
return web.Response(status=status)
2021-05-25 02:24:56 +00:00
def verify_signature(self, request_data, header_signature):
# do not store your secret key in your code, pull from environment variable
2021-06-20 03:04:41 +00:00
secret_key = os.environ.get('WEBHOOK_SECRET')
2021-05-25 02:24:56 +00:00
if not header_signature:
return 404
# separate the signature from the sha1 indication
sha_name, signature = header_signature.split('=')
if sha_name != 'sha1':
return 501
# create a new hmac with the secret key and the request data
mac = hmac.new(secret_key.encode(), msg=request_data, digestmod='sha1')
# verify the digest matches the signature
if not hmac.compare_digest(mac.hexdigest(), signature):
return 404
return 200
if __name__ == '__main__':
2021-06-20 03:04:41 +00:00
app = App()
2021-05-25 02:24:56 +00:00
2021-06-01 12:22:53 +00:00