def _do_new(self, args): node = {} node['username'] = self._get_input("Username: "******"Url: ") node['notes'] = self._get_input("Notes: ") node['tags'] = self._get_tags() node = Node(clear_text=True, **node) nid = self._db.add_node(node) node.id = nid return node
def do_new(self, arg): try: username = self.get_username() password = self.get_password() url = self.get_url() notes = self.get_notes() node = Node(username, password, url, notes) tags = self.get_tags() node.set_tags(tags) self._db.addnodes([node]) print "Password ID: %d" % (node.get_id()) except Exception, e: self.error(e)
def listnodes(apply=['require_login']): global AUTHENTICATED, TAGS, DB _filter = None if 'POST' in request.method: _filter = request.POST.get('tag') if _filter: DB._filtertags = [] if _filter == 'None': DB._filtertags = [] nodeids = DB.listnodes() raw_nodes = DB.getnodes(nodeids) _nodes_inst = [] for node in raw_nodes: _nodes_inst.append(Node.from_encrypted_entries( node[1], node[2], node[3], node[4], node[5:])) _nodes_inst[-1]._id = node[0] nodesd = _nodes_inst ce = CryptoEngine.get() tags = [ce.decrypt(t).decode() for t in DB.listtags()] html_nodes = template("index.tpl", nodes=nodesd, tags=tags, request=request, template_lookup=[resource_filename('pwman', 'ui/templates')]) return html_nodes
def do_export(self, args): """export the database to a given format""" try: args = ast.literal_eval(args) except Exception: args = {} filename = args.get('filename', 'pwman-export.csv') delim = args.get('delimiter', ';') nodeids = self._db.listnodes() nodes = self._db.getnodes(nodeids) with open(filename, 'w') as csvfile: writer = csv.writer(csvfile, delimiter=delim) writer.writerow(['Username', 'URL', 'Password', 'Notes', 'Tags']) for node in nodes: n = Node.from_encrypted_entries(node[1], node[2], node[3], node[4], node[5:]) tags = n.tags tags = ','.join(t.strip() for t in tags) r = list([n.username, n.url, n.password, n.notes]) writer.writerow(r + [tags]) print("Successfuly exported database to {}".format( os.path.join(os.getcwd(), filename)))
def do_edit(self, args, menu=None): ids = self._get_ids(args) for i in ids: i = int(i) node = self._db.getnodes([i]) if not node: print("Node not found ...") return node = node[0] node = node[1:5] + [node[5:]] node = Node.from_encrypted_entries(*node) if not menu: menu = CMDLoop(self.config) print ("Editing node %d." % (i)) menu.add(CliMenuItem("Username", node.username)) menu.add(CliMenuItem("Password", node.password)) menu.add(CliMenuItem("Url", node.url)) menunotes = CliMenuItem("Notes", node.notes) menu.add(menunotes) tgetter = lambda: ', '.join(t for t in node.tags) menu.add(CliMenuItem("Tags", tgetter())) menu.run(node) self._db.editnode(i, **node.to_encdict()) # when done with node erase it zerome(node._password)
def do_edit(self, args, menu=None): ids = self._get_ids(args) for i in ids: i = int(i) node = self._db.getnodes([i]) if not node: print("Node not found ...") return node = node[0] node = node[1:5] + [node[5:]] node = Node.from_encrypted_entries(*node) if not menu: menu = CMDLoop(self.config) print("Editing node %d." % (i)) menu.add(CliMenuItem("Username", node.username)) menu.add(CliMenuItem("Password", node.password)) menu.add(CliMenuItem("Url", node.url)) menunotes = CliMenuItem("Notes", node.notes) menu.add(menunotes) menu.add( CliMenuItem("Tags", ','.join(map(lambda x: x.decode(), node.tags)))) # noqa menu.run(node) self._db.editnode(i, **node.to_encdict()) # when done with node erase it zerome(node._password)
class TestNode(unittest.TestCase): def setUp(self): self.node = Node(username=b'foo', password=b's3kr3t', url=b'example.com', notes=b'just a reminder to self', tags=[b'baz', b'baz']) def test_do_encdict(self): ce = CryptoEngine.get() for k, v in self.node.to_encdict().items(): if k == 'user': self.assertEqual(ce.decrypt(v).decode(), getattr(self.node, 'username')) elif k != 'tags': self.assertEqual(ce.decrypt(v).decode(), getattr(self.node, k)) def test_setters(self): new_node = {'username': b'baz', 'password': b'n3ws3k43t', 'notes': b'i have changed the password', 'url': b'newexample.com', 'tags': [b'tag1', b'tag2']} for k in new_node: setattr(self.node, k, new_node[k]) for attribute in ['username', 'password', 'url', 'notes']: self.assertEqual(bytearray(getattr(self.node, attribute), 'utf-8'), new_node[attribute]) self.assertEqual(bytearray(getattr(self.node, 'username'), 'utf-8'), new_node['username']) self.assertEqual(bytearray(getattr(self.node, 'password'), 'utf-8'), new_node['password']) self.assertEqual(getattr(self.node, 'tags'), new_node['tags'])
def do_export(self, args): """export the database to a given format""" try: args = ast.literal_eval(args) except Exception: args = {} filename = args.get('filename', 'pwman-export.csv') delim = args.get('delimiter', ';') nodeids = self._db.listnodes() nodes = self._db.getnodes(nodeids) with open(filename, 'w') as csvfile: writer = csv.writer(csvfile, delimiter=delim) writer.writerow(['Username', 'URL', 'Password', 'Notes', 'Tags']) for node in nodes: n = Node.from_encrypted_entries(node[1], node[2], node[3], node[4], node[5:]) tags = n.tags tags = ','.join(t.strip().decode() for t in tags) r = list([n.username, n.url, n.password, n.notes]) writer.writerow(r + [tags]) with open(filename) as f: for line in f.readlines(): print(line) print("Successfuly exported database to {}".format( os.path.join(os.getcwd(), filename)))
def _db_entries_to_nodes(self, raw_nodes): _nodes_inst = [] # user, pass, url, notes for node in raw_nodes: _nodes_inst.append( Node.from_encrypted_entries(node[1], node[2], node[3], node[4], node[5:])) _nodes_inst[-1]._id = node[0] return _nodes_inst
def import_data(self, db, file): tree = ET.parse(file) root = tree.getroot() nodes = root.findall("Node") nodesarray = [] for n in nodes: username = n.findtext("username") password = n.findtext("password") url = n.findtext("url") notes = n.findtext("notes") node = Node(username, password, url, notes) tagnames = n.find("tags").findall("name") tags = [] for t in tagnames: tags.append(Tag(t.text)) node.set_tags(tags) nodesarray.append(node) db.addnodes(nodesarray)
def view_node(no): global DB node = DB.getnodes([no]) node = DB.getnodes([no])[0] node = Node.from_encrypted_entries(node[1], node[2], node[3], node[4], node[5:]) return template("ajax.tpl", request=request, node=node, template_lookup=templates_path)
def parse_list(self, db, list): lists = list.findall("PwList") for l in lists: name = l.get("name").lower().replace(' ', '') self.tagstack.append(name) self.parse_list(db, l) self.tagstack.pop() items = list.findall("PwItem") tags = [] for t in self.tagstack: tags.append(Tag(t)) for i in items: username = i.findtext("user") password = i.findtext("passwd") url = i.findtext("host") notes = "%s | %s" % (i.findtext("name"), i.findtext("launch")) n = Node(username, password, url, notes) n.set_tags(tags) self.nodes.append(n)
def test_8_do_edit_2(self): node = self.tester.cli._db.getnodes([1])[0] node = node[1:5] + [node[5:]] node = Node.from_encrypted_entries(*node) sys.stdin = StringIO(("2\ns3kr3t\nx\n")) self.tester.cli.do_edit('1') v = StringIO() sys.stdin = sys.__stdin__ sys.stdout = v self.tester.cli.do_print('1') self.assertIn('\x1b[31mPassword:\x1b[0m s3kr3t', v.getvalue())
def test_8_do_edit_1(self): node = self.tester.cli._db.getnodes([1])[0] node = node[1:5] + [node[5:]] node = Node.from_encrypted_entries(*node) sys.stdin = StringIO(("1\nfoo\nx\n")) self.tester.cli.do_edit('1') v = StringIO() sys.stdin = sys.__stdin__ sys.stdout = v self.tester.cli.do_print('1') self.assertIn('\x1b[31mUsername:\x1b[0m foo', v.getvalue())
def test_6_listnodes(self): node = Node(clear_text=True, **{ 'username': u"hatman", 'password': u"secret", 'url': u"wonderland.com", 'notes': u"a really great place", 'tags': [u'baz', u'bar'] }) self.db.add_node(node) ids = self.db.listnodes() self.assertEqual(2, len(ids))
def test_5_add_node(self): innode = [ u"TBONE", u"S3K43T", u"example.org", u"some note", [u"bartag", u"footag"] ] kwargs = { "username": innode[0], "password": innode[1], "url": innode[2], "notes": innode[3], "tags": innode[4] } node = Node(clear_text=True, **kwargs) self.db.add_node(node) outnode = self.db.getnodes([1])[0] no = outnode[1:5] no.append(outnode[5:]) o = Node.from_encrypted_entries(*no) self.assertEqual(list(node), list(o))
def _db_entries_to_nodes(self, raw_nodes): _nodes_inst = [] # user, pass, url, notes for node in raw_nodes: _nodes_inst.append(Node.from_encrypted_entries( node[1], node[2], node[3], node[4], node[5:])) _nodes_inst[-1]._id = node[0] return _nodes_inst
def test_3_add_node(self): node = Node(clear_text=True, **{ 'username': "******", 'password': "******", 'url': "wonderland.com", 'notes': "a really great place", 'tags': ['foo', 'bar'] }) self.db.add_node(node) rv = self.db._cur.execute("select * from node") ce = CryptoEngine.get() res = rv.fetchone() self.assertEqual(ce.decrypt(res[1]), b"alice")
def do_new(self, args): """ can override default config settings the following way: Pwman3 0.2.1 (c) visit: http://github.com/pwman3/pwman3 pwman> n {'leetify':False, 'numerics':True, 'special_chars':True} Password (Blank to generate): """ errmsg = ( """could not parse config override, please input some""" + """ kind of dictionary, e.g.: n {'leetify':False, """ + """'numerics':True, 'special_chars':True}""" ) try: username = self.get_username() if args: try: args = ast.literal_eval(args) except Exception: raise Exception(errmsg) if not isinstance(args, dict): raise Exception(errmsg) password = self.get_password(1, **args) else: numerics = config.get_value("Generator", "numerics").lower() == "true" # TODO: allow custom leetifying through the config leetify = config.get_value("Generator", "leetify").lower() == "true" special_chars = config.get_value("Generator", "special_chars").lower() == "true" password = self.get_password(0, numerics=numerics, symbols=leetify, special_signs=special_chars) url = self.get_url() notes = self.get_notes() node = Node(username, password, url, notes) tags = self.get_tags() node.set_tags(tags) self._db.addnodes([node]) print "Password ID: %d" % (node.get_id()) except Exception, e: self.error(e)
def test_4_test_tags(self): node = Node(clear_text=True, **{ 'username': u"alice", 'password': u"secret", 'url': u"wonderland.com", 'notes': u"a really great place", 'tags': ['foo', 'bar'] }) ce = CryptoEngine.get() self.db._get_or_create_tag(node._tags[0]) self.assertEqual(1, self.db._get_or_create_tag(node._tags[0])) rv = self.db._get_or_create_tag(ce.encrypt(b'baz')) self.assertEqual(3, rv) self.db._con.commit()
def _create_node(self, row): """create a node object with encrypted properties""" try: n = { 'clear_text': True, 'username': row[0], 'password': row[2], 'url': row[1], 'notes': row[3], 'tags': row[4].split(',') } node = Node(**n) except IndexError as err: print('{}\nDid you specify the correct delimiter?'.format(err)) sys.exit(1) return node
def test_5_add_node(self): innode = [u"TBONE", u"S3K43T", u"example.org", u"some note", [u"bartag", u"footag"]] kwargs = { "username":innode[0], "password": innode[1], "url": innode[2], "notes": innode[3], "tags": innode[4] } node = Node(clear_text=True, **kwargs) self.db.add_node(node) outnode = self.db.getnodes([1])[0] no = outnode[1:5] no.append(outnode[5:]) o = Node.from_encrypted_entries(*no) self.assertEqual(list(node), list(o))
def edit_node(no=None): global DB if 'POST' in request.method: submit_node(no, request) if no: node = DB.getnodes([no])[0] node = Node.from_encrypted_entries(node[1], node[2], node[3], node[4], node[5:]) output = template('edit.tpl', node=node, template_lookup=templates_path) return output
class TestNode(unittest.TestCase): def setUp(self): self.node = Node(username=b'foo', password=b's3kr3t', url=b'example.com', notes=b'just a reminder to self', tags=[b'baz', b'baz']) def test_do_encdict(self): ce = CryptoEngine.get() for k, v in self.node.to_encdict().items(): if k == 'user': self.assertEqual( ce.decrypt(v).decode(), getattr(self.node, 'username')) elif k != 'tags': self.assertEqual(ce.decrypt(v).decode(), getattr(self.node, k)) def test_setters(self): new_node = { 'username': b'baz', 'password': b'n3ws3k43t', 'notes': b'i have changed the password', 'url': b'newexample.com', 'tags': [b'tag1', b'tag2'] } for k in new_node: setattr(self.node, k, new_node[k]) for attribute in ['username', 'password', 'url', 'notes']: self.assertEqual(bytearray(getattr(self.node, attribute), 'utf-8'), new_node[attribute]) self.assertEqual(bytearray(getattr(self.node, 'username'), 'utf-8'), new_node['username']) self.assertEqual(bytearray(getattr(self.node, 'password'), 'utf-8'), new_node['password']) self.assertEqual(getattr(self.node, 'tags'), new_node['tags'])
def setUp(self): self.node = Node(username=b'foo', password=b's3kr3t', url=b'example.com', notes=b'just a reminder to self', tags=[b'baz', b'baz'])
def setUp(self): self.node = Node(username=u'foo', password=u's3kr3t', url=u'example.com', notes=u'just a reminder to self', tags=[u'baz', u'baz'])