nodes.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # ============================================================================
  2. # :This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>
  18. # ============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. from pwman.util.crypto_engine import CryptoEngine
  22. class Node(object):
  23. def __init__(self, clear_text=True, **kwargs):
  24. if clear_text:
  25. enc = CryptoEngine.get()
  26. self._username = enc.encrypt(kwargs.get('username')).strip()
  27. self._password = enc.encrypt(kwargs.get('password')).strip()
  28. self._url = enc.encrypt(kwargs.get('url')).strip()
  29. self._notes = enc.encrypt(kwargs.get('notes')).strip()
  30. self._tags = [enc.encrypt(t).strip() for t in kwargs.get('tags',
  31. '')]
  32. @classmethod
  33. def from_encrypted_entries(cls, username, password, url, notes, tags):
  34. """
  35. We use this alternatively, to create a node instance when reading
  36. the encrypted entities from the database
  37. """
  38. node = Node(clear_text=False)
  39. node._username = username.strip()
  40. node._password = password.strip()
  41. node._url = url.strip()
  42. node._notes = notes.strip()
  43. node._tags = [t.strip() for t in tags]
  44. return node
  45. def __iter__(self):
  46. for item in ['_username', '_password',
  47. '_url', '_notes']:
  48. yield getattr(self, item)
  49. yield self._tags
  50. @property
  51. def password(self):
  52. """Get the current password."""
  53. enc = CryptoEngine.get()
  54. return enc.decrypt(self._password).strip()
  55. @property
  56. def username(self):
  57. """Get the current username."""
  58. enc = CryptoEngine.get()
  59. return enc.decrypt(self._username).strip()
  60. @username.setter
  61. def username(self, value):
  62. """Set the username."""
  63. enc = CryptoEngine.get()
  64. self._username = enc.encrypt(value).strip()
  65. @password.setter
  66. def password(self, value):
  67. """Set the Notes."""
  68. enc = CryptoEngine.get()
  69. self._password = enc.encrypt(value).strip()
  70. @property
  71. def tags(self):
  72. enc = CryptoEngine.get()
  73. try:
  74. return [enc.decrypt(tag) for tag in filter(None, self._tags)]
  75. except Exception:
  76. return [tag for tag in filter(None, self._tags)]
  77. @tags.setter
  78. def tags(self, value):
  79. self._tags = [tag for tag in value]
  80. @property
  81. def url(self):
  82. """Get the current url."""
  83. enc = CryptoEngine.get()
  84. return enc.decrypt(self._url).strip()
  85. @url.setter
  86. def url(self, value):
  87. """Set the Notes."""
  88. enc = CryptoEngine.get()
  89. self._url = enc.encrypt(value).strip()
  90. @property
  91. def notes(self):
  92. """Get the current notes."""
  93. enc = CryptoEngine.get()
  94. return enc.decrypt(self._notes).strip()
  95. @notes.setter
  96. def notes(self, value):
  97. """Set the Notes."""
  98. enc = CryptoEngine.get()
  99. self._notes = enc.encrypt(value).strip()