Remnawave_python-sdk/remnawave/controllers/webhooks.py
Artem 950a7ffab3
Add Pydantic models for nodes usage history, subscriptions, and user management
- Implemented models for nodes usage history including NodeInfoDto, GetUserAccessibleNodesResponse, and usage statistics.
- Created subscription-related models such as UserSubscription, SubscriptionInfoData, and various response DTOs for subscription information.
- Developed models for subscription settings, templates, and system statistics, enhancing the API's capability to manage and retrieve subscription configurations.
- Introduced user management models including CreateUserRequestDto, UpdateUserRequestDto, and various response DTOs for user actions.
- Added bulk actions for user updates and statistics tracking, improving the efficiency of user management operations.
- Established a base controller for handling API requests and responses, along with decorators for HTTP methods to streamline API interactions.
- Included utility functions for serialization using orjson for better performance with Pydantic models.
2025-07-03 12:22:16 +02:00

32 lines
No EOL
1,007 B
Python

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)