webui.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, static_file
  22. from pwman.util.crypto_engine import CryptoEngine
  23. import pwman.data.factory
  24. from pwman.data.nodes import Node
  25. from pwman import parser_options, get_conf_options
  26. from pkg_resources import resource_filename
  27. import sys
  28. from signal import SIGTERM
  29. import os
  30. templates_path = [resource_filename('pwman', 'ui/templates')]
  31. statics = [resource_filename('pwman', 'ui/templates/static')][0]
  32. AUTHENTICATED = False
  33. TAGS = None
  34. DB = None
  35. # BUG: Error: SQLite: Incorrect number of bindings supplied.
  36. # The current statement uses 2, and there are 1 supplied.
  37. # When issuing multiple times filter
  38. # WEB GUI shows multiple tags as one tag!
  39. @route('/exit', method=['GET'])
  40. def exit():
  41. os.kill(os.getpid(), SIGTERM)
  42. def require_auth(fn):
  43. def check_auth(**kwargs):
  44. if AUTHENTICATED:
  45. return fn(**kwargs)
  46. else:
  47. redirect("/auth")
  48. return check_auth
  49. @route('/static/<filename:path>')
  50. def send_static(filename):
  51. return static_file(filename, root=statics)
  52. @route('/node/:no')
  53. @require_auth
  54. def view_node(no):
  55. global DB
  56. node = DB.getnodes([no])
  57. node = DB.getnodes([no])[0]
  58. node = Node.from_encrypted_entries(node[1],
  59. node[2],
  60. node[3],
  61. node[4],
  62. node[5:])
  63. output = template("view.tpl", node=node, template_lookup=templates_path)
  64. return output
  65. def submit_node(id, request):
  66. # create new\update node based on request.params.items()
  67. redirect('/')
  68. @route('/new/', method=['GET', 'POST'])
  69. @route('/edit/:no', method=['GET', 'POST'])
  70. @require_auth
  71. def edit_node(no=None):
  72. global DB
  73. if 'POST' in request.method:
  74. submit_node(no, request)
  75. if no:
  76. node = DB.getnodes([no])[0]
  77. node = Node.from_encrypted_entries(node[1],
  78. node[2],
  79. node[3],
  80. node[4],
  81. node[5:])
  82. output = template('edit.tpl', node=node,
  83. template_lookup=templates_path)
  84. return output
  85. @route('/forget', method=['GET', 'POST'])
  86. def forget():
  87. global AUTHENTICATED
  88. AUTHENTICATED = False
  89. enc = CryptoEngine.get()
  90. enc.forget()
  91. redirect('/auth')
  92. @route('/auth', method=['GET', 'POST'])
  93. def is_authenticated():
  94. global AUTHENTICATED
  95. crypto = CryptoEngine.get()
  96. if request.method == 'POST':
  97. key = request.POST.get('pwd', '')
  98. while True:
  99. try:
  100. crypto.authenticate(key)
  101. break
  102. except Exception:
  103. redirect('/auth')
  104. AUTHENTICATED = True
  105. redirect('/')
  106. else:
  107. return template("login.tpl", template_lookup=templates_path)
  108. @route('/', method=['GET', 'POST'])
  109. @require_auth
  110. def listnodes(apply=['require_login']):
  111. global AUTHENTICATED, TAGS, DB
  112. _filter = None
  113. if 'POST' in request.method:
  114. _filter = request.POST.get('tag')
  115. if _filter:
  116. DB._filtertags = []
  117. if _filter == 'None':
  118. DB._filtertags = []
  119. nodeids = DB.listnodes()
  120. raw_nodes = DB.getnodes(nodeids)
  121. _nodes_inst = []
  122. for node in raw_nodes:
  123. _nodes_inst.append(Node.from_encrypted_entries(
  124. node[1],
  125. node[2],
  126. node[3],
  127. node[4],
  128. node[5:]))
  129. _nodes_inst[-1]._id = node[0]
  130. nodesd = _nodes_inst
  131. ce = CryptoEngine.get()
  132. tags = [ce.decrypt(t).decode() for t in DB.listtags()]
  133. html_nodes = template("main.tpl", nodes=nodesd, tags=tags,
  134. template_lookup=[resource_filename('pwman',
  135. 'ui/templates')])
  136. return html_nodes
  137. @route('/static/<filepath:path>')
  138. def server_static(filepath):
  139. return static_file(filepath, root=statics)
  140. class Pwman3WebDaemon(object):
  141. def __enter__(self):
  142. return self
  143. def run(self):
  144. global AUTHENTICATED, TAGS, DB
  145. OSX = False
  146. sys.argv = []
  147. args = parser_options().parse_args()
  148. xselpath, dburi, configp = get_conf_options(args, OSX)
  149. DB = pwman.data.factory.createdb(dburi, None)
  150. DB.open()
  151. print(dir(DB))
  152. CryptoEngine.get()
  153. debug(True)
  154. run(port=9030)
  155. def __exit__(self, type, value, traceback):
  156. return isinstance(value, TypeError)
  157. if __name__ == '__main__':
  158. with Pwman3WebDaemon() as webui:
  159. webui.run()