nodes.py 4.9 KB

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