From 15ae9e6a20d0a33da24ad960720e2a7193ef36b7 Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 27 Apr 2025 00:23:57 +0200 Subject: [PATCH] feat: Update version to 1.0.7 in pyproject.toml; add WebhookUtility for webhook validation --- pyproject.toml | 2 +- remnawave_api/__init__.py | 2 ++ remnawave_api/controllers/__init__.py | 2 ++ remnawave_api/controllers/webhooks.py | 32 +++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 remnawave_api/controllers/webhooks.py diff --git a/pyproject.toml b/pyproject.toml index 6de904f..aff2ec7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "remnawave-api" -version = "1.0.6" +version = "1.0.7" description = "A Python SDK for interacting with the Remnawave API." authors = [ {name = "Artem",email = "sm1ky@forestsnet.com"} diff --git a/remnawave_api/__init__.py b/remnawave_api/__init__.py index 01a9c0c..b3745fa 100644 --- a/remnawave_api/__init__.py +++ b/remnawave_api/__init__.py @@ -21,6 +21,8 @@ from remnawave_api.controllers import ( UsersController, UsersStatsController, XrayConfigController, + WebhookUtility, + # WebhookUtility is not a controller, but it's included in the controllers module for convenience ) diff --git a/remnawave_api/controllers/__init__.py b/remnawave_api/controllers/__init__.py index 2c2d41b..41e49fe 100644 --- a/remnawave_api/controllers/__init__.py +++ b/remnawave_api/controllers/__init__.py @@ -15,6 +15,7 @@ from .users import UsersController from .users_bulk_actions import UsersBulkActionsController from .users_stats import UsersStatsController from .xray_config import XrayConfigController +from .webhooks import WebhookUtility __all__ = [ "APITokensManagementController", @@ -34,4 +35,5 @@ __all__ = [ "UsersBulkActionsController", "UsersStatsController", "XrayConfigController", + "WebhookUtility" ] diff --git a/remnawave_api/controllers/webhooks.py b/remnawave_api/controllers/webhooks.py new file mode 100644 index 0000000..493ee68 --- /dev/null +++ b/remnawave_api/controllers/webhooks.py @@ -0,0 +1,32 @@ +import hmac +import hashlib +import json +from typing import Union + +class WebhookUtility: + @staticmethod + def validate_webhook( + body: Union[str, dict], + signature: str, + webhook_secret: str + ) -> bool: + """ + Validates the webhook's authenticity using HMAC SHA-256. + + :param body: The webhook request body (either a JSON string or a parsed dictionary). + :param signature: The signature received from the server. + :param webhook_secret: The secret key used to compute the HMAC. + :return: True if the signature matches, otherwise False. + """ + if isinstance(body, str): + original_body = body + else: + original_body = json.dumps(body, separators=(',', ':')) + + computed_signature = hmac.new( + webhook_secret.encode('utf-8'), + original_body.encode('utf-8'), + hashlib.sha256 + ).hexdigest() + + return hmac.compare_digest(computed_signature, signature) \ No newline at end of file