nodes.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 pwman.util.crypto_engine import CryptoEngine
  22. class NewNode(object):
  23. def __str__(self): # pragma: no cover
  24. enc = CryptoEngine.get()
  25. try:
  26. tags = ', '.join([enc.decrypt(tag).strip() for tag in filter(None,
  27. self._tags)])
  28. except Exception:
  29. tags = ', '.join([tag.strip() for tag in filter(None, self._tags)])
  30. user = enc.decrypt(self._username).strip()
  31. url = enc.decrypt(self._url).strip()
  32. return '{0}@{1}\t{2}'.format(user, url,
  33. tags)
  34. def dump_edit_to_db(self):
  35. dump = u""
  36. try:
  37. dump += u"username:"+self._username.decode()+u"##"
  38. except AttributeError:
  39. dump += u"username:"+self._username+u"##"
  40. try:
  41. dump += u"password:"+self._password.decode()+u"##"
  42. except AttributeError:
  43. dump += u"password:"+self._password+u"##"
  44. try:
  45. dump += u"url:"+self._url.decode()+u"##"
  46. except AttributeError:
  47. dump += u"url:"+self._url+u"##"
  48. try:
  49. dump += u"notes:"+self._notes.decode()+u"##"
  50. except AttributeError:
  51. dump += u"notes:"+self._notes+u"##"
  52. dump += u"tags:"
  53. tagsloc = u""
  54. for tag in self._tags:
  55. if isinstance(tag, str):
  56. tagsloc += u"tag:"+tag.strip()+u"**endtag**"
  57. else:
  58. tagsloc += u'tag:'+tag._name.decode()+u'**endtag**'
  59. dump += tagsloc
  60. dump = [dump]
  61. return dump
  62. @property
  63. def password(self):
  64. """Get the current password."""
  65. enc = CryptoEngine.get()
  66. return enc.decrypt(self._password).strip()
  67. @property
  68. def username(self):
  69. """Get the current username."""
  70. enc = CryptoEngine.get()
  71. return enc.decrypt(self._username).strip()
  72. @username.setter
  73. def username(self, value):
  74. """Set the username."""
  75. enc = CryptoEngine.get()
  76. self._username = enc.encrypt(value).strip()
  77. @password.setter
  78. def password(self, value):
  79. """Set the Notes."""
  80. enc = CryptoEngine.get()
  81. self._password = enc.encrypt(value).strip()
  82. @property
  83. def tags(self):
  84. enc = CryptoEngine.get()
  85. try:
  86. return [enc.decrypt(tag) for tag in filter(None, self._tags)]
  87. except Exception:
  88. return [tag for tag in filter(None, self._tags)]
  89. @tags.setter
  90. def tags(self, value):
  91. self._tags = [tag for tag in value]
  92. @property
  93. def url(self):
  94. """Get the current url."""
  95. enc = CryptoEngine.get()
  96. return enc.decrypt(self._url).strip()
  97. @url.setter
  98. def url(self, value):
  99. """Set the Notes."""
  100. enc = CryptoEngine.get()
  101. self._url = enc.encrypt(value).strip()
  102. @property
  103. def notes(self):
  104. """Get the current notes."""
  105. enc = CryptoEngine.get()
  106. return enc.decrypt(self._notes).strip()
  107. @notes.setter
  108. def notes(self, value):
  109. """Set the Notes."""
  110. enc = CryptoEngine.get()
  111. self._notes = enc.encrypt(value).strip()
  112. class Node(object):
  113. def __init__(self, clear_text=True, **kwargs):
  114. if clear_text:
  115. enc = CryptoEngine.get()
  116. self._username = enc.encrypt(kwargs.get('username')).strip()
  117. self._password = enc.encrypt(kwargs.get('password')).strip()
  118. self._url = enc.encrypt(kwargs.get('url')).strip()
  119. self._notes = enc.encrypt(kwargs.get('notes')).strip()
  120. _tags = [enc.encrypt(t).strip() for t in kwargs.get('tags')]
  121. self._tags = _tags
  122. @classmethod
  123. def from_encrypted_entries(cls, username, password, url, notes, tags):
  124. """
  125. We use this alternatively, to create a node instance when reading
  126. the encrypted entities from the database
  127. """
  128. node = Node(clear_text=False)
  129. node._username = username.strip()
  130. node._password = password.strip()
  131. node._url = url.strip()
  132. node._notes = notes.strip()
  133. node._tags = [t.strip() for t in tags]
  134. return node
  135. def __repr__(self):
  136. """we use this method to write node to the database,
  137. this is only practical if node is a blob!
  138. However, the new db uses a clear table structure."""
  139. res = u''
  140. tags = self.__dict__.pop('_tags', None)
  141. for item in self.__dict__:
  142. res += self.__dict__[item]
  143. return res
  144. def __iter__(self):
  145. for item in ['_username', '_password',
  146. '_url', '_notes']:
  147. yield getattr(self, item)
  148. yield self._tags
  149. @property
  150. def password(self):
  151. """Get the current password."""
  152. enc = CryptoEngine.get()
  153. return enc.decrypt(self._password).strip()
  154. @property
  155. def username(self):
  156. """Get the current username."""
  157. enc = CryptoEngine.get()
  158. return enc.decrypt(self._username).strip()
  159. @username.setter
  160. def username(self, value):
  161. """Set the username."""
  162. enc = CryptoEngine.get()
  163. self._username = enc.encrypt(value).strip()
  164. @password.setter
  165. def password(self, value):
  166. """Set the Notes."""
  167. enc = CryptoEngine.get()
  168. self._password = enc.encrypt(value).strip()
  169. @property
  170. def tags(self):
  171. enc = CryptoEngine.get()
  172. try:
  173. return [enc.decrypt(tag) for tag in filter(None, self._tags)]
  174. except Exception:
  175. return [tag for tag in filter(None, self._tags)]
  176. @tags.setter
  177. def tags(self, value):
  178. self._tags = [tag for tag in value]
  179. @property
  180. def url(self):
  181. """Get the current url."""
  182. enc = CryptoEngine.get()
  183. return enc.decrypt(self._url).strip()
  184. @url.setter
  185. def url(self, value):
  186. """Set the Notes."""
  187. enc = CryptoEngine.get()
  188. self._url = enc.encrypt(value).strip()
  189. @property
  190. def notes(self):
  191. """Get the current notes."""
  192. enc = CryptoEngine.get()
  193. return enc.decrypt(self._notes).strip()
  194. @notes.setter
  195. def notes(self, value):
  196. """Set the Notes."""
  197. enc = CryptoEngine.get()
  198. self._notes = enc.encrypt(value).strip()