identity.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Utilities for interacting with Identity Servers"""
  16. from twisted.internet import defer
  17. from synapse.api.errors import (
  18. CodeMessageException
  19. )
  20. from ._base import BaseHandler
  21. from synapse.util.async import run_on_reactor
  22. from synapse.api.errors import SynapseError
  23. import json
  24. import logging
  25. logger = logging.getLogger(__name__)
  26. class IdentityHandler(BaseHandler):
  27. def __init__(self, hs):
  28. super(IdentityHandler, self).__init__(hs)
  29. self.http_client = hs.get_simple_http_client()
  30. self.trusted_id_servers = set(hs.config.trusted_third_party_id_servers)
  31. self.trust_any_id_server_just_for_testing_do_not_use = (
  32. hs.config.use_insecure_ssl_client_just_for_testing_do_not_use
  33. )
  34. @defer.inlineCallbacks
  35. def threepid_from_creds(self, creds):
  36. yield run_on_reactor()
  37. if 'id_server' in creds:
  38. id_server = creds['id_server']
  39. elif 'idServer' in creds:
  40. id_server = creds['idServer']
  41. else:
  42. raise SynapseError(400, "No id_server in creds")
  43. if 'client_secret' in creds:
  44. client_secret = creds['client_secret']
  45. elif 'clientSecret' in creds:
  46. client_secret = creds['clientSecret']
  47. else:
  48. raise SynapseError(400, "No client_secret in creds")
  49. if id_server not in self.trusted_id_servers:
  50. if self.trust_any_id_server_just_for_testing_do_not_use:
  51. logger.warn(
  52. "Trusting untrustworthy ID server %r even though it isn't"
  53. " in the trusted id list for testing because"
  54. " 'use_insecure_ssl_client_just_for_testing_do_not_use'"
  55. " is set in the config",
  56. id_server,
  57. )
  58. else:
  59. logger.warn('%s is not a trusted ID server: rejecting 3pid ' +
  60. 'credentials', id_server)
  61. defer.returnValue(None)
  62. data = {}
  63. try:
  64. data = yield self.http_client.get_json(
  65. "https://%s%s" % (
  66. id_server,
  67. "/_matrix/identity/api/v1/3pid/getValidated3pid"
  68. ),
  69. {'sid': creds['sid'], 'client_secret': client_secret}
  70. )
  71. except CodeMessageException as e:
  72. data = json.loads(e.msg)
  73. if 'medium' in data:
  74. defer.returnValue(data)
  75. defer.returnValue(None)
  76. @defer.inlineCallbacks
  77. def bind_threepid(self, creds, mxid):
  78. yield run_on_reactor()
  79. logger.debug("binding threepid %r to %s", creds, mxid)
  80. data = None
  81. if 'id_server' in creds:
  82. id_server = creds['id_server']
  83. elif 'idServer' in creds:
  84. id_server = creds['idServer']
  85. else:
  86. raise SynapseError(400, "No id_server in creds")
  87. if 'client_secret' in creds:
  88. client_secret = creds['client_secret']
  89. elif 'clientSecret' in creds:
  90. client_secret = creds['clientSecret']
  91. else:
  92. raise SynapseError(400, "No client_secret in creds")
  93. try:
  94. data = yield self.http_client.post_urlencoded_get_json(
  95. "https://%s%s" % (
  96. id_server, "/_matrix/identity/api/v1/3pid/bind"
  97. ),
  98. {
  99. 'sid': creds['sid'],
  100. 'client_secret': client_secret,
  101. 'mxid': mxid,
  102. }
  103. )
  104. logger.debug("bound threepid %r to %s", creds, mxid)
  105. except CodeMessageException as e:
  106. data = json.loads(e.msg)
  107. defer.returnValue(data)
  108. @defer.inlineCallbacks
  109. def requestEmailToken(self, id_server, email, client_secret, send_attempt, **kwargs):
  110. yield run_on_reactor()
  111. params = {
  112. 'email': email,
  113. 'client_secret': client_secret,
  114. 'send_attempt': send_attempt,
  115. }
  116. params.update(kwargs)
  117. try:
  118. data = yield self.http_client.post_urlencoded_get_json(
  119. "https://%s%s" % (
  120. id_server,
  121. "/_matrix/identity/api/v1/validate/email/requestToken"
  122. ),
  123. params
  124. )
  125. defer.returnValue(data)
  126. except CodeMessageException as e:
  127. logger.info("Proxied requestToken failed: %r", e)
  128. raise e