pubkeyservlets.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 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. from unpaddedbase64 import encode_base64
  18. from sydent.db.invite_tokens import JoinTokenStore
  19. from sydent.http.servlets import get_args, jsonwrap
  20. class Ed25519Servlet(Resource):
  21. isLeaf = True
  22. def __init__(self, syd):
  23. self.sydent = syd
  24. @jsonwrap
  25. def render_GET(self, request):
  26. pubKey = self.sydent.keyring.ed25519.verify_key
  27. pubKeyBase64 = encode_base64(pubKey.encode())
  28. return {'public_key': pubKeyBase64}
  29. class PubkeyIsValidServlet(Resource):
  30. isLeaf = True
  31. def __init__(self, syd):
  32. self.sydent = syd
  33. @jsonwrap
  34. def render_GET(self, request):
  35. args = get_args(request, ("public_key",))
  36. pubKey = self.sydent.keyring.ed25519.verify_key
  37. pubKeyBase64 = encode_base64(pubKey.encode())
  38. return {'valid': args["public_key"] == pubKeyBase64}
  39. class EphemeralPubkeyIsValidServlet(Resource):
  40. isLeaf = True
  41. def __init__(self, syd):
  42. self.joinTokenStore = JoinTokenStore(syd)
  43. @jsonwrap
  44. def render_GET(self, request):
  45. args = get_args(request, ("public_key",))
  46. publicKey = args["public_key"]
  47. return {
  48. 'valid': self.joinTokenStore.validateEphemeralPublicKey(publicKey),
  49. }