def normalize_before_encryption(unencrypted, content_type, content_encoding, enforce_text_only=False): """Normalize unencrypted prior to plugin encryption processing.""" if not unencrypted: raise s.SecretNoPayloadProvidedException() # Validate and normalize content-type. normalized_mime = mime_types.normalize_content_type(content_type) if not mime_types.is_supported(normalized_mime): raise s.SecretContentTypeNotSupportedException(content_type) # Process plain-text type. if normalized_mime in mime_types.PLAIN_TEXT: # normalize text to binary string unencrypted = unencrypted.encode('utf-8') # Process binary type. else: # payload has to be decoded if mime_types.is_base64_processing_needed(content_type, content_encoding): try: unencrypted = base64.b64decode(unencrypted) except TypeError: raise s.SecretPayloadDecodingError() elif mime_types.use_binary_content_as_is(content_type, content_encoding): unencrypted = unencrypted elif enforce_text_only: # For text-based protocols (such as the one-step secret POST), # only 'base64' encoding is possible/supported. raise s.SecretContentEncodingMustBeBase64() elif content_encoding: # Unsupported content-encoding request. raise s.SecretContentEncodingNotSupportedException( content_encoding) return unencrypted, normalized_mime
def normalize_before_encryption(unencrypted, content_type, content_encoding, enforce_text_only=False): """Normalize unencrypted prior to plugin encryption processing.""" if not unencrypted: raise s.SecretNoPayloadProvidedException() # Validate and normalize content-type. normalized_mime = mime_types.normalize_content_type(content_type) if not mime_types.is_supported(normalized_mime): raise s.SecretContentTypeNotSupportedException(content_type) # Process plain-text type. if normalized_mime in mime_types.PLAIN_TEXT: # normalize text to binary string unencrypted = unencrypted.encode('utf-8') # Process binary type. else: # payload has to be decoded if mime_types.is_base64_processing_needed(content_type, content_encoding): try: unencrypted = base64.b64decode(unencrypted) except TypeError: raise s.SecretPayloadDecodingError() elif mime_types.use_binary_content_as_is(content_type, content_encoding): unencrypted = unencrypted elif enforce_text_only: # For text-based protocols (such as the one-step secret POST), # only 'base64' encoding is possible/supported. raise s.SecretContentEncodingMustBeBase64() elif content_encoding: # Unsupported content-encoding request. raise s.SecretContentEncodingNotSupportedException( content_encoding ) return unencrypted, normalized_mime
def test_not_base64_needed_text(self): r = mime_types.is_base64_processing_needed('text/plain', 'base64') self.assertFalse(r)
def test_not_base64_needed_invalid_content_type(self): r = mime_types.is_base64_processing_needed('bababooey', 'base64') self.assertFalse(r)
def test_not_base64_needed_binary(self): r = mime_types.is_base64_processing_needed('application/octet-stream', None) self.assertFalse(r)
def test_is_base64_plus_needed(self): r = mime_types.is_base64_processing_needed('application/octet-stream', 'base64;q=0.5, ' 'gzip;q=0.6, compress') self.assertTrue(r)
def test_is_base64_needed(self): r = mime_types.is_base64_processing_needed('application/octet-stream', 'base64') self.assertTrue(r)
def test_is_base64_plus_needed(self): r = mime_types.is_base64_processing_needed( 'application/octet-stream', 'base64;q=0.5, ' 'gzip;q=0.6, compress') self.assertTrue(r)