Skip to content

Commit

Permalink
feat: rename Webhook class to WebhookValidation
Browse files Browse the repository at this point in the history
  • Loading branch information
ilfa committed Nov 22, 2024
1 parent deac0a0 commit 8710516
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/rude-kids-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"fingerprint-pro-server-api-python-sdk": major
---

Rename `Webhook` class to `WebhookValidation`.
Right now, `Webhook` class points to the actual data model.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ This SDK provides utility method for verifying the HMAC signature of the incomin
```python
import os
from flask import Flask, request, jsonify
from fingerprint_pro_server_api_sdk import Webhook
from fingerprint_pro_server_api_sdk import WebhookValidation

app = Flask(__name__)

Expand All @@ -229,7 +229,7 @@ def webhook_handler():
data = request.get_data()

# Validate the webhook signature
is_valid = Webhook.is_valid_webhook_signature(header, data, secret)
is_valid = WebhookValidation.is_valid_webhook_signature(header, data, secret)
if not is_valid:
return jsonify({"message": "Webhook signature is invalid."}), 403

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import hashlib


class Webhook:
class WebhookValidation:
"""Manages work with webhooks."""
@staticmethod
def is_valid_hmac_signature(signature: str, data: bytes, secret: str) -> bool:
Expand All @@ -23,7 +23,7 @@ def is_valid_webhook_signature(header: str, data: bytes, secret: str) -> bool:
parts = signature.split('=')
if len(parts) == 2:
version, hash_value = parts
if version == "v1" and Webhook.is_valid_hmac_signature(hash_value, data, secret):
if version == "v1" and WebhookValidation.is_valid_hmac_signature(hash_value, data, secret):
return True

return False
Expand Down
4 changes: 2 additions & 2 deletions template/README.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ This SDK provides utility method for verifying the HMAC signature of the incomin
```python
import os
from flask import Flask, request, jsonify
from fingerprint_pro_server_api_sdk import Webhook
from fingerprint_pro_server_api_sdk import WebhookValidation

app = Flask(__name__)

Expand All @@ -235,7 +235,7 @@ def webhook_handler():
data = request.get_data()

# Validate the webhook signature
is_valid = Webhook.is_valid_webhook_signature(header, data, secret)
is_valid = WebhookValidation.is_valid_webhook_signature(header, data, secret)
if not is_valid:
return jsonify({"message": "Webhook signature is invalid."}), 403

Expand Down
16 changes: 8 additions & 8 deletions test/test_webhook_validation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from fingerprint_pro_server_api_sdk import Webhook
from fingerprint_pro_server_api_sdk import WebhookValidation


class TestWebhookValidation(unittest.TestCase):
Expand All @@ -10,31 +10,31 @@ class TestWebhookValidation(unittest.TestCase):
data = b"data"

def test_valid_header(self):
result = Webhook.is_valid_webhook_signature(self.valid_header, self.data, self.secret)
result = WebhookValidation.is_valid_webhook_signature(self.valid_header, self.data, self.secret)
self.assertTrue(result)

def test_invalid_header(self):
result = Webhook.is_valid_webhook_signature("v2=invalid", self.data, self.secret)
result = WebhookValidation.is_valid_webhook_signature("v2=invalid", self.data, self.secret)
self.assertFalse(result)

def test_header_without_version(self):
result = Webhook.is_valid_webhook_signature("invalid", self.data, self.secret)
result = WebhookValidation.is_valid_webhook_signature("invalid", self.data, self.secret)
self.assertFalse(result)

def test_header_with_unsupported_version(self):
result = Webhook.is_valid_webhook_signature(self.valid_header_v2, self.data, self.secret)
result = WebhookValidation.is_valid_webhook_signature(self.valid_header_v2, self.data, self.secret)
self.assertFalse(result)

def test_empty_header(self):
result = Webhook.is_valid_webhook_signature("", self.data, self.secret)
result = WebhookValidation.is_valid_webhook_signature("", self.data, self.secret)
self.assertFalse(result)

def test_empty_secret(self):
result = Webhook.is_valid_webhook_signature("invalid", self.data, "")
result = WebhookValidation.is_valid_webhook_signature("invalid", self.data, "")
self.assertFalse(result)

def test_empty_data(self):
result = Webhook.is_valid_webhook_signature(self.valid_header, b"", self.secret)
result = WebhookValidation.is_valid_webhook_signature(self.valid_header, b"", self.secret)
self.assertFalse(result)


Expand Down
4 changes: 2 additions & 2 deletions webhook_signature_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from fingerprint_pro_server_api_sdk import Webhook
from fingerprint_pro_server_api_sdk import WebhookValidation

header = "v1=1b2c16b75bd2a870c114153ccda5bcfca63314bc722fa160d690de133ccbb9db"
secret = "secret"
data = b"data"

is_valid = Webhook.is_valid_webhook_signature(header, data, secret)
is_valid = WebhookValidation.is_valid_webhook_signature(header, data, secret)

print("Webhook signature is correct!" if is_valid else "Webhook signature is incorrect!")

0 comments on commit 8710516

Please sign in to comment.