nodes.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. node._username = bytes(username, 'utf8').strip()
  71. node._password = bytes(password, 'utf8').strip()
  72. node._url = bytes(url, 'utf8').strip()
  73. node._notes = bytes(notes, 'utf8').strip()
  74. node._tags = [bytes(t, 'utf8').strip() for t in tags]
  75. return node
  76. def __iter__(self):
  77. for item in ['_username', '_password',
  78. '_url', '_notes']:
  79. yield getattr(self, item)
  80. yield self._tags
  81. @property
  82. def password(self):
  83. """Get the current password."""
  84. enc = CryptoEngine.get()
  85. p = enc.decrypt(self._password).strip()
  86. return p.decode()
  87. @property
  88. def username(self):
  89. """Get the current username."""
  90. enc = CryptoEngine.get()
  91. u = enc.decrypt(self._username).strip()
  92. return u.decode()
  93. @username.setter
  94. def username(self, value):
  95. """Set the username."""
  96. enc = CryptoEngine.get()
  97. self._username = enc.encrypt(value).strip()
  98. @password.setter
  99. def password(self, value):
  100. """Set the Notes."""
  101. enc = CryptoEngine.get()
  102. self._password = enc.encrypt(value).strip()
  103. @property
  104. def tags(self):
  105. enc = CryptoEngine.get()
  106. try:
  107. return [enc.decrypt(tag).decode() for tag in
  108. filter(None, self._tags)]
  109. except Exception:
  110. return [tag for tag in filter(None, self._tags)]
  111. @tags.setter
  112. def tags(self, value):
  113. enc = CryptoEngine.get()
  114. self._tags = [enc.encrypt(tag).strip() for tag in value]
  115. @property
  116. def url(self):
  117. """Get the current url."""
  118. enc = CryptoEngine.get()
  119. u = enc.decrypt(self._url).strip()
  120. return u.decode()
  121. @url.setter
  122. def url(self, value):
  123. """Set the Notes."""
  124. enc = CryptoEngine.get()
  125. self._url = enc.encrypt(value).strip()
  126. @property
  127. def notes(self):
  128. """Get the current notes."""
  129. enc = CryptoEngine.get()
  130. n = enc.decrypt(self._notes).strip()
  131. return n.decode()
  132. @notes.setter
  133. def notes(self, value):
  134. """Set the Notes."""
  135. enc = CryptoEngine.get()
  136. self._notes = enc.encrypt(value).strip()