nodes.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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
  33. kwargs.get('tags', '')]
  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))
  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))
  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))
  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))
  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. def to_encdict(self):
  52. """
  53. Return a dictionary of encrypted records
  54. """
  55. d = {}
  56. d['user'] = self._username
  57. d['password'] = self._password
  58. d['notes'] = self._notes
  59. d['url'] = self._url
  60. d['tags'] = self._tags
  61. return d
  62. @classmethod
  63. def from_encrypted_entries(cls, username, password, url, notes, tags):
  64. """
  65. We use this alternatively, to create a node instance when reading
  66. the encrypted entities from the database
  67. """
  68. node = Node(clear_text=False)
  69. node._username = bytes(username).strip()
  70. node._password = bytes(password).strip()
  71. node._url = bytes(url).strip()
  72. node._notes = bytes(notes).strip()
  73. node._tags = [bytes(t).strip() for t in tags]
  74. return node
  75. def __iter__(self):
  76. for item in ['_username', '_password',
  77. '_url', '_notes']:
  78. yield getattr(self, item)
  79. yield self._tags
  80. @property
  81. def password(self):
  82. """Get the current password."""
  83. enc = CryptoEngine.get()
  84. p = enc.decrypt(self._password).strip()
  85. return p.decode()
  86. @property
  87. def username(self):
  88. """Get the current username."""
  89. enc = CryptoEngine.get()
  90. u = enc.decrypt(self._username).strip()
  91. return u.decode()
  92. @username.setter
  93. def username(self, value):
  94. """Set the username."""
  95. enc = CryptoEngine.get()
  96. self._username = enc.encrypt(value).strip()
  97. @password.setter
  98. def password(self, value):
  99. """Set the Notes."""
  100. enc = CryptoEngine.get()
  101. self._password = enc.encrypt(value).strip()
  102. @property
  103. def tags(self):
  104. enc = CryptoEngine.get()
  105. try:
  106. return [enc.decrypt(tag).decode() for tag in
  107. filter(None, self._tags)]
  108. except Exception:
  109. return [tag for tag in filter(None, self._tags)]
  110. @tags.setter
  111. def tags(self, value):
  112. self._tags = [tag for tag in value]
  113. @property
  114. def url(self):
  115. """Get the current url."""
  116. enc = CryptoEngine.get()
  117. u = enc.decrypt(self._url).strip()
  118. return u.decode()
  119. @url.setter
  120. def url(self, value):
  121. """Set the Notes."""
  122. enc = CryptoEngine.get()
  123. self._url = enc.encrypt(value).strip()
  124. @property
  125. def notes(self):
  126. """Get the current notes."""
  127. enc = CryptoEngine.get()
  128. n = enc.decrypt(self._notes).strip()
  129. return n.decode()
  130. @notes.setter
  131. def notes(self, value):
  132. """Set the Notes."""
  133. enc = CryptoEngine.get()
  134. self._notes = enc.encrypt(value).strip()