webui.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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. templates_path = [resource_filename('pwman', 'ui/templates')]
  28. statics = [resource_filename('pwman', 'ui/templates/static')][0]
  29. AUTHENTICATED = False
  30. TAGS = None
  31. DB = None
  32. def require_auth(fn):
  33. def check_auth(**kwargs):
  34. if AUTHENTICATED:
  35. return fn(**kwargs)
  36. else:
  37. redirect("/auth")
  38. return check_auth
  39. @route('/node/:no')
  40. @require_auth
  41. def view_node(no):
  42. global DB
  43. node = DB.getnodes([no])
  44. output = template("view.tpl", node=node[0], template_lookup=templates_path)
  45. return output
  46. def submit_node(id, request):
  47. # create new\update node based on request.params.items()
  48. redirect('/')
  49. @route('/new/', method=['GET', 'POST'])
  50. @route('/edit/:no', method=['GET', 'POST'])
  51. @require_auth
  52. def edit_node(no=None):
  53. global DB
  54. if 'POST' in request.method:
  55. submit_node(no, request)
  56. if no:
  57. node = DB.getnodes([no])[0]
  58. else:
  59. class Node(object):
  60. def __init__(self):
  61. self._id = None
  62. self.username = ''
  63. self.password = ''
  64. self.url = ''
  65. self.notes = ''
  66. self.tags = ''
  67. node = Node()
  68. output = template('edit.tpl', node=node,
  69. template_lookup=templates_path)
  70. return output
  71. @route('/forget', method=['GET', 'POST'])
  72. def forget():
  73. global AUTHENTICATED
  74. AUTHENTICATED = False
  75. enc = CryptoEngine.get()
  76. enc.forget()
  77. redirect('/auth')
  78. @route('/auth', method=['GET', 'POST'])
  79. def is_authenticated():
  80. global AUTHENTICATED
  81. crypto = CryptoEngine.get()
  82. if request.method == 'POST':
  83. key = request.POST.get('pwd', '')
  84. crypto.auth(key)
  85. AUTHENTICATED = True
  86. redirect('/')
  87. else:
  88. return template("login.tpl", template_lookup=templates_path)
  89. @route('/', method=['GET', 'POST'])
  90. @require_auth
  91. def listnodes(apply=['require_login']):
  92. global AUTHENTICATED, TAGS, DB
  93. _filter = None
  94. if 'POST' in request.method:
  95. _filter = request.POST.get('tag')
  96. if _filter:
  97. DB._filtertags = [TagNew(_filter.strip())]
  98. if _filter == 'None':
  99. DB._filtertags = []
  100. nodeids = DB.listnodes()
  101. nodes = DB.getnodes(nodeids)
  102. nodesd = [''] * len(nodes)
  103. for idx, node in enumerate(nodes):
  104. ntags = [t.strip() for t in filter(None, node.tags)]
  105. nodesd[idx] = ('@'.join((node.username, node.url)),
  106. ', '.join(ntags))
  107. if not TAGS:
  108. TAGS = list(set([''.join(node.tags).strip() for node in nodes]))
  109. TAGS.sort()
  110. TAGS.insert(0, 'None')
  111. html_nodes = template("main.tpl", nodes=nodes, tags=TAGS,
  112. template_lookup=[resource_filename('pwman',
  113. 'ui/templates')])
  114. return html_nodes
  115. @route('/static/<filepath:path>')
  116. def server_static(filepath):
  117. return static_file(filepath, root=statics)
  118. if __name__ == '__main__':
  119. OSX = False
  120. args = parser_options().parse_args()
  121. xselpath, dbtype = get_conf_options(args, OSX)
  122. dbver = 0.4
  123. DB = pwman.data.factory.create(dbtype, dbver)
  124. DB.open()
  125. crypto = CryptoEngine.get()
  126. debug(True)
  127. run(reloader=True, port=9030)