Files
MathApp/payment/permissions.py
2026-08-11 16:43:12 +03:30

32 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from django.conf import settings
from rest_framework.permissions import BasePermission
class HasInternalServiceKey(BasePermission):
"""
برای endpointهایی مثل POST /api/payment/orders که طبق سند فقط قراره
سرور-به-سرور (از اپ Subscription) صدا زده بشن، نه از یک کلاینت دلخواه.
درخواست باید هدر X-Internal-Key رو برابر settings.INTERNAL_SERVICE_KEY
بفرسته؛ در غیر این صورت 403 برمی‌گرده.
"""
message = 'این endpoint فقط برای فراخوانی داخلی/سرور-به-سرور است.'
def has_permission(self, request, view):
provided = request.headers.get('X-Internal-Key', '')
return bool(provided) and provided == settings.INTERNAL_SERVICE_KEY
class HasWebhookSecret(BasePermission):
"""
برای POST /api/payment/webhook: باید از سمت درگاه پرداخت بیاد، نه یک
کلاینت دلخواه. تا زمان اتصال درگاه واقعی (که خودش امضای HMAC داره)،
یک shared-secret ساده از طریق هدر X-Webhook-Secret بررسی می‌شه.
"""
message = 'امضای وب‌هوک نامعتبر است.'
def has_permission(self, request, view):
provided = request.headers.get('X-Webhook-Secret', '')
return bool(provided) and provided == settings.PAYMENT_WEBHOOK_SECRET