def generate_timestamp(): """ Generates an OAuth timestamp. :see: Nonce and Timestamp (http://tools.ietf.org/html/rfc5849#section-3.3) :returns: A string containing a positive integer representing time. """ return bytes(int(time.time()))
def bytes_to_decimal(byte_string): """ Converts a byte string to its decimal representation. :param byte_string: Byte string. :returns: Decimal-encoded byte string. """ #return bytes(int(bytes_to_hex(byte_string), 16)) return bytes(bytes_to_long(byte_string))
def urlparse_normalized(url): """ Like :func:`urlparse.urlparse` but also normalizes scheme, netloc, port, and the path. Use with OAuth URLs. :see: Base String URI (http://tools.ietf.org/html/rfc5849#section-3.4.1.2) :param url: The URL to split and normalize. :returns: Tuple that contains these elements: ``(scheme, netloc, path, params, query, fragment)`` """ if not url: raise InvalidUrlError("Invalid URL `%r`" % (url,)) parts = urlparse(url) scheme = parts.scheme.lower() # Netloc. username = parts.username or "" password = (":" + parts.password) if parts.password else "" credentials = username + password credentials = (credentials + "@") if credentials else "" # Exclude default port numbers. # See: if parts.port: if (scheme == "http" and parts.port == 80) or (scheme == "https" and parts.port == 443): port = "" else: port = (":" + bytes(parts.port)) if parts.port else "" else: port = "" netloc = credentials + parts.hostname.lower() + port # http://tools.ietf.org/html/rfc3986#section-3 # and http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.2 path = parts.path or "/" matrix_params = parts.params or "" fragment = parts.fragment or "" query = parts.query or "" return scheme, netloc, path, matrix_params, query, fragment
def percent_encode(value): """ Percent-encodes according to the OAuth spec. Used in constructing the signature base string and the "Authorization" header field. :see: Percent Encoding (http://tools.ietf.org/html/rfc5849#section-3.6) :param value: Query string parameter value to escape. If the value is a Unicode string, it will be encoded to UTF-8. A byte string is considered exactly that, a byte string and will not be UTF-8 encoded—however, it will be percent-encoded. :returns: Percent-encoded string. """ value = bytes(to_utf8_if_unicode(value)) return quote(value, safe="~")