2021-11-26 02:49:40 +00:00
|
|
|
from builtins import object
|
2016-12-02 17:50:21 +00:00
|
|
|
import hashlib
|
|
|
|
|
|
|
|
from django.conf import settings
|
2016-11-28 19:53:27 +00:00
|
|
|
|
2017-11-03 15:58:24 +00:00
|
|
|
# This is backwards compatibilty support for a custom function we wrote
|
|
|
|
# ourselves that is no longer necessary in modern Django.
|
|
|
|
from django.shortcuts import render as render_template_with_context
|
|
|
|
|
2023-09-07 12:59:23 +00:00
|
|
|
|
2016-12-02 20:07:35 +00:00
|
|
|
class ParameterValidator(object):
|
|
|
|
def __init__(self, given_hash_or_params, params_hash_key=None):
|
|
|
|
if params_hash_key is None:
|
|
|
|
self.given_hash = given_hash_or_params
|
|
|
|
else:
|
|
|
|
self.given_hash = given_hash_or_params.get(params_hash_key)
|
2021-11-26 01:41:27 +00:00
|
|
|
seed = getattr(settings, 'CONSERVANCY_SECRET_KEY', '').encode('utf-8')
|
2016-12-02 20:07:35 +00:00
|
|
|
self.hasher = hashlib.sha256(seed)
|
2023-09-07 12:59:23 +00:00
|
|
|
if isinstance(self.given_hash, str):
|
2016-12-02 20:07:35 +00:00
|
|
|
self.hash_type = type(self.given_hash)
|
|
|
|
else:
|
|
|
|
self.hash_type = type(self.hasher.hexdigest())
|
|
|
|
self.valid = None
|
|
|
|
if not (self.given_hash and seed):
|
|
|
|
self.fail()
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.valid = self.valid and None
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, exc_tb):
|
|
|
|
if exc_type is None:
|
|
|
|
self.check()
|
|
|
|
else:
|
|
|
|
self.fail()
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
self.valid = self.valid and None
|
|
|
|
self.hasher.update(data)
|
|
|
|
|
|
|
|
def check(self):
|
|
|
|
if self.valid or (self.valid is None):
|
|
|
|
self.valid = self.hash_type(self.hasher.hexdigest()) == self.given_hash
|
|
|
|
return self.valid
|
|
|
|
|
|
|
|
def fail(self):
|
|
|
|
self.valid = False
|