1-
21"""
32A simple Flask app that will act as a endpoint for a hook.
43
1110
1211"""
1312
13+
1414import os
1515import argparse
1616import sys
1717import json
1818import string
1919import time
20- from flask import Flask , request
20+ from flask import Flask , request , abort
2121import hashlib
2222import hmac
23- #from werkzeug.exceptions import HTTPException # Import HTTPException
23+ from werkzeug .exceptions import HTTPException # Add this import
24+
2425
2526def verify_signature (payload_body , secret_token , signature_header ):
2627 """
@@ -36,15 +37,15 @@ def verify_signature(payload_body, secret_token, signature_header):
3637 signature_header: header received from GitHub (x-hub-signature-256)
3738 """
3839 if not signature_header :
39- raise HTTPException ( status_code = 403 , detail = "x-hub-signature-256 header is missing!" )
40+ abort ( 403 , description = "x-hub-signature-256 header is missing!" )
4041 hash_object = hmac .new (secret_token .encode ('utf-8' ), msg = payload_body , digestmod = hashlib .sha256 )
4142 expected_signature = "sha256=" + hash_object .hexdigest ()
4243 if not hmac .compare_digest (expected_signature , signature_header ):
43- raise HTTPException ( status_code = 403 , detail = "Request signature didn't match signature on record" )
44+ abort ( 403 , description = "Request signature didn't match signature on record" )
4445 else :
45- print ("---------------------" )
46- print ("the webhook signature matches" )
47- print ("---------------------" )
46+ app . logger . debug ("-" * 21 )
47+ app . logger . debug ("the webhook signature matches" )
48+ app . logger . debug ("-" * 21 )
4849
4950
5051app = Flask (__name__ )
@@ -53,18 +54,22 @@ def verify_signature(payload_body, secret_token, signature_header):
5354@app .route ('/webhook' , methods = ['POST' ])
5455def slurphook ():
5556 if request .method == 'POST' :
56- print ("hook triggered" )
57- print ("---------------------" )
58- print ( "X-Hub-Signature-256:" , request . headers . get ( 'X-Hub-Signature-256' ))
57+ app . logger . debug ("hook triggered" )
58+ app . logger . debug ("-" * 21 )
59+
5960 signature_header = request .headers .get ('X-Hub-Signature-256' )
60- print ("---------------------" )
61- print ("Headers:" , request .headers ) # Print the headers
62- print ("---------------------" )
63- #print("Payload body:", request.data.decode('utf-8')) # Print the payload body
64- print ("JSON payload:\n \n " , json .dumps (request .json , indent = 4 )) # Print the JSON payload if available
65- verify_signature (request .data , args .hook_secret , signature_header )
66- #verify_signature(request.data.decode('utf-8'), "bangersandmash", signature_header)
67- return ('status' ,200 )
61+ app .logger .debug (f"X-Hub-Signature-256: { signature_header } " )
62+ app .logger .debug ("-" * 21 )
63+ app .logger .debug (f"Headers: { request .headers } " )
64+ app .logger .debug ("-" * 21 )
65+ app .logger .debug (f"JSON payload:\n \n { json .dumps (request .json , indent = 4 )} " )
66+
67+ if signature_header and args .hook_secret :
68+ verify_signature (request .data , args .hook_secret , signature_header )
69+ else :
70+ app .logger .debug ("Skipping signature verification - no signature header or secret provided" )
71+
72+ return ('status' , args .status_code )
6873
6974
7075if __name__ == '__main__' :
@@ -75,10 +80,18 @@ def slurphook():
7580 "--secret" ,
7681 action = "store" ,
7782 dest = "hook_secret" ,
78- default = False ,
83+ default = None ,
7984 help = "The secret for the webhook" ,
8085 )
8186
87+ parser .add_argument (
88+ "--status-code" ,
89+ action = "store" ,
90+ dest = "status_code" ,
91+ default = 200 ,
92+ help = "The response code the webhook will return" ,
93+ )
94+
8295 args = parser .parse_args ()
8396
8497 app .config ['DEBUG' ] = True
0 commit comments