webui.py 6.1 KB

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