urls.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 six.moves.urllib.parse import urlencode
  20. from synapse.config import ConfigError
  21. CLIENT_PREFIX = "/_matrix/client/api/v1"
  22. CLIENT_V2_ALPHA_PREFIX = "/_matrix/client/v2_alpha"
  23. FEDERATION_PREFIX = "/_matrix/federation/v1"
  24. STATIC_PREFIX = "/_matrix/static"
  25. WEB_CLIENT_PREFIX = "/_matrix/client"
  26. CONTENT_REPO_PREFIX = "/_matrix/content"
  27. SERVER_KEY_PREFIX = "/_matrix/key/v1"
  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(
  39. "form_secret not set in config",
  40. )
  41. if hs_config.public_baseurl is None:
  42. raise ConfigError(
  43. "public_baseurl not set in config",
  44. )
  45. self._hmac_secret = hs_config.form_secret.encode("utf-8")
  46. self._public_baseurl = hs_config.public_baseurl
  47. def build_user_consent_uri(self, user_id):
  48. """Build a URI which we can give to the user to do their privacy
  49. policy consent
  50. Args:
  51. user_id (str): mxid or username of user
  52. Returns
  53. (str) the URI where the user can do consent
  54. """
  55. mac = hmac.new(
  56. key=self._hmac_secret,
  57. msg=user_id,
  58. digestmod=sha256,
  59. ).hexdigest()
  60. consent_uri = "%s_matrix/consent?%s" % (
  61. self._public_baseurl,
  62. urlencode({
  63. "u": user_id,
  64. "h": mac
  65. }),
  66. )
  67. return consent_uri