简体   繁体   中英

adding an auth check to github webhooks using flask + python3

I've been trying to add auth to github but for some reason my hashes don't match,

here is my code:

@app.route('/update', methods=["POST"])
def update():
    assert request.method == "POST"
    signature = request.headers.get("X-Hub-Signature")
    if not signature or not signature.startswith("sha1="):
        abort(400, "X-Hub-Signature required")

    sha_name, sinature = signature.split("=")
    if sha_name != "sha1":
        abort(501)

    # Create local hash of payload
    digest = hmac.new(github_secret.encode(), msg=request.data, digestmod="sha1").hexdigest()


    # Verify signature
    if not hmac.compare_digest(signature, digest):
        abort(400, "Invalid signature")

The issue was for all future googlers, I used request.data which was None because the content-type header was "application/x-www-form-urlencoded" thx to this answer for explaining that so I had to use request.get_data() instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM