import hmac
import hashlib

from rest_framework import permissions
from rest_framework import exceptions

from framework import sentry
from website import settings


class RequestComesFromMailgun(permissions.BasePermission):
    """Verify that request comes from Mailgun.
    Adapted here from conferences/message.py
    Signature comparisons as recomended from mailgun docs:
    https://documentation.mailgun.com/en/latest/user_manual.html#webhooks
    """
    def has_permission(self, request, view):
        if request.method != 'POST':
            raise exceptions.MethodNotAllowed(method=request.method)
        data = request.data
        if not data:
            raise exceptions.ParseError('Request body is empty')
        signature = hmac.new(
            key=settings.MAILGUN_API_KEY,
            msg='{}{}'.format(
                data['timestamp'],
                data['token'],
            ),
            digestmod=hashlib.sha256,
        ).hexdigest()
        if 'signature' not in data:
            error_message = 'Signature required in request body'
            sentry.log_message(error_message)
            raise exceptions.ParseError(error_message)
        if not hmac.compare_digest(unicode(signature), unicode(data['signature'])):
            raise exceptions.ParseError('Invalid signature')
        return True

import hmac
import hashlib

from rest_framework import permissions
from rest_framework import exceptions

from framework import sentry
from website import settings


class RequestComesFromMailgun(permissions.BasePermission):
    """Verify that request comes from Mailgun.
    Adapted here from conferences/message.py
    Signature comparisons as recomended from mailgun docs:
    https://documentation.mailgun.com/en/latest/user_manual.html#webhooks
    """
    def has_permission(self, request, view):
        if request.method != 'POST':
            raise exceptions.MethodNotAllowed(method=request.method)
        data = request.data
        if not data:
            raise exceptions.ParseError('Request body is empty')
        if not settings.MAILGUN_API_KEY:
            return False
        signature = hmac.new(
            key=settings.MAILGUN_API_KEY,
            msg='{}{}'.format(
                data['timestamp'],
                data['token'],
            ),
            digestmod=hashlib.sha256,
        ).hexdigest()
        if 'signature' not in data:
            error_message = 'Signature required in request body'
            sentry.log_message(error_message)
            raise exceptions.ParseError(error_message)
        if not hmac.compare_digest(unicode(signature), unicode(data['signature'])):
            raise exceptions.ParseError('Invalid signature')
        return True

Return False early if mailgun API key isn't set locally
Return False early if mailgun API key isn't set locally

  import hmac
  import hashlib
  
  from rest_framework import permissions
  from rest_framework import exceptions
  
  from framework import sentry
  from website import settings
  
  
  class RequestComesFromMailgun(permissions.BasePermission):
      """Verify that request comes from Mailgun.
      Adapted here from conferences/message.py
      Signature comparisons as recomended from mailgun docs:
      https://documentation.mailgun.com/en/latest/user_manual.html#webhooks
      """
      def has_permission(self, request, view):
          if request.method != 'POST':
              raise exceptions.MethodNotAllowed(method=request.method)
          data = request.data
          if not data:
              raise exceptions.ParseError('Request body is empty')
+         if not settings.MAILGUN_API_KEY:
+             return False
          signature = hmac.new(
              key=settings.MAILGUN_API_KEY,
              msg='{}{}'.format(
                  data['timestamp'],
                  data['token'],
              ),
              digestmod=hashlib.sha256,
          ).hexdigest()
          if 'signature' not in data:
              error_message = 'Signature required in request body'
              sentry.log_message(error_message)
              raise exceptions.ParseError(error_message)
          if not hmac.compare_digest(unicode(signature), unicode(data['signature'])):
              raise exceptions.ParseError('Invalid signature')
          return True
  
Return False early if mailgun API key isn't set locally
## Code Before:
import hmac
import hashlib

from rest_framework import permissions
from rest_framework import exceptions

from framework import sentry
from website import settings


class RequestComesFromMailgun(permissions.BasePermission):
    """Verify that request comes from Mailgun.
    Adapted here from conferences/message.py
    Signature comparisons as recomended from mailgun docs:
    https://documentation.mailgun.com/en/latest/user_manual.html#webhooks
    """
    def has_permission(self, request, view):
        if request.method != 'POST':
            raise exceptions.MethodNotAllowed(method=request.method)
        data = request.data
        if not data:
            raise exceptions.ParseError('Request body is empty')
        signature = hmac.new(
            key=settings.MAILGUN_API_KEY,
            msg='{}{}'.format(
                data['timestamp'],
                data['token'],
            ),
            digestmod=hashlib.sha256,
        ).hexdigest()
        if 'signature' not in data:
            error_message = 'Signature required in request body'
            sentry.log_message(error_message)
            raise exceptions.ParseError(error_message)
        if not hmac.compare_digest(unicode(signature), unicode(data['signature'])):
            raise exceptions.ParseError('Invalid signature')
        return True

## Instruction:
Return False early if mailgun API key isn't set locally
## Code After:
import hmac
import hashlib

from rest_framework import permissions
from rest_framework import exceptions

from framework import sentry
from website import settings


class RequestComesFromMailgun(permissions.BasePermission):
    """Verify that request comes from Mailgun.
    Adapted here from conferences/message.py
    Signature comparisons as recomended from mailgun docs:
    https://documentation.mailgun.com/en/latest/user_manual.html#webhooks
    """
    def has_permission(self, request, view):
        if request.method != 'POST':
            raise exceptions.MethodNotAllowed(method=request.method)
        data = request.data
        if not data:
            raise exceptions.ParseError('Request body is empty')
        if not settings.MAILGUN_API_KEY:
            return False
        signature = hmac.new(
            key=settings.MAILGUN_API_KEY,
            msg='{}{}'.format(
                data['timestamp'],
                data['token'],
            ),
            digestmod=hashlib.sha256,
        ).hexdigest()
        if 'signature' not in data:
            error_message = 'Signature required in request body'
            sentry.log_message(error_message)
            raise exceptions.ParseError(error_message)
        if not hmac.compare_digest(unicode(signature), unicode(data['signature'])):
            raise exceptions.ParseError('Invalid signature')
        return True
