codekingpro/portable-devtools
114k
1from urllib.parse import urlparse, parse_qs2 3from oauthlib.common import add_params_to_uri4 5 6def slack_compliance_fix(session):7 def _non_compliant_param_name(url, headers, data):8 # If the user has already specified the token, either in the URL9 # or in a data dictionary, then there's nothing to do.10 # If the specified token is different from ``session.access_token``,11 # we assume the user intends to override the access token.12 url_query = dict(parse_qs(urlparse(url).query))13 token = url_query.get("token")14 if not token and isinstance(data, dict):15 token = data.get("token")16 17 if token:18 # Nothing to do, just return.19 return url, headers, data20 21 if not data:22 data = {"token": session.access_token}23 elif isinstance(data, dict):24 data["token"] = session.access_token25 else:26 # ``data`` is something other than a dict: maybe a stream,27 # maybe a file object, maybe something else. We can't easily28 # modify it, so we'll set the token by modifying the URL instead.29 token = [("token", session.access_token)]30 url = add_params_to_uri(url, token)31 return url, headers, data32 33 session.register_compliance_hook("protected_request", _non_compliant_param_name)34 return session35 