2018-05-30 11:19:10 -04:00
|
|
|
import hashlib
|
2018-07-16 17:09:21 -04:00
|
|
|
import hmac
|
2018-07-31 16:17:24 -04:00
|
|
|
import json
|
|
|
|
|
2018-11-02 15:20:08 -04:00
|
|
|
import requests
|
2018-05-30 11:19:10 -04:00
|
|
|
from django_rq import job
|
2018-07-31 16:17:24 -04:00
|
|
|
from rest_framework.utils.encoders import JSONEncoder
|
2018-05-30 11:19:10 -04:00
|
|
|
|
2019-12-05 16:42:10 -05:00
|
|
|
from .choices import ObjectChangeActionChoices
|
2019-10-04 12:08:48 -04:00
|
|
|
from .constants import *
|
2018-05-30 11:19:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
@job('default')
|
2019-03-24 15:35:42 -04:00
|
|
|
def process_webhook(webhook, data, model_name, event, timestamp, username, request_id):
|
2018-05-30 11:19:10 -04:00
|
|
|
"""
|
|
|
|
Make a POST request to the defined Webhook
|
|
|
|
"""
|
|
|
|
payload = {
|
2019-12-05 16:42:10 -05:00
|
|
|
'event': dict(ObjectChangeActionChoices)[event].lower(),
|
2018-05-30 11:19:10 -04:00
|
|
|
'timestamp': timestamp,
|
2018-12-04 00:40:54 -05:00
|
|
|
'model': model_name,
|
2019-03-24 15:31:12 -04:00
|
|
|
'username': username,
|
2019-03-24 15:35:42 -04:00
|
|
|
'request_id': request_id,
|
2018-05-30 11:19:10 -04:00
|
|
|
'data': data
|
|
|
|
}
|
|
|
|
headers = {
|
|
|
|
'Content-Type': webhook.get_http_content_type_display(),
|
|
|
|
}
|
2019-10-13 03:09:58 -04:00
|
|
|
if webhook.additional_headers:
|
|
|
|
headers.update(webhook.additional_headers)
|
|
|
|
|
2018-05-30 11:19:10 -04:00
|
|
|
params = {
|
|
|
|
'method': 'POST',
|
|
|
|
'url': webhook.payload_url,
|
|
|
|
'headers': headers
|
|
|
|
}
|
|
|
|
|
|
|
|
if webhook.http_content_type == WEBHOOK_CT_JSON:
|
2018-07-31 16:17:24 -04:00
|
|
|
params.update({'data': json.dumps(payload, cls=JSONEncoder)})
|
2018-05-30 11:19:10 -04:00
|
|
|
elif webhook.http_content_type == WEBHOOK_CT_X_WWW_FORM_ENCODED:
|
|
|
|
params.update({'data': payload})
|
|
|
|
|
|
|
|
prepared_request = requests.Request(**params).prepare()
|
|
|
|
|
|
|
|
if webhook.secret != '':
|
2018-08-07 14:19:46 -04:00
|
|
|
# Sign the request with a hash of the secret key and its content.
|
|
|
|
hmac_prep = hmac.new(
|
|
|
|
key=webhook.secret.encode('utf8'),
|
|
|
|
msg=prepared_request.body.encode('utf8'),
|
|
|
|
digestmod=hashlib.sha512
|
|
|
|
)
|
2018-05-30 11:19:10 -04:00
|
|
|
prepared_request.headers['X-Hook-Signature'] = hmac_prep.hexdigest()
|
|
|
|
|
|
|
|
with requests.Session() as session:
|
|
|
|
session.verify = webhook.ssl_verification
|
2019-10-13 01:43:08 -04:00
|
|
|
if webhook.ca_file_path:
|
|
|
|
session.verify = webhook.ca_file_path
|
2018-05-30 11:19:10 -04:00
|
|
|
response = session.send(prepared_request)
|
|
|
|
|
|
|
|
if response.status_code >= 200 and response.status_code <= 299:
|
|
|
|
return 'Status {} returned, webhook successfully processed.'.format(response.status_code)
|
|
|
|
else:
|
|
|
|
raise requests.exceptions.RequestException(
|
2020-01-10 16:42:02 +00:00
|
|
|
"Status {} returned with content '{}', webhook FAILED to process.".format(response.status_code, response.content)
|
2018-05-30 11:19:10 -04:00
|
|
|
)
|