mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
refactor
This commit is contained in:
parent
f600083dee
commit
5bf9561984
@ -54,15 +54,17 @@ class WebhookView(CsrfExemptMixin, APIView):
|
|||||||
self.init(request, *args, **kwargs)
|
self.init(request, *args, **kwargs)
|
||||||
# get webhook definition
|
# get webhook definition
|
||||||
self.get_webhook(endpoint, *args, **kwargs)
|
self.get_webhook(endpoint, *args, **kwargs)
|
||||||
|
|
||||||
# check headers
|
# check headers
|
||||||
headers = request.headers
|
headers = request.headers
|
||||||
self.validate_token(headers)
|
|
||||||
|
|
||||||
# process data
|
|
||||||
try:
|
try:
|
||||||
payload = json.loads(request.body)
|
payload = json.loads(request.body)
|
||||||
except json.decoder.JSONDecodeError as error:
|
except json.decoder.JSONDecodeError as error:
|
||||||
raise NotAcceptable(error.msg)
|
raise NotAcceptable(error.msg)
|
||||||
|
|
||||||
|
# validate
|
||||||
|
self.validate_token(payload, headers)
|
||||||
|
# process data
|
||||||
self.save_data(payload, headers, request)
|
self.save_data(payload, headers, request)
|
||||||
self.process_payload(payload, headers, request)
|
self.process_payload(payload, headers, request)
|
||||||
|
|
||||||
@ -87,25 +89,25 @@ class WebhookView(CsrfExemptMixin, APIView):
|
|||||||
if self.webhook.token:
|
if self.webhook.token:
|
||||||
self.token = self.webhook.token
|
self.token = self.webhook.token
|
||||||
self.verify = VerificationMethod.TOKEN
|
self.verify = VerificationMethod.TOKEN
|
||||||
|
# TODO make a object-setting
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def validate_token(self, headers):
|
def validate_token(self, payload, headers):
|
||||||
token = headers.get(self.TOKEN_NAME, "")
|
token = headers.get(self.TOKEN_NAME, "")
|
||||||
|
|
||||||
# no token
|
# no token
|
||||||
if self.verify == VerificationMethod.NONE:
|
if self.verify == VerificationMethod.NONE:
|
||||||
return True
|
pass
|
||||||
|
|
||||||
# static token
|
# static token
|
||||||
elif self.verify == VerificationMethod.TOKEN:
|
elif self.verify == VerificationMethod.TOKEN:
|
||||||
if not compare_digest(token, self.token):
|
if not compare_digest(token, self.token):
|
||||||
raise PermissionDenied(self.MESSAGE_TOKEN_ERROR)
|
raise PermissionDenied(self.MESSAGE_TOKEN_ERROR)
|
||||||
return True
|
|
||||||
|
|
||||||
# hmac token
|
# hmac token
|
||||||
elif self.verify == VerificationMethod.HMAC:
|
elif self.verify == VerificationMethod.HMAC:
|
||||||
# TODO write check
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def save_data(self, payload, headers=None, request=None):
|
def save_data(self, payload, headers=None, request=None):
|
||||||
# TODO safe data
|
# TODO safe data
|
||||||
|
Loading…
Reference in New Issue
Block a user