signer.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright 2014 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import TYPE_CHECKING, Any, Dict
  15. import signedjson.sign
  16. if TYPE_CHECKING:
  17. from sydent.sydent import Sydent
  18. from sydent.threepid import ThreepidAssociation
  19. class Signer:
  20. def __init__(self, sydent: "Sydent") -> None:
  21. self.sydent = sydent
  22. def signedThreePidAssociation(self, assoc: "ThreepidAssociation") -> Dict[str, Any]:
  23. """
  24. Signs a 3PID association.
  25. :param assoc: The association to sign.
  26. :return: A signed representation of the association.
  27. """
  28. sgassoc = {
  29. "medium": assoc.medium,
  30. "address": assoc.address,
  31. "mxid": assoc.mxid,
  32. "ts": assoc.ts,
  33. "not_before": assoc.not_before,
  34. "not_after": assoc.not_after,
  35. }
  36. sgassoc.update(assoc.extra_fields)
  37. sgassoc = signedjson.sign.sign_json(
  38. sgassoc, self.sydent.config.general.server_name, self.sydent.keyring.ed25519
  39. )
  40. return sgassoc