webui.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. from pkg_resources import resource_filename
  27. # TODO: split template info and put it in data file:
  28. # access them with
  29. filename = resource_filename("pwman", "htmlfiles/index.html")
  30. AUTHENTICATED = False
  31. TAGS = None
  32. DB = None
  33. tmplt = """
  34. % rebase('base.tpl')
  35. %#template to generate a HTML table from a list of tuples (or list of lists, or tuple of tuples or ...)
  36. <form action="/" method="POST">
  37. <select multiple name="tag" onchange="this.form.submit()">
  38. %for tag in tags:
  39. <option value="{{tag}}">{{tag}}</option>
  40. %end
  41. </select>
  42. </form>
  43. <p>Click on username to view the details:</p>
  44. <table border="1">
  45. %for node in nodes:
  46. <tr>
  47. %#for item in node:
  48. %# <td><a href={{node._id}}><{{item}}</a></td>
  49. <td><a href=/node/{{node._id}}>{{node.username}}@{{node.url}}</a></td>
  50. <td>{{ ', '.join([t.strip() for t in filter(None, node.tags)]) }}</td>
  51. <td>edit</td>
  52. %end
  53. </tr>
  54. %end
  55. </table>
  56. """
  57. edit_node_tmplt = """
  58. <form action="/edit/{{node._id}}" method="POST">
  59. Username: <input type="text" name="username" value="{{node.username}}"><br>
  60. Password: <input type="password" name="password" value="{{node.password}}"><br>
  61. Repeat Password: <input type="password" name="password" value="{{node.password}}"><br>
  62. Notes: <input type="text" name="notes" value="{{node.notes}}"><br>
  63. Tags: <input type="text" name="tags" value="{{node.tags}}"><br>
  64. <input type="submit" value="Save edits">
  65. </form>
  66. """
  67. login = """
  68. <p>Please enter your database password: <b>
  69. <form action="/auth" method="POST">
  70. Password: <input type="password" name="pwd">
  71. </form>"""
  72. @route('/node/:no')
  73. def view_node(no):
  74. global DB
  75. node = DB.getnodes([no])
  76. tmplt = """
  77. <table border="1">
  78. <tr><td>Username:</td> <td>{{ node.username }}</td></tr>
  79. <tr><td>Password:</td> <td>{{ node.password }}</td></tr>
  80. <tr><td>Url:</td> <td>{{node.url}} </td></tr>
  81. <tr><td>Notes:</td> <td>{{node.notes}}</td></tr>
  82. <tr><td>Tags:</td> <td>{{node.tags}}</td></tr>
  83. </table>
  84. """
  85. output = template(tmplt, node=node[0])
  86. return output
  87. def submit_node(id, request):
  88. # create new\update node based on request.params.items()
  89. redirect('/')
  90. @route('/new/', method=['GET', 'POST'])
  91. @route('/edit/:no', method=['GET', 'POST'])
  92. def edit_node(no=None):
  93. global DB
  94. if 'POST' in request.method:
  95. submit_node(no, request)
  96. if no:
  97. node = DB.getnodes([no])[0]
  98. else:
  99. class node(object):
  100. def __init__(self):
  101. self._id = None
  102. self.username = ''
  103. self.password = ''
  104. self.url = ''
  105. self.notes = ''
  106. self.tags = ''
  107. node = node()
  108. output = template(edit_node_tmplt, node=node)
  109. return output
  110. @route('/auth', method=['GET', 'POST'])
  111. def is_authenticated():
  112. global AUTHENTICATED
  113. crypto = CryptoEngine.get()
  114. if request.method == 'POST':
  115. key = request.POST.get('pwd', '')
  116. crypto.auth(key)
  117. AUTHENTICATED = True
  118. redirect('/')
  119. else:
  120. return login
  121. @route('/', method=['GET', 'POST'])
  122. def listnodes():
  123. global AUTHENTICATED, TAGS, DB
  124. _filter = None
  125. OSX = False
  126. args = parser_options().parse_args()
  127. xselpath, dbtype = get_conf_options(args, OSX)
  128. dbver = 0.4
  129. DB = pwman.data.factory.create(dbtype, dbver)
  130. DB.open()
  131. crypto = CryptoEngine.get()
  132. if not AUTHENTICATED:
  133. redirect('/auth')
  134. if 'POST' in request.method:
  135. _filter = request.POST.get('tag')
  136. if _filter:
  137. DB._filtertags = [TagNew(_filter.strip())]
  138. if _filter == 'None':
  139. DB._filtertags = []
  140. nodeids = DB.listnodes()
  141. nodes = DB.getnodes(nodeids)
  142. nodesd = [''] * len(nodes)
  143. for idx, node in enumerate(nodes):
  144. ntags = [t.strip() for t in filter(None, node.tags)]
  145. nodesd[idx] = ('@'.join((node.username, node.url)),
  146. ', '.join(ntags))
  147. if not TAGS:
  148. TAGS = list(set([''.join(node.tags).strip() for node in nodes]))
  149. TAGS.sort()
  150. TAGS.insert(0, 'None')
  151. print(len(TAGS))
  152. html_nodes = template(tmplt, nodes=nodes, tags=TAGS)
  153. return html_nodes
  154. debug(True)
  155. run(reloader=True)