1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
"""
"""
# Created on 2014.04.26
#
# Author: Giovanni Cannata
#
# Copyright 2015 Giovanni Cannata
#
# This file is part of ldap3.
#
# ldap3 is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ldap3 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ldap3 in the COPYING and COPYING.LESSER files.
# If not, see <http://www.gnu.org/licenses/>.
from base64 import b64encode, b64decode
import datetime
from .. import SEQUENCE_TYPES
from ..utils.ciDict import CaseInsensitiveDict
from ..core.exceptions import LDAPDefinitionError
def escape_filter_chars(text):
""" Escape chars mentioned in RFC4515. """
output = text.replace('\\', r'\5c')
output = output.replace(r'*', r'\2a')
output = output.replace(r'(', r'\28')
output = output.replace(r')', r'\29')
output = output.replace('\x00', r'\00')
return output
def escape_bytes(bytes_value):
if bytes_value:
if str != bytes: # Python 3
if isinstance(bytes_value, str):
bytes_value = bytearray(bytes_value, encoding='utf-8')
escaped = '\\'.join([('%02x' % int(b)) for b in bytes_value])
else: # Python 2
if isinstance(bytes_value, unicode):
bytes_value = bytes_value.encode('utf-8')
escaped = '\\'.join([('%02x' % ord(b)) for b in bytes_value])
else:
escaped = ''
return ('\\' + escaped) if escaped else ''
def prepare_for_stream(value):
if str != bytes: # Python 3
return value
else: # Python 2
return value.decode()
def check_escape(raw_string):
if '\\' not in raw_string:
return raw_string
escaped = ''
i = 0
while i < len(raw_string):
if raw_string[i] == '\\' and i < len(raw_string) - 2:
try:
value = int(raw_string[i + 1: i + 3], 16)
escaped += chr(value)
i += 2
except ValueError:
escaped += '\\\\'
else:
escaped += raw_string[i]
i += 1
return escaped
def json_encode_b64(obj):
try:
return dict(encoding='base64', encoded=b64encode(obj))
except Exception as e:
raise LDAPDefinitionError('unable to encode ' + str(obj) + ' - ' + str(e))
# noinspection PyProtectedMember
def check_json_dict(json_dict):
# needed for python 2
for k, v in json_dict.items():
if isinstance(v, dict):
check_json_dict(v)
elif isinstance(v, CaseInsensitiveDict):
check_json_dict(v._store)
elif isinstance(v, SEQUENCE_TYPES):
for i, e in enumerate(v):
if isinstance(e, dict):
check_json_dict(e)
elif isinstance(e, CaseInsensitiveDict):
check_json_dict(e._store)
else:
v[i] = format_json(e)
else:
json_dict[k] = format_json(v)
def json_hook(obj):
if hasattr(obj, 'keys') and len(list(obj.keys())) == 2 and 'encoding' in obj.keys() and 'encoded' in obj.keys():
return b64decode(obj['encoded'])
return obj
# noinspection PyProtectedMember
def format_json(obj):
if isinstance(obj, CaseInsensitiveDict):
return obj._store
if isinstance(obj, datetime.datetime):
return str(obj)
if isinstance(obj, int):
return obj
if str == bytes:
if isinstance(obj, long): # long exists only in python2
return obj
try:
if str != bytes: # python3
if isinstance(obj, bytes):
return check_escape(str(obj, 'utf-8', errors='strict'))
raise LDAPDefinitionError('unable to serialize ' + str(obj))
else: # python2
if isinstance(obj, unicode):
return obj
else:
return unicode(check_escape(obj))
except (TypeError, UnicodeDecodeError):
pass
try:
return json_encode_b64(bytes(obj))
except Exception:
pass
raise LDAPDefinitionError('unable to serialize ' + str(obj))
|