nodes.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 colorama import Fore
  22. from pwman.util.crypto_engine import CryptoEngine
  23. import pwman.ui.tools
  24. class Node(object):
  25. def __init__(self, clear_text=True, **kwargs):
  26. if clear_text:
  27. enc = CryptoEngine.get()
  28. self._username = enc.encrypt(kwargs.get('username')).strip()
  29. self._password = enc.encrypt(kwargs.get('password')).strip()
  30. self._url = enc.encrypt(kwargs.get('url')).strip()
  31. self._notes = enc.encrypt(kwargs.get('notes')).strip()
  32. self._tags = [enc.encrypt(t).strip() for t in kwargs.get('tags',
  33. '')]
  34. def __str__(self):
  35. p = "{entry_title:>{width}} {entry:<{width}}\n".format(
  36. entry_title=pwman.ui.tools.typeset('Username:', Fore.RED),
  37. width=10, entry=str(self.username.decode()))
  38. p += "{entry_title:>{width}} {entry:<{width}}\n".format(
  39. entry_title=pwman.ui.tools.typeset('Password:', Fore.RED),
  40. width=10, entry=str(self.password.decode()))
  41. p += "{entry_title:>{width}} {entry:<{width}}\n".format(
  42. entry_title=pwman.ui.tools.typeset('URL:', Fore.RED),
  43. width=10, entry=str(self.url.decode()))
  44. p += "{entry_title:>{width}} {entry:<{width}}\n".format(
  45. entry_title=pwman.ui.tools.typeset('Notes:', Fore.RED),
  46. width=10, entry=str(self.notes.decode()))
  47. p += "{entry_title:>{width}} {entry:<{width}}\n".format(
  48. entry_title=pwman.ui.tools.typeset('Tags:', Fore.RED),
  49. width=10, entry=str(self.tags))
  50. return p
  51. @classmethod
  52. def from_encrypted_entries(cls, username, password, url, notes, tags):
  53. """
  54. We use this alternatively, to create a node instance when reading
  55. the encrypted entities from the database
  56. """
  57. node = Node(clear_text=False)
  58. node._username = username.strip()
  59. node._password = password.strip()
  60. node._url = url.strip()
  61. node._notes = notes.strip()
  62. node._tags = [t.strip() for t in tags]
  63. return node
  64. def __iter__(self):
  65. for item in ['_username', '_password',
  66. '_url', '_notes']:
  67. yield getattr(self, item)
  68. yield self._tags
  69. @property
  70. def password(self):
  71. """Get the current password."""
  72. enc = CryptoEngine.get()
  73. p = enc.decrypt(self._password).strip()
  74. return p.decode()
  75. @property
  76. def username(self):
  77. """Get the current username."""
  78. enc = CryptoEngine.get()
  79. u = enc.decrypt(self._username).strip()
  80. return u.decode()
  81. @username.setter
  82. def username(self, value):
  83. """Set the username."""
  84. enc = CryptoEngine.get()
  85. self._username = enc.encrypt(value).strip()
  86. @password.setter
  87. def password(self, value):
  88. """Set the Notes."""
  89. enc = CryptoEngine.get()
  90. self._password = enc.encrypt(value).strip()
  91. @property
  92. def tags(self):
  93. enc = CryptoEngine.get()
  94. try:
  95. return [enc.decrypt(tag).decode() for tag in
  96. filter(None, self._tags)]
  97. except Exception:
  98. return [tag.decode() for tag in filter(None, self._tags)]
  99. @tags.setter
  100. def tags(self, value):
  101. self._tags = [tag for tag in value]
  102. @property
  103. def url(self):
  104. """Get the current url."""
  105. enc = CryptoEngine.get()
  106. u = enc.decrypt(self._url).strip()
  107. return u.decode()
  108. @url.setter
  109. def url(self, value):
  110. """Set the Notes."""
  111. enc = CryptoEngine.get()
  112. self._url = enc.encrypt(value).strip()
  113. @property
  114. def notes(self):
  115. """Get the current notes."""
  116. enc = CryptoEngine.get()
  117. n = enc.decrypt(self._notes).strip()
  118. return n.decode()
  119. @notes.setter
  120. def notes(self, value):
  121. """Set the Notes."""
  122. enc = CryptoEngine.get()
  123. self._notes = enc.encrypt(value).strip()