webui.py 5.3 KB

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