slack.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. try:
  2. from urlparse import urlparse, parse_qs
  3. except ImportError:
  4. from urllib.parse import urlparse, parse_qs
  5. from oauthlib.common import add_params_to_uri
  6. def 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 URL
  9. # 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. if token:
  17. # Nothing to do, just return.
  18. return url, headers, data
  19. if not data:
  20. data = {"token": session.access_token}
  21. elif isinstance(data, dict):
  22. data["token"] = session.access_token
  23. else:
  24. # ``data`` is something other than a dict: maybe a stream,
  25. # maybe a file object, maybe something else. We can't easily
  26. # modify it, so we'll set the token by modifying the URL instead.
  27. token = [("token", session.access_token)]
  28. url = add_params_to_uri(url, token)
  29. return url, headers, data
  30. session.register_compliance_hook("protected_request", _non_compliant_param_name)
  31. return session