def parse_request(self): """Http version checking and connection close, force Basic Authentication""" import base64 try: from urllib import unquote as unquote_to_bytes except ImportError: from urllib.parse import unquote_to_bytes isValid = False if SimpleXMLRPCRequestHandler.parse_request(self): enctype, encstr = self.headers.get('Authorization').split() encstr = base64.b64decode(encstr).decode("utf-8") emailid, password = encstr.split(':') emailid = unquote_to_bytes(emailid).decode("utf-8") password = unquote_to_bytes(password).decode("utf-8") isValid = emailid == BMConfigParser().get('bitmessagesettings', 'apiusername') and password == BMConfigParser().get('bitmessagesettings', 'apipassword') else: logger.warning( 'Authentication failed because header lacks' ' Authentication field') time.sleep(2) if isValid is False: self.send_error(401, b'Authetication failed') return isValid
def _try_unescape(match): byte_string = unquote_to_bytes(match.group(0)) unicode_string = byte_string.decode('utf-8', 'iriutf8') for safe_char in list(safe): unicode_string = unicode_string.replace( safe_char, '%%%02x' % ord(safe_char)) return unicode_string
def _urlunquote(byte_string, remap=None, preserve=None): """ Unquotes a URI portion from a byte string into unicode using UTF-8 :param byte_string: A byte string of the data to unquote :param remap: A list of characters (as unicode) that should be re-mapped to a %XX encoding. This is used when characters are not valid in part of a URL. :param preserve: A bool - indicates that the chars to be remapped if they occur in non-hex form, should be preserved. E.g. / for URL path. :return: A unicode string """ if byte_string is None: return byte_string if byte_string == b'': return '' if preserve: replacements = ['\x1A', '\x1C', '\x1D', '\x1E', '\x1F'] preserve_unmap = {} for char in remap: replacement = replacements.pop(0) preserve_unmap[replacement] = char byte_string = byte_string.replace(char.encode('ascii'), replacement.encode('ascii')) byte_string = unquote_to_bytes(byte_string) if remap: for char in remap: byte_string = byte_string.replace( char.encode('ascii'), ('%%%02x' % ord(char)).encode('ascii')) output = byte_string.decode('utf-8', 'iriutf8') if preserve: for replacement, original in preserve_unmap.items(): output = output.replace(replacement, original) return output
def _urlunquote(byte_string, remap=None, preserve=None): """ Unquotes a URI portion from a byte string into unicode using UTF-8 :param byte_string: A byte string of the data to unquote :param remap: A list of characters (as unicode) that should be re-mapped to a %XX encoding. This is used when characters are not valid in part of a URL. :param preserve: A bool - indicates that the chars to be remapped if they occur in non-hex form, should be preserved. E.g. / for URL path. :return: A unicode string """ if byte_string is None: return byte_string if byte_string == b'': return '' if preserve: replacements = ['\x1A', '\x1C', '\x1D', '\x1E', '\x1F'] preserve_unmap = {} for char in remap: replacement = replacements.pop(0) preserve_unmap[replacement] = char byte_string = byte_string.replace(char.encode('ascii'), replacement.encode('ascii')) byte_string = unquote_to_bytes(byte_string) if remap: for char in remap: byte_string = byte_string.replace(char.encode('ascii'), ('%%%02x' % ord(char)).encode('ascii')) output = byte_string.decode('utf-8', 'iriutf8') if preserve: for replacement, original in preserve_unmap.items(): output = output.replace(replacement, original) return output
def url_to_unicode(s): # Python 2 # >>> print(unquote('caf%c3%a9').decode('utf-8')) # 'café' # >>> print(unquote(u'caf%c3%a9').encode('raw_unicode_escape').decode('utf-8')) # 'café' # # Python 3 # >>> print(unquote('caf%c3%a9')) # 'café' # >>> print(unquote_to_bytes(b'caf%c3%a9').decode('utf-8')) # 'café' # # So for consistent results, we need to encode() the incoming string # and call unquote (2) or unquote_to_bytes (3) before decode() s = s.encode('utf-8') s = unquote_to_bytes(s) return s.decode('utf-8')
def name_from_url(url): """Extract everything after the last slash in the URL""" url = re.sub(r"^.*/", "", url) unquoted = unquote_to_bytes(url.encode("utf-8")) return unquoted.decode("utf-8")
def _try_unescape(match): byte_string = unquote_to_bytes(match.group(0)) unicode_string = byte_string.decode('utf-8', 'iriutf8') for safe_char in list(safe): unicode_string = unicode_string.replace(safe_char, '%%%02x' % ord(safe_char)) return unicode_string
def name_from_url(url): """Extract everything after the last slash in the URL""" url = re.sub(r'^.*/', '', url) unquoted = unquote_to_bytes(url.encode('utf-8')) return unquoted.decode('utf-8')
def basic_str(quote_auth): auth = unquote_to_bytes(quote_auth) auth = BMS_encode(auth, 'base64').decode("utf-8") auth = "".join(auth.split()) # get rid of whitespace return auth