webui.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. from __future__ import print_function
  21. from bottle import route, run, debug, template, request, redirect
  22. from pwman.util.crypto import CryptoEngine
  23. import pwman.data.factory
  24. from pwman.data.tags import TagNew
  25. from pwman import parser_options, get_conf_options
  26. AUTHENTICATED = False
  27. TAGS = None
  28. DB = None
  29. tmplt = """
  30. %#template to generate a HTML table from a list of tuples (or list of lists, or tuple of tuples or ...)
  31. <form action="/" method="POST">
  32. <select multiple name="tag" onchange="this.form.submit()">
  33. %for tag in tags:
  34. <option value="{{tag}}">{{tag}}</option>
  35. %end
  36. </select>
  37. </form>
  38. <p>Click on username to view the details:</p>
  39. <table border="1">
  40. %for node in nodes:
  41. <tr>
  42. %#for item in node:
  43. %# <td><a href={{node._id}}><{{item}}</a></td>
  44. <td><a href=/node/{{node._id}}>{{node.username}}@{{node.url}}</a></td>
  45. <td>{{ ', '.join([t.strip() for t in filter(None, node.tags)]) }}</td>
  46. <td>edit</td>
  47. %end
  48. </tr>
  49. %end
  50. </table>
  51. """
  52. edit_node_tmplt = """
  53. <form action="/edit/{{node._id}}" method="POST">
  54. Username: <input type="text" name="username" value="{{node.username}}"><br>
  55. Password: <input type="password" name="password" value="{{node.password}}"><br>
  56. Repeat Password: <input type="password" name="password" value="{{node.password}}"><br>
  57. Notes: <input type="text" name="notes" value="{{node.notes}}"><br>
  58. Tags: <input type="text" name="tags" value="{{node.tags}}"><br>
  59. <input type="submit" value="Save edits">
  60. </form>
  61. """
  62. login = """
  63. <p>Please enter your database password: <b>
  64. <form action="/auth" method="POST">
  65. Password: <input type="password" name="pwd">
  66. </form>"""
  67. @route('/node/:no')
  68. def view_node(no):
  69. global DB
  70. node = DB.getnodes([no])
  71. tmplt = """
  72. <table border="1">
  73. <tr><td>Username:</td> <td>{{ node.username }}</td></tr>
  74. <tr><td>Password:</td> <td>{{ node.password }}</td></tr>
  75. <tr><td>Url:</td> <td>{{node.url}} </td></tr>
  76. <tr><td>Notes:</td> <td>{{node.notes}}</td></tr>
  77. <tr><td>Tags:</td> <td>{{node.tags}}</td></tr>
  78. </table>
  79. """
  80. output = template(tmplt, node=node[0])
  81. return output
  82. @route('/new/', method=['GET', 'POST'])
  83. @route('/edit/:no', method=['GET', 'POST'])
  84. def edit_node(no=None):
  85. global DB
  86. print("in new")
  87. if no:
  88. node = DB.getnodes([no])[0]
  89. else:
  90. print("in new")
  91. class node(object):
  92. def __init__(self):
  93. self._id = None
  94. self.username = ''
  95. self.password = ''
  96. self.url = ''
  97. self.notes = ''
  98. self.tags = ''
  99. node = node()
  100. output = template(edit_node_tmplt, node=node)
  101. return output
  102. @route('/auth', method=['GET', 'POST'])
  103. def is_authenticated():
  104. global AUTHENTICATED
  105. crypto = CryptoEngine.get()
  106. if request.method == 'POST':
  107. key = request.POST.get('pwd', '')
  108. crypto.auth(key)
  109. AUTHENTICATED = True
  110. redirect('/')
  111. else:
  112. return login
  113. @route('/', method=['GET', 'POST'])
  114. def listnodes():
  115. global AUTHENTICATED, TAGS, DB
  116. _filter = None
  117. OSX = False
  118. args = parser_options().parse_args()
  119. xselpath, dbtype = get_conf_options(args, OSX)
  120. dbver = 0.4
  121. DB = pwman.data.factory.create(dbtype, dbver)
  122. DB.open()
  123. crypto = CryptoEngine.get()
  124. if not AUTHENTICATED:
  125. redirect('/auth')
  126. if 'POST' in request.method:
  127. _filter = request.POST.get('tag')
  128. if _filter:
  129. DB._filtertags = [TagNew(_filter.strip())]
  130. if _filter == 'None':
  131. DB._filtertags = []
  132. nodeids = DB.listnodes()
  133. nodes = DB.getnodes(nodeids)
  134. nodesd = [''] * len(nodes)
  135. for idx, node in enumerate(nodes):
  136. ntags = [t.strip() for t in filter(None, node.tags)]
  137. nodesd[idx] = ('@'.join((node.username, node.url)),
  138. ', '.join(ntags))
  139. if not TAGS:
  140. TAGS = list(set([''.join(node.tags).strip() for node in nodes]))
  141. TAGS.sort()
  142. TAGS.insert(0, 'None')
  143. print(len(TAGS))
  144. output = template(tmplt, nodes=nodes, tags=TAGS)
  145. return output
  146. debug(True)
  147. run(reloader=True)