Example #1
0
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
Example #2
0
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
Example #3
0
def normalize_content_type(content_type):
    """Normalize the content type and validate that it is supported."""
    normalized_mime = mime_types.normalize_content_type(content_type)
    if not mime_types.is_supported(normalized_mime):
        raise s.SecretContentTypeNotSupportedException(content_type)
    return normalized_mime
Example #4
0
 def test_bogus_mime_normalization(self):
     mime = 'something/bogus'
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(r, 'something/bogus')
Example #5
0
 def test_binary_normalization(self):
     mime = 'application/octet-stream'
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(r, 'application/octet-stream')
Example #6
0
 def test_malformed_charset_in_plain_text_mime(self):
     mime = 'text/plain; charset is ISO-8859-1'
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(r, mime)
Example #7
0
 def _test_plain_text_mime_type(self, mime):
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(r, 'text/plain')
Example #8
0
 def test_bogus_mime_normalization(self):
     mime = 'something/bogus'
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(r, 'something/bogus')
Example #9
0
 def test_binary_normalization(self):
     mime = 'application/octet-stream'
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(r, 'application/octet-stream')
Example #10
0
 def test_malformed_charset_in_plain_text_mime(self):
     mime = 'text/plain; charset is ISO-8859-1'
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(r, mime)
Example #11
0
 def _test_plain_text_mime_type(self, mime):
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(r, 'text/plain')
Example #12
0
 def test_unsupported_charset_in_plain_text_mime(self):
     mime = 'text/plain; charset=ISO-8859-1'
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(mime, r)
Example #13
0
def normalize_content_type(content_type):
    """Normalize the content type and validate that it is supported."""
    normalized_mime = mime_types.normalize_content_type(content_type)
    if not mime_types.is_supported(normalized_mime):
        raise s.SecretContentTypeNotSupportedException(content_type)
    return normalized_mime
Example #14
0
 def test_unsupported_charset_in_plain_text_mime(self):
     mime = 'text/plain; charset=ISO-8859-1'
     r = mime_types.normalize_content_type(mime)
     self.assertEqual(mime, r)