nodes.py 5.8 KB

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