1
0

blindlysignstuffservlet.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. # Copyright 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. from __future__ import absolute_import
  16. from twisted.web.resource import Resource
  17. import logging
  18. import signedjson.key
  19. import signedjson.sign
  20. from sydent.db.invite_tokens import JoinTokenStore
  21. from sydent.http.servlets import get_args, jsonwrap, send_cors, MatrixRestError
  22. from sydent.http.auth import authV2
  23. logger = logging.getLogger(__name__)
  24. class BlindlySignStuffServlet(Resource):
  25. isLeaf = True
  26. def __init__(self, syd, require_auth=False):
  27. self.sydent = syd
  28. self.server_name = syd.server_name
  29. self.tokenStore = JoinTokenStore(syd)
  30. self.require_auth = require_auth
  31. @jsonwrap
  32. def render_POST(self, request):
  33. send_cors(request)
  34. if self.require_auth:
  35. authV2(self.sydent, request)
  36. args = get_args(request, ("private_key", "token", "mxid"))
  37. private_key_base64 = args['private_key']
  38. token = args['token']
  39. mxid = args['mxid']
  40. sender = self.tokenStore.getSenderForToken(token)
  41. if sender is None:
  42. raise MatrixRestError(404, "M_UNRECOGNIZED", "Didn't recognize token")
  43. to_sign = {
  44. "mxid": mxid,
  45. "sender": sender,
  46. "token": token,
  47. }
  48. try:
  49. private_key = signedjson.key.decode_signing_key_base64(
  50. "ed25519",
  51. "0",
  52. private_key_base64
  53. )
  54. signed = signedjson.sign.sign_json(
  55. to_sign,
  56. self.server_name,
  57. private_key
  58. )
  59. except:
  60. logger.exception("signing failed")
  61. raise MatrixRestError(500, "M_UNKNOWN", "Internal Server Error")
  62. return signed
  63. def render_OPTIONS(self, request):
  64. send_cors(request)
  65. return b''