nodes.py 5.7 KB

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