threepidbindservlet.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 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.util.stringutils import is_valid_client_secret
  19. from sydent.db.valsession import ThreePidValSessionStore
  20. from sydent.http.servlets import get_args, jsonwrap, send_cors, MatrixRestError
  21. from sydent.http.auth import authV2
  22. from sydent.util.stringutils import is_valid_client_secret
  23. from sydent.validators import SessionExpiredException, IncorrectClientSecretException, InvalidSessionIdException,\
  24. SessionNotValidatedException
  25. from sydent.threepid.bind import BindingNotPermittedException
  26. class ThreePidBindServlet(Resource):
  27. def __init__(self, sydent, require_auth=False):
  28. self.sydent = sydent
  29. self.require_auth = require_auth
  30. @jsonwrap
  31. def render_POST(self, request):
  32. send_cors(request)
  33. account = None
  34. if self.require_auth:
  35. account = authV2(self.sydent, request)
  36. args = get_args(request, ('sid', 'client_secret', 'mxid'))
  37. sid = args['sid']
  38. mxid = args['mxid']
  39. clientSecret = args['client_secret']
  40. if not is_valid_client_secret(clientSecret):
  41. raise MatrixRestError(
  42. 400, 'M_INVALID_PARAM', 'Invalid client_secret provided')
  43. if account:
  44. # This is a v2 API so only allow binding to the logged in user id
  45. if account.userId != mxid:
  46. raise MatrixRestError(403, 'M_UNAUTHORIZED', "This user is prohibited from binding to the mxid");
  47. try:
  48. valSessionStore = ThreePidValSessionStore(self.sydent)
  49. s = valSessionStore.getValidatedSession(sid, clientSecret)
  50. except (IncorrectClientSecretException, InvalidSessionIdException):
  51. # Return the same error for not found / bad client secret otherwise
  52. # people can get information about sessions without knowing the
  53. # secret.
  54. raise MatrixRestError(
  55. 404,
  56. 'M_NO_VALID_SESSION',
  57. "No valid session was found matching that sid and client secret")
  58. except SessionExpiredException:
  59. raise MatrixRestError(
  60. 400,
  61. 'M_SESSION_EXPIRED',
  62. "This validation session has expired: call requestToken again")
  63. except SessionNotValidatedException:
  64. raise MatrixRestError(
  65. 400,
  66. 'M_SESSION_NOT_VALIDATED',
  67. "This validation session has not yet been completed")
  68. try:
  69. res = self.sydent.threepidBinder.addBinding(s.medium, s.address, mxid)
  70. except BindingNotPermittedException:
  71. raise MatrixRestError(
  72. 400,
  73. 'M_BINDING_NOT_PERMITTED',
  74. "This threepid may not be bound to this mxid")
  75. return res
  76. def render_OPTIONS(self, request):
  77. send_cors(request)
  78. return b''