def test_invalid_value(self): """ Initializing the Identity object with an invalid Base6č encoded payload should raise a ValueError. """ with self.assertRaises(ValueError): from_auth_header("invalid Base64")
def test_invalid_type(self): """ Initializing the Identity object with an invalid type that can’t be a Base64 encoded payload should raise a TypeError. """ with self.assertRaises(TypeError): from_auth_header(["not", "a", "string"])
def test_invalid_format(self): """ Initializing the Identity object with an valid Base64 encoded payload that does not contain the "identity" field. """ identity = self._identity() dict_ = identity._asdict() json = dumps(dict_) base64 = b64encode(json.encode()) with self.assertRaises(KeyError): from_auth_header(base64)
def test_valid(self): """ Initialize the Identity object with an encoded payload – a base64-encoded JSON. That would typically be a raw HTTP header content. """ expected_identity = self._identity() identity_data = expected_identity._asdict() identity_data_dicts = [ identity_data, # Test with extra data in the identity dict { **identity_data, **{ "extra_data": "value" } }, ] for identity_data in identity_data_dicts: with self.subTest(identity_data=identity_data): identity = {"identity": identity_data} json = dumps(identity) base64 = b64encode(json.encode()) try: actual_identity = from_auth_header(base64) self.assertEqual(expected_identity, actual_identity) except (TypeError, ValueError): self.fail() self.assertEqual(actual_identity.is_trusted_system, False)
def authentication_header_handler(apikey, required_scopes=None): try: identity = from_auth_header(apikey) except Exception as exc: login_failure_count.inc() logger.error(str(exc), exc_info=True) abort(401, f"Identity Error: {str(exc)}") return {"uid": identity}
def authentication_header_handler(apikey, required_scopes=None): try: identity = from_auth_header(apikey) validate(identity) except Exception as e: login_failure_count.inc() logger.debug("Failed to validate identity header value", exc_info=True) return None return {"uid": identity}