invite_tokens.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 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. import time
  16. class JoinTokenStore(object):
  17. def __init__(self, sydent):
  18. self.sydent = sydent
  19. def storeToken(self, medium, address, roomId, sender, token):
  20. cur = self.sydent.db.cursor()
  21. cur.execute("INSERT INTO invite_tokens"
  22. " ('medium', 'address', 'room_id', 'sender', 'token', 'received_ts')"
  23. " VALUES (?, ?, ?, ?, ?, ?)",
  24. (medium, address, roomId, sender, token, int(time.time())))
  25. self.sydent.db.commit()
  26. def getTokens(self, medium, address):
  27. cur = self.sydent.db.cursor()
  28. res = cur.execute(
  29. "SELECT medium, address, room_id, sender, token FROM invite_tokens"
  30. " WHERE medium = ? AND address = ? AND sent_ts IS NULL",
  31. (medium, address,)
  32. )
  33. rows = res.fetchall()
  34. ret = []
  35. for row in rows:
  36. medium, address, roomId, sender, token = row
  37. ret.append({
  38. "medium": medium,
  39. "address": address,
  40. "room_id": roomId,
  41. "sender": sender,
  42. "token": token,
  43. })
  44. return ret
  45. def markTokensAsSent(self, medium, address):
  46. cur = self.sydent.db.cursor()
  47. cur.execute(
  48. "UPDATE invite_tokens SET sent_ts = ? WHERE medium = ? AND address = ?",
  49. (int(time.time()), medium, address,)
  50. )
  51. self.sydent.db.commit()
  52. def storeEphemeralPublicKey(self, publicKey):
  53. cur = self.sydent.db.cursor()
  54. cur.execute(
  55. "INSERT INTO ephemeral_public_keys"
  56. " (public_key, persistence_ts)"
  57. " VALUES (?, ?)",
  58. (publicKey, int(time.time()))
  59. )
  60. self.sydent.db.commit()
  61. def validateEphemeralPublicKey(self, publicKey):
  62. cur = self.sydent.db.cursor()
  63. cur.execute(
  64. "UPDATE ephemeral_public_keys"
  65. " SET verify_count = verify_count + 1"
  66. " WHERE public_key = ?",
  67. (publicKey,)
  68. )
  69. self.sydent.db.commit()
  70. return cur.rowcount > 0
  71. def getSenderForToken(self, token):
  72. cur = self.sydent.db.cursor()
  73. res = cur.execute(
  74. "SELECT sender FROM invite_tokens WHERE token = ?",
  75. (token,)
  76. )
  77. rows = res.fetchall()
  78. if rows:
  79. return rows[0][0]
  80. return None
  81. def deleteTokens(self, medium, address):
  82. cur = self.sydent.db.cursor()
  83. cur.execute(
  84. "DELETE FROM invite_tokens WHERE medium = ? AND address = ?",
  85. (medium, address,)
  86. )
  87. self.sydent.db.commit()