lookupservlet.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014,2017 OpenMarket Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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. from __future__ import absolute_import
  17. from twisted.web.resource import Resource
  18. from sydent.db.threepid_associations import GlobalAssociationStore
  19. import logging
  20. import signedjson.sign
  21. from sydent.http.servlets import get_args, jsonwrap, send_cors, MatrixRestError
  22. from sydent.util import json_decoder
  23. logger = logging.getLogger(__name__)
  24. class LookupServlet(Resource):
  25. isLeaf = True
  26. def __init__(self, syd):
  27. self.sydent = syd
  28. @jsonwrap
  29. def render_GET(self, request):
  30. """
  31. Look up an individual threepid.
  32. ** DEPRECATED **
  33. Params: 'medium': the medium of the threepid
  34. 'address': the address of the threepid
  35. Returns: A signed association if the threepid has a corresponding mxid, otherwise the empty object.
  36. """
  37. send_cors(request)
  38. args = get_args(request, ('medium', 'address'))
  39. medium = args['medium']
  40. address = args['address']
  41. globalAssocStore = GlobalAssociationStore(self.sydent)
  42. sgassoc = globalAssocStore.signedAssociationStringForThreepid(medium, address)
  43. if not sgassoc:
  44. return {}
  45. sgassoc = json_decoder.decode(sgassoc)
  46. if not self.sydent.server_name in sgassoc['signatures']:
  47. # We have not yet worked out what the proper trust model should be.
  48. #
  49. # Maybe clients implicitly trust a server they talk to (and so we
  50. # should sign every assoc we return as ourselves, so they can
  51. # verify this).
  52. #
  53. # Maybe clients really want to know what server did the original
  54. # verification, and want to only know exactly who signed the assoc.
  55. #
  56. # Until we work out what we should do, sign all assocs we return as
  57. # ourself. This is vaguely ok because there actually is only one
  58. # identity server, but it happens to have two names (matrix.org and
  59. # vector.im), and so we're not really lying too much.
  60. #
  61. # We do this when we return assocs, not when we receive them over
  62. # replication, so that we can undo this decision in the future if
  63. # we wish, without having destroyed the raw underlying data.
  64. sgassoc = signedjson.sign.sign_json(
  65. sgassoc,
  66. self.sydent.server_name,
  67. self.sydent.keyring.ed25519
  68. )
  69. return sgassoc
  70. def render_OPTIONS(self, request):
  71. send_cors(request)
  72. return b''