slack.py 1.3 KB

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