Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prerelease fixes #91

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mode": "pre",
"mode": "exit",
"tag": "dev",
"initialVersions": {
"fingerprint-pro-server-api-python-sdk": "7.1.0"
Expand Down
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
2 changes: 1 addition & 1 deletion fingerprint_pro_server_api_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@
from fingerprint_pro_server_api_sdk.models.webhook_velocity import WebhookVelocity
from fingerprint_pro_server_api_sdk.models.webhook_virtual_machine import WebhookVirtualMachine
# import custom methods into sdk package
from fingerprint_pro_server_api_sdk.webhook import Webhook
from fingerprint_pro_server_api_sdk.webhook_validation import WebhookValidation
from fingerprint_pro_server_api_sdk.sealed import ApiClientDeserializer, DecryptionAlgorithm, DecryptionKey, \
UnsealError, UnsealAggregateError, unseal_event_response
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
2 changes: 1 addition & 1 deletion template/__init__package.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ from {{packageName}}.base_model import BaseModel
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}
{{/model}}{{/models}}
# import custom methods into sdk package
from {{packageName}}.webhook import Webhook
from {{packageName}}.webhook_validation import WebhookValidation
from {{packageName}}.sealed import ApiClientDeserializer, DecryptionAlgorithm, DecryptionKey, \
UnsealError, UnsealAggregateError, unseal_event_response
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!")
Loading