lookupservlet.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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
  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 self.sydent.server_name not 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, self.sydent.server_name, self.sydent.keyring.ed25519
  66. )
  67. return sgassoc
  68. def render_OPTIONS(self, request):
  69. send_cors(request)
  70. return b""