Skip to content

Commit 0978422

Browse files
committed
WIP Add signature verification function
1 parent ce3cf07 commit 0978422

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

frameioclient/lib/utils.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22
import sys
3+
import hmac
34
import xxhash
5+
import hashlib
46

57
KB = 1024
68
MB = KB * KB
@@ -125,6 +127,30 @@ def format_headers(token, version):
125127
'x-frameio-client': 'python/{}'.format(version)
126128
}
127129

130+
@staticmethod
131+
def verify_signature(curr_time: float, req_time: float, signature: str, body: str, secret: str):
132+
"""
133+
Verify webhook/custom action signature
134+
135+
:Args:
136+
curr_time (float): Current epoch time
137+
req_time (float): Request epoch time
138+
signature (str): Signature provided by the frame.io API for the given request
139+
body (str): Webhook body from the received POST
140+
secret (str): The secret for this custom action/webhook that you saved when you first created it
141+
"""
142+
if int(curr_time) - int(req_time) < 500:
143+
message = 'v0:{}:{}'.format(req_time, body)
144+
calculated_signature = 'v0={}'.format(hmac.new(
145+
bytes(secret, 'latin-1'),
146+
msg=bytes(message, 'latin-1'),
147+
digestmod=hashlib.sha256).hexdigest())
148+
if calculated_signature == signature:
149+
return True
150+
else:
151+
return False
152+
else:
153+
return False
128154

129155
class PaginatedResponse(object):
130156
def __init__(self, results=[], limit=None, page_size=0, total=0,

tests/integration.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import json
44
import time
55
import socket
6-
import platform
7-
import mimetypes
86
import shutil
97
import requests
8+
import platform
109

1110
from math import ceil
1211
from pprint import pprint, pformat

tests/test_signature_verification.py

Whitespace-only changes.

0 commit comments

Comments
 (0)