urls.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Contains the URL paths to prefix various aspects of the server with. """
  17. import hmac
  18. from hashlib import sha256
  19. from urllib.parse import urlencode
  20. from synapse.config import ConfigError
  21. CLIENT_API_PREFIX = "/_matrix/client"
  22. FEDERATION_PREFIX = "/_matrix/federation"
  23. FEDERATION_V1_PREFIX = FEDERATION_PREFIX + "/v1"
  24. FEDERATION_V2_PREFIX = FEDERATION_PREFIX + "/v2"
  25. FEDERATION_UNSTABLE_PREFIX = FEDERATION_PREFIX + "/unstable"
  26. STATIC_PREFIX = "/_matrix/static"
  27. WEB_CLIENT_PREFIX = "/_matrix/client"
  28. SERVER_KEY_V2_PREFIX = "/_matrix/key/v2"
  29. MEDIA_PREFIX = "/_matrix/media/r0"
  30. LEGACY_MEDIA_PREFIX = "/_matrix/media/v1"
  31. class ConsentURIBuilder(object):
  32. def __init__(self, hs_config):
  33. """
  34. Args:
  35. hs_config (synapse.config.homeserver.HomeServerConfig):
  36. """
  37. if hs_config.form_secret is None:
  38. raise ConfigError("form_secret not set in config")
  39. if hs_config.public_baseurl is None:
  40. raise ConfigError("public_baseurl not set in config")
  41. self._hmac_secret = hs_config.form_secret.encode("utf-8")
  42. self._public_baseurl = hs_config.public_baseurl
  43. def build_user_consent_uri(self, user_id):
  44. """Build a URI which we can give to the user to do their privacy
  45. policy consent
  46. Args:
  47. user_id (str): mxid or username of user
  48. Returns
  49. (str) the URI where the user can do consent
  50. """
  51. mac = hmac.new(
  52. key=self._hmac_secret, msg=user_id.encode("ascii"), digestmod=sha256
  53. ).hexdigest()
  54. consent_uri = "%s_matrix/consent?%s" % (
  55. self._public_baseurl,
  56. urlencode({"u": user_id, "h": mac}),
  57. )
  58. return consent_uri