|
| 1 | +import binascii |
| 2 | +import json |
| 3 | +from base64 import b64decode |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | + |
| 7 | +from django.utils.timezone import utc |
| 8 | + |
| 9 | +from .base import AnymailBaseWebhookView |
| 10 | +from ..exceptions import ( |
| 11 | + AnymailInvalidAddress, |
| 12 | + AnymailWebhookValidationFailure, |
| 13 | + AnymailImproperlyInstalled, |
| 14 | + _LazyError, |
| 15 | + AnymailConfigurationError, |
| 16 | +) |
| 17 | +from ..inbound import AnymailInboundMessage |
| 18 | +from ..signals import ( |
| 19 | + inbound, |
| 20 | + tracking, |
| 21 | + AnymailInboundEvent, |
| 22 | + AnymailTrackingEvent, |
| 23 | + EventType, |
| 24 | + RejectReason, |
| 25 | +) |
| 26 | +from ..utils import parse_single_address, get_anymail_setting |
| 27 | + |
| 28 | +try: |
| 29 | + from cryptography.hazmat.primitives import serialization, hashes |
| 30 | + from cryptography.hazmat.backends import default_backend |
| 31 | + from cryptography.hazmat.primitives.asymmetric import padding |
| 32 | + from cryptography.exceptions import InvalidSignature |
| 33 | +except ImportError: |
| 34 | + # This module gets imported by anymail.urls, so don't complain about cryptography missing |
| 35 | + # unless one of the Postal webhook views is actually used and needs it |
| 36 | + error = _LazyError(AnymailImproperlyInstalled(missing_package='cryptography', backend='postal')) |
| 37 | + serialization = error |
| 38 | + hashes = error |
| 39 | + default_backend = error |
| 40 | + padding = error |
| 41 | + InvalidSignature = object |
| 42 | + |
| 43 | + |
| 44 | +class PostalBaseWebhookView(AnymailBaseWebhookView): |
| 45 | + """Base view class for Postal webhooks""" |
| 46 | + |
| 47 | + esp_name = "Postal" |
| 48 | + |
| 49 | + warn_if_no_basic_auth = False |
| 50 | + |
| 51 | + # These can be set from kwargs in View.as_view, or pulled from settings in init: |
| 52 | + webhook_key = None |
| 53 | + |
| 54 | + def __init__(self, **kwargs): |
| 55 | + self.webhook_key = get_anymail_setting('webhook_key', esp_name=self.esp_name, kwargs=kwargs, allow_bare=True) |
| 56 | + |
| 57 | + super().__init__(**kwargs) |
| 58 | + |
| 59 | + def validate_request(self, request): |
| 60 | + try: |
| 61 | + signature = request.META["HTTP_X_POSTAL_SIGNATURE"] |
| 62 | + except KeyError: |
| 63 | + raise AnymailWebhookValidationFailure("X-Postal-Signature header missing from webhook") |
| 64 | + |
| 65 | + public_key = serialization.load_pem_public_key( |
| 66 | + ('-----BEGIN PUBLIC KEY-----\n' + self.webhook_key + '\n-----END PUBLIC KEY-----').encode(), |
| 67 | + backend=default_backend() |
| 68 | + ) |
| 69 | + |
| 70 | + try: |
| 71 | + public_key.verify( |
| 72 | + b64decode(signature), |
| 73 | + request.body, |
| 74 | + padding.PKCS1v15(), |
| 75 | + hashes.SHA1() |
| 76 | + ) |
| 77 | + except (InvalidSignature, binascii.Error): |
| 78 | + raise AnymailWebhookValidationFailure( |
| 79 | + "Postal webhook called with incorrect signature") |
| 80 | + |
| 81 | + |
| 82 | +class PostalTrackingWebhookView(PostalBaseWebhookView): |
| 83 | + """Handler for Postal message, engagement, and generation event webhooks""" |
| 84 | + |
| 85 | + signal = tracking |
| 86 | + |
| 87 | + def parse_events(self, request): |
| 88 | + esp_event = json.loads(request.body.decode("utf-8")) |
| 89 | + |
| 90 | + if 'rcpt_to' in esp_event: |
| 91 | + raise AnymailConfigurationError( |
| 92 | + "You seem to have set Postal's *inbound* webhook " |
| 93 | + "to Anymail's Postal *tracking* webhook URL.") |
| 94 | + |
| 95 | + raw_timestamp = esp_event.get("timestamp") |
| 96 | + timestamp = ( |
| 97 | + datetime.fromtimestamp(int(raw_timestamp), tz=utc) |
| 98 | + if raw_timestamp |
| 99 | + else None |
| 100 | + ) |
| 101 | + |
| 102 | + payload = esp_event.get("payload", {}) |
| 103 | + |
| 104 | + status_types = { |
| 105 | + "Sent": EventType.DELIVERED, |
| 106 | + "SoftFail": EventType.DEFERRED, |
| 107 | + "HardFail": EventType.FAILED, |
| 108 | + "Held": EventType.QUEUED, |
| 109 | + } |
| 110 | + |
| 111 | + if "status" in payload: |
| 112 | + event_type = status_types.get(payload["status"], EventType.UNKNOWN) |
| 113 | + elif "bounce" in payload: |
| 114 | + event_type = EventType.BOUNCED |
| 115 | + elif "url" in payload: |
| 116 | + event_type = EventType.CLICKED |
| 117 | + else: |
| 118 | + event_type = EventType.UNKNOWN |
| 119 | + |
| 120 | + description = payload.get("details") |
| 121 | + mta_response = payload.get("output") |
| 122 | + |
| 123 | + # extract message-related fields |
| 124 | + message = payload.get("message") or payload.get("original_message", {}) |
| 125 | + message_id = message.get("id") |
| 126 | + tag = message.get("tag") |
| 127 | + recipient = None |
| 128 | + message_to = message.get("to") |
| 129 | + if message_to is not None: |
| 130 | + try: |
| 131 | + recipient = parse_single_address(message_to).addr_spec |
| 132 | + except AnymailInvalidAddress: |
| 133 | + pass |
| 134 | + |
| 135 | + if message.get("direction") == "incoming": |
| 136 | + # Let's ignore tracking events about an inbound emails. |
| 137 | + # This happens when an inbound email could not be forwarded. |
| 138 | + # The email didn't originate from Anymail, so the user can't do much about it. |
| 139 | + # It is part of normal Postal operation, not a configuration error. |
| 140 | + return [] |
| 141 | + |
| 142 | + # only for MessageLinkClicked |
| 143 | + click_url = payload.get("url") |
| 144 | + user_agent = payload.get("user_agent") |
| 145 | + |
| 146 | + event = AnymailTrackingEvent( |
| 147 | + event_type=event_type, |
| 148 | + timestamp=timestamp, |
| 149 | + event_id=esp_event.get('uuid'), |
| 150 | + esp_event=esp_event, |
| 151 | + click_url=click_url, |
| 152 | + description=description, |
| 153 | + message_id=message_id, |
| 154 | + metadata=None, |
| 155 | + mta_response=mta_response, |
| 156 | + recipient=recipient, |
| 157 | + reject_reason=RejectReason.BOUNCED if event_type == EventType.BOUNCED else None, |
| 158 | + tags=[tag], |
| 159 | + user_agent=user_agent, |
| 160 | + ) |
| 161 | + |
| 162 | + return [event] |
| 163 | + |
| 164 | + |
| 165 | +class PostalInboundWebhookView(PostalBaseWebhookView): |
| 166 | + """Handler for Postal inbound relay webhook""" |
| 167 | + |
| 168 | + signal = inbound |
| 169 | + |
| 170 | + def parse_events(self, request): |
| 171 | + esp_event = json.loads(request.body.decode("utf-8")) |
| 172 | + |
| 173 | + if 'status' in esp_event: |
| 174 | + raise AnymailConfigurationError( |
| 175 | + "You seem to have set Postal's *tracking* webhook " |
| 176 | + "to Anymail's Postal *inbound* webhook URL.") |
| 177 | + |
| 178 | + raw_mime = esp_event["message"] |
| 179 | + if esp_event.get("base64") is True: |
| 180 | + raw_mime = b64decode(esp_event["message"]).decode("utf-8") |
| 181 | + message = AnymailInboundMessage.parse_raw_mime(raw_mime) |
| 182 | + |
| 183 | + message.envelope_sender = esp_event.get('mail_from', None) |
| 184 | + message.envelope_recipient = esp_event.get('rcpt_to', None) |
| 185 | + |
| 186 | + event = AnymailInboundEvent( |
| 187 | + event_type=EventType.INBOUND, |
| 188 | + timestamp=None, |
| 189 | + event_id=esp_event.get("id"), |
| 190 | + esp_event=esp_event, |
| 191 | + message=message, |
| 192 | + ) |
| 193 | + |
| 194 | + return [event] |
0 commit comments