nodes.py 5.6 KB

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