Skip to content

Commit

Permalink
SG-21177 update unsecure version of httplib2 in python api (#248)
Browse files Browse the repository at this point in the history
* Upgrade httplib2 to v0.19.1 and fix the update when tweaking the imports
* Add pyparsing library
* Fix the import of pyparsing inside httplib2

Co-authored-by: Jean-Francois Boismenu <jean.francois.boismenu@autodesk.com>
Co-authored-by: Ariel Calzada <ariel.calzada.solano@autodesk.com>
  • Loading branch information
3 people authored Sep 23, 2021
1 parent 595c4c6 commit 4a8f543
Show file tree
Hide file tree
Showing 9 changed files with 7,649 additions and 837 deletions.
495 changes: 108 additions & 387 deletions shotgun_api3/lib/httplib2/python2/__init__.py

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions shotgun_api3/lib/httplib2/python2/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import base64
import re

from ... import pyparsing as pp

from .error import *

UNQUOTE_PAIRS = re.compile(r"\\(.)")
unquote = lambda s, l, t: UNQUOTE_PAIRS.sub(r"\1", t[0][1:-1])

# https://tools.ietf.org/html/rfc7235#section-1.2
# https://tools.ietf.org/html/rfc7235#appendix-B
tchar = "!#$%&'*+-.^_`|~" + pp.nums + pp.alphas
token = pp.Word(tchar).setName("token")
token68 = pp.Combine(pp.Word("-._~+/" + pp.nums + pp.alphas) + pp.Optional(pp.Word("=").leaveWhitespace())).setName(
"token68"
)

quoted_string = pp.dblQuotedString.copy().setName("quoted-string").setParseAction(unquote)
auth_param_name = token.copy().setName("auth-param-name").addParseAction(pp.downcaseTokens)
auth_param = auth_param_name + pp.Suppress("=") + (quoted_string | token)
params = pp.Dict(pp.delimitedList(pp.Group(auth_param)))

scheme = token("scheme")
challenge = scheme + (params("params") | token68("token"))

authentication_info = params.copy()
www_authenticate = pp.delimitedList(pp.Group(challenge))


def _parse_authentication_info(headers, headername="authentication-info"):
"""https://tools.ietf.org/html/rfc7615
"""
header = headers.get(headername, "").strip()
if not header:
return {}
try:
parsed = authentication_info.parseString(header)
except pp.ParseException as ex:
# print(ex.explain(ex))
raise MalformedHeader(headername)

return parsed.asDict()


def _parse_www_authenticate(headers, headername="www-authenticate"):
"""Returns a dictionary of dictionaries, one dict per auth_scheme."""
header = headers.get(headername, "").strip()
if not header:
return {}
try:
parsed = www_authenticate.parseString(header)
except pp.ParseException as ex:
# print(ex.explain(ex))
raise MalformedHeader(headername)

retval = {
challenge["scheme"].lower(): challenge["params"].asDict()
if "params" in challenge
else {"token": challenge.get("token")}
for challenge in parsed
}
return retval
48 changes: 48 additions & 0 deletions shotgun_api3/lib/httplib2/python2/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# All exceptions raised here derive from HttpLib2Error
class HttpLib2Error(Exception):
pass


# Some exceptions can be caught and optionally
# be turned back into responses.
class HttpLib2ErrorWithResponse(HttpLib2Error):
def __init__(self, desc, response, content):
self.response = response
self.content = content
HttpLib2Error.__init__(self, desc)


class RedirectMissingLocation(HttpLib2ErrorWithResponse):
pass


class RedirectLimit(HttpLib2ErrorWithResponse):
pass


class FailedToDecompressContent(HttpLib2ErrorWithResponse):
pass


class UnimplementedDigestAuthOptionError(HttpLib2ErrorWithResponse):
pass


class UnimplementedHmacDigestAuthOptionError(HttpLib2ErrorWithResponse):
pass


class MalformedHeader(HttpLib2Error):
pass


class RelativeURIError(HttpLib2Error):
pass


class ServerNotFoundError(HttpLib2Error):
pass


class ProxiesUnavailableError(HttpLib2Error):
pass
Loading

0 comments on commit 4a8f543

Please sign in to comment.