2020-01-23 15:04:40 -05:00
|
|
|
import hashlib
|
|
|
|
import hmac
|
2018-05-30 11:19:10 -04:00
|
|
|
|
2018-07-16 17:09:21 -04:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2020-01-24 12:00:24 -05:00
|
|
|
from django.utils import timezone
|
2020-04-24 15:43:58 -04:00
|
|
|
from django_rq import get_queue
|
2018-05-30 11:19:10 -04:00
|
|
|
|
2018-11-02 15:20:08 -04:00
|
|
|
from extras.models import Webhook
|
2018-07-30 14:23:49 -04:00
|
|
|
from utilities.api import get_serializer_for_model
|
2019-12-05 16:30:15 -05:00
|
|
|
from .choices import *
|
2020-03-16 11:58:35 -04:00
|
|
|
from .utils import FeatureQuery
|
2018-07-30 14:23:49 -04:00
|
|
|
|
|
|
|
|
2020-01-23 15:04:40 -05:00
|
|
|
def generate_signature(request_body, secret):
|
|
|
|
"""
|
|
|
|
Return a cryptographic signature that can be used to verify the authenticity of webhook data.
|
|
|
|
"""
|
|
|
|
hmac_prep = hmac.new(
|
|
|
|
key=secret.encode('utf8'),
|
2020-04-29 00:06:26 -04:00
|
|
|
msg=request_body,
|
2020-01-23 15:04:40 -05:00
|
|
|
digestmod=hashlib.sha512
|
|
|
|
)
|
|
|
|
return hmac_prep.hexdigest()
|
|
|
|
|
|
|
|
|
2019-03-24 15:35:42 -04:00
|
|
|
def enqueue_webhooks(instance, user, request_id, action):
|
2018-07-30 14:23:49 -04:00
|
|
|
"""
|
|
|
|
Find Webhook(s) assigned to this instance + action and enqueue them
|
|
|
|
to be processed
|
|
|
|
"""
|
2020-01-15 16:18:47 -05:00
|
|
|
obj_type = ContentType.objects.get_for_model(instance.__class__)
|
|
|
|
|
2020-03-16 11:58:35 -04:00
|
|
|
webhook_models = ContentType.objects.filter(FeatureQuery('webhooks').get_query())
|
2020-01-15 16:18:47 -05:00
|
|
|
if obj_type not in webhook_models:
|
2018-07-30 16:33:37 -04:00
|
|
|
return
|
|
|
|
|
2018-08-07 15:41:31 -04:00
|
|
|
# Retrieve any applicable Webhooks
|
|
|
|
action_flag = {
|
2019-12-05 16:30:15 -05:00
|
|
|
ObjectChangeActionChoices.ACTION_CREATE: 'type_create',
|
|
|
|
ObjectChangeActionChoices.ACTION_UPDATE: 'type_update',
|
|
|
|
ObjectChangeActionChoices.ACTION_DELETE: 'type_delete',
|
2018-08-07 15:41:31 -04:00
|
|
|
}[action]
|
|
|
|
webhooks = Webhook.objects.filter(obj_type=obj_type, enabled=True, **{action_flag: True})
|
|
|
|
|
|
|
|
if webhooks.exists():
|
2018-07-30 14:23:49 -04:00
|
|
|
# Get the Model's API serializer class and serialize the object
|
|
|
|
serializer_class = get_serializer_for_model(instance.__class__)
|
|
|
|
serializer_context = {
|
|
|
|
'request': None,
|
|
|
|
}
|
|
|
|
serializer = serializer_class(instance, context=serializer_context)
|
|
|
|
|
2020-04-24 15:43:58 -04:00
|
|
|
# Enqueue the webhooks
|
2018-07-30 14:23:49 -04:00
|
|
|
webhook_queue = get_queue('default')
|
|
|
|
for webhook in webhooks:
|
|
|
|
webhook_queue.enqueue(
|
|
|
|
"extras.webhooks_worker.process_webhook",
|
|
|
|
webhook,
|
|
|
|
serializer.data,
|
2018-12-04 00:40:54 -05:00
|
|
|
instance._meta.model_name,
|
2018-07-30 14:23:49 -04:00
|
|
|
action,
|
2020-01-24 12:00:24 -05:00
|
|
|
str(timezone.now()),
|
2019-03-24 15:35:42 -04:00
|
|
|
user.username,
|
|
|
|
request_id
|
2018-07-30 14:23:49 -04:00
|
|
|
)
|