-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebhooks.py
221 lines (175 loc) · 7.53 KB
/
webhooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
from __future__ import annotations
import hmac
import json
import math
import base64
import hashlib
from typing import cast
from datetime import datetime, timezone, timedelta
from ..types import WebhookEvent
from .._types import (
HeadersLike,
)
from .._utils import (
get_required_header,
)
from .._models import construct_type
from .._resource import SyncAPIResource, AsyncAPIResource
__all__ = ["Webhooks", "AsyncWebhooks"]
class Webhooks(SyncAPIResource):
def unwrap(
self,
payload: str | bytes,
headers: HeadersLike,
*,
secret: str | None = None,
) -> WebhookEvent:
"""Validates that the given payload was sent by Finch and parses the payload."""
self.verify_signature(payload=payload, headers=headers, secret=secret)
return cast(
WebhookEvent,
construct_type(
value=json.loads(payload),
type_=WebhookEvent, # type: ignore[arg-type]
),
)
def verify_signature(
self,
payload: str | bytes,
headers: HeadersLike,
*,
secret: str | None = None,
) -> None:
"""Validates whether or not the webhook payload was sent by Finch.
An error will be raised if the webhook payload was not sent by Finch.
"""
if secret is None:
secret = self._client.webhook_secret
if secret is None:
raise ValueError(
"The webhook secret must either be set using the env var, FINCH_WEBHOOK_SECRET, on the client class, Finch(webhook_secret='123'), or passed to this function"
)
try:
parsedSecret = base64.b64decode(secret)
except Exception as err:
raise ValueError("Bad secret") from err
msg_id = get_required_header(headers, "finch-event-id")
msg_timestamp = get_required_header(headers, "finch-timestamp")
# validate the timestamp
webhook_tolerance = timedelta(minutes=5)
now = datetime.now(tz=timezone.utc)
try:
timestamp = datetime.fromtimestamp(float(msg_timestamp), tz=timezone.utc)
except Exception as err:
raise ValueError("Invalid timestamp header: " + msg_timestamp + ". Could not convert to timestamp") from err
# too old
if timestamp < (now - webhook_tolerance):
raise ValueError("Webhook timestamp is too old")
# too new
if timestamp > (now + webhook_tolerance):
raise ValueError("Webhook timestamp is too new")
# create the signature
body = payload.decode("utf-8") if isinstance(payload, bytes) else payload
if not isinstance(body, str): # pyright: ignore[reportUnnecessaryIsInstance]
raise ValueError(
"Webhook body should be a string of JSON (or bytes which can be decoded to a utf-8 string), not a parsed dictionary."
)
timestamp_str = str(math.floor(timestamp.replace(tzinfo=timezone.utc).timestamp()))
to_sign = f"{msg_id}.{timestamp_str}.{body}".encode()
expected_signature = hmac.new(parsedSecret, to_sign, hashlib.sha256).digest()
msg_signature = get_required_header(headers, "finch-signature")
# Signature header can contain multiple signatures delimited by spaces
passed_sigs = msg_signature.split(" ")
for versioned_sig in passed_sigs:
values = versioned_sig.split(",")
if len(values) != 2:
# signature is not formatted like {version},{signature}
continue
(version, signature) = values
# Only verify prefix v1
if version != "v1":
continue
sig_bytes = base64.b64decode(signature)
if hmac.compare_digest(expected_signature, sig_bytes):
# valid!
return None
raise ValueError("None of the given webhook signatures match the expected signature")
class AsyncWebhooks(AsyncAPIResource):
def unwrap(
self,
payload: str | bytes,
headers: HeadersLike,
*,
secret: str | None = None,
) -> WebhookEvent:
"""Validates that the given payload was sent by Finch and parses the payload."""
self.verify_signature(payload=payload, headers=headers, secret=secret)
return cast(
WebhookEvent,
construct_type(
value=json.loads(payload),
type_=WebhookEvent, # type: ignore[arg-type]
),
)
def verify_signature(
self,
payload: str | bytes,
headers: HeadersLike,
*,
secret: str | None = None,
) -> None:
"""Validates whether or not the webhook payload was sent by Finch.
An error will be raised if the webhook payload was not sent by Finch.
"""
if secret is None:
secret = self._client.webhook_secret
if secret is None:
raise ValueError(
"The webhook secret must either be set using the env var, FINCH_WEBHOOK_SECRET, on the client class, Finch(webhook_secret='123'), or passed to this function"
)
try:
parsedSecret = base64.b64decode(secret)
except Exception as err:
raise ValueError("Bad secret") from err
msg_id = get_required_header(headers, "finch-event-id")
msg_timestamp = get_required_header(headers, "finch-timestamp")
# validate the timestamp
webhook_tolerance = timedelta(minutes=5)
now = datetime.now(tz=timezone.utc)
try:
timestamp = datetime.fromtimestamp(float(msg_timestamp), tz=timezone.utc)
except Exception as err:
raise ValueError("Invalid timestamp header: " + msg_timestamp + ". Could not convert to timestamp") from err
# too old
if timestamp < (now - webhook_tolerance):
raise ValueError("Webhook timestamp is too old")
# too new
if timestamp > (now + webhook_tolerance):
raise ValueError("Webhook timestamp is too new")
# create the signature
body = payload.decode("utf-8") if isinstance(payload, bytes) else payload
if not isinstance(body, str): # pyright: ignore[reportUnnecessaryIsInstance]
raise ValueError(
"Webhook body should be a string of JSON (or bytes which can be decoded to a utf-8 string), not a parsed dictionary."
)
timestamp_str = str(math.floor(timestamp.replace(tzinfo=timezone.utc).timestamp()))
to_sign = f"{msg_id}.{timestamp_str}.{body}".encode()
expected_signature = hmac.new(parsedSecret, to_sign, hashlib.sha256).digest()
msg_signature = get_required_header(headers, "finch-signature")
# Signature header can contain multiple signatures delimited by spaces
passed_sigs = msg_signature.split(" ")
for versioned_sig in passed_sigs:
values = versioned_sig.split(",")
if len(values) != 2:
# signature is not formatted like {version},{signature}
continue
(version, signature) = values
# Only verify prefix v1
if version != "v1":
continue
sig_bytes = base64.b64decode(signature)
if hmac.compare_digest(expected_signature, sig_bytes):
# valid!
return None
raise ValueError("None of the given webhook signatures match the expected signature")