2020-07-20 19:59:28 +02:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2020-07-20 19:59:28 +02:00
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_string
|
|
|
|
from zerver.lib.webhooks.common import OptionalUserSpecifiedTopicStr, check_send_webhook_message
|
2020-07-20 19:59:28 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
|
2021-12-16 21:34:08 +01:00
|
|
|
@webhook_view("Canarytokens")
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2020-07-20 19:59:28 +02:00
|
|
|
def api_canarytoken_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
message: JsonBodyPayload[WildValue],
|
2023-08-12 09:34:31 +02:00
|
|
|
user_specified_topic: OptionalUserSpecifiedTopicStr = None,
|
2020-07-20 19:59:28 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
"""
|
|
|
|
Construct a response to a webhook event from a Thinkst canarytoken from
|
|
|
|
canarytokens.org. Canarytokens from Thinkst's paid product have a different
|
|
|
|
schema and should use the "thinkst" integration. See linked documentation
|
|
|
|
below for a schema:
|
|
|
|
|
|
|
|
https://help.canary.tools/hc/en-gb/articles/360002426577-How-do-I-configure-notifications-for-a-Generic-Webhook-
|
|
|
|
"""
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = "canarytoken alert"
|
2021-02-12 08:19:30 +01:00
|
|
|
body = (
|
2021-12-17 07:03:22 +01:00
|
|
|
f"**:alert: Canarytoken has been triggered on {message['time'].tame(check_string)}!**\n\n"
|
|
|
|
f"{message['memo'].tame(check_string)} \n\n"
|
|
|
|
f"[Manage this canarytoken]({message['manage_url'].tame(check_string)})"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-07-20 19:59:28 +02:00
|
|
|
|
|
|
|
if user_specified_topic:
|
|
|
|
topic = user_specified_topic
|
|
|
|
|
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|