feat: Update version to 1.0.7 in pyproject.toml; add WebhookUtility for webhook validation

This commit is contained in:
Artem 2025-04-27 00:23:57 +02:00
parent 167eca47de
commit 15ae9e6a20
No known key found for this signature in database
GPG key ID: 833485276B7902CE
4 changed files with 37 additions and 1 deletions

View file

@ -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"}

View file

@ -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
)

View file

@ -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"
]

View file

@ -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)