Created
October 8, 2021 16:31
-
-
Save lanbugs/1b61a3ee72d22511653880eeb5bdeca9 to your computer and use it in GitHub Desktop.
Gitea Webhook Auth Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from flask import Flask, request, abort | |
from pprint import pprint | |
import json | |
import hmac | |
app = Flask(__name__) | |
@app.route("/", methods=['POST']) | |
def webhook(): | |
if request.method != 'POST': | |
abort(501) | |
secret_key = b"123" | |
header_signature = request.headers.get("X-Hub-Signature-256") | |
if header_signature is None: | |
abort(403) | |
sha_name, signature = header_signature.split("=") | |
if sha_name != 'sha256': | |
abort(501) | |
mac = hmac.new(secret_key, msg=request.data, digestmod='sha256') | |
if not str(mac.hexdigest()) == str(signature): | |
abort(403) | |
pprint(json.loads(request.data)) | |
return {"result": "ok"} | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment