def get(cls, value): # DB does not contain key but the key_hash. value_hash = hash_utils.hash(value) for model_object in ApiKeyDB.objects(key_hash=value_hash): return model_object raise ApiKeyNotFoundError('ApiKey with key_hash=%s not found.' % value_hash)
def get(cls, value): # DB does not contain key but the key_hash. value_hash = hash_utils.hash(value) result = cls.query(key_hash=value_hash).first() if not result: raise ApiKeyNotFoundError('ApiKey with key_hash=%s not found.' % value_hash) return result
def test_hash_uniqueness(self): count = 10000 api_keys = [auth_utils.generate_api_key() for _ in range(count)] hashes = set([hash_utils.hash(api_key) for api_key in api_keys]) self.assertEqual(len(hashes), count, 'Expected all unique hashes.')
def test_hash_repeatability(self): api_key = auth_utils.generate_api_key() hash1 = hash_utils.hash(api_key) hash2 = hash_utils.hash(api_key) self.assertEqual(hash1, hash2, 'Expected a repeated hash.')
def generate_api_key_and_hash(): api_key = generate_api_key() api_key_hash = hash_utils.hash(api_key) return api_key, api_key_hash