2020-07-20 19:59:28 +02:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
|
|
|
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.request import REQ, has_request_variables
|
|
|
|
from zerver.lib.response import json_success
|
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("Canarytoken")
|
2020-07-20 19:59:28 +02:00
|
|
|
@has_request_variables
|
|
|
|
def api_canarytoken_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-02-12 08:20:45 +01:00
|
|
|
message: Dict[str, Any] = REQ(argument_type="body"),
|
|
|
|
user_specified_topic: Optional[str] = REQ("topic", default=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 = (
|
|
|
|
f"**:alert: Canarytoken has been triggered on {message['time']}!**\n\n"
|
|
|
|
f"{message['memo']} \n\n"
|
|
|
|
f"[Manage this canarytoken]({message['manage_url']})"
|
|
|
|
)
|
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)
|
|
|
|
return json_success()
|