Beispiel #1
0
    def test_items_basic(self):
        # Verify item list empty initially and insert an item
        with self.s.begin():
            items = api.item_getall(self.s, self.u)
            self.assertEqual(items, [])
            item = models.Item(blob="blob", category_id=None, user=self.u)
            api.item_create(self.s, [item])

        # Retrieve and verify inserted item
        with self.s.begin():
            items = api.item_getall(self.s, self.u)
            self.assertEqual(len(items), 1)
            self.assertEqual(items[0].blob, "blob")

        # Update the item
        with self.s.begin():
            item.blob = "new blob"
            api.item_update(self.s, [item])

        # Check the updated item
        with self.s.begin():
            items = api.item_getall(self.s, self.u)
            self.assertEqual(len(items), 1)
            self.assertEqual(items[0].blob, "new blob")
            self.assertEqual(items[0].category_id, None)

        # Update item with valid category
        with self.s.begin():
            category = models.Category(name="blah", user=self.u)
            api.category_create(self.s, [category])
            item.category_id = 1
            api.item_update(self.s, [item])

        # Check the updated item
        with self.s.begin():
            items = api.item_getall(self.s, self.u)
            self.assertEqual(len(items), 1)
            self.assertEqual(items[0].blob, "new blob")
            self.assertEqual(items[0].category_id, 1)
            self.assertIsNotNone(items[0].category)

        # Clean up
        with self.s.begin():
            categories = api.category_getall(self.s, self.u)
            api.category_delete(self.s, categories, True)

        # Verify clean up successful
        with self.s.begin():
            categories = api.category_getall(self.s, self.u)
            self.assertEqual(len(categories), 0)
            items = api.item_getall(self.s, self.u)
            self.assertEqual(len(items), 0)
Beispiel #2
0
    def _do_post(self, phrase):
        item_list, error = self._check_payload(expect_list=True)
        if error:
            return error

        cipher = aescipher.AESCipher(phrase)
        items = []
        for row in item_list:
            # Make sure item id is parsed from request
            try:
                item_id = row['id']
            except KeyError:
                return self.error("Missing item id in list!")
            if not item_id:
                return self.error("Empty item id in list!")

            # Extract various item data into a list
            name = self._parse_or_set_empty(row, 'name')
            url = self._parse_or_set_empty(row, 'url')
            account = self._parse_or_set_empty(row, 'account')
            username = self._parse_or_set_empty(row, 'username')
            password = self._parse_or_set_empty(row, 'password')
            blob = self._parse_or_set_empty(row, 'blob')
            category_id = self._parse_or_set_empty(row, 'category_id')
            full_row = [name, url, account, username, password, blob]

            try:
                # TODO: (alex) deteremine if ok to insert completely empty item
                encoded_row = [
                    base64.b64encode(x.encode()).decode() for x in full_row
                ]
                encrypted_blob = cipher.encrypt("~".join(encoded_row))
                [name, url, account, username, password,
                 blob] = self._chunk6(encrypted_blob.decode())
                items.append(
                    models.Item(id=item_id,
                                name=name,
                                url=url,
                                account=account,
                                username=username,
                                password=password,
                                blob=blob,
                                category_id=category_id))
            except (AttributeError, TypeError):
                return self.error("Invalid item data in list!")

        try:
            api.item_update(items, session=self.session)
            return {'result': "success"}
        except Exception:
            return self.error("Unable to update items in the database!")
Beispiel #3
0
    def test_items_basic(self):
        # Expect empty item list initially
        items = api.item_getall(session=self.session)
        self.assertEqual(items, [])

        # Insert and retrieve an item
        item = models.Item(blob="blob", category_id=None)
        api.item_create([item], session=self.session)
        items = api.item_getall(session=self.session)
        self.assertEqual(len(items), 1)

        # Update and check the item
        item.blob = "new blob"
        item.category_id = 999
        api.item_update([item], session=self.session)
        items = api.item_getall(session=self.session)
        self.assertEqual(len(items), 1)
        self.assertEqual(items[0].blob, "new blob")
        self.assertEqual(items[0].category_id, 999)

        # Update item with valid category
        category = models.Category(name="blah")
        api.category_create([category], session=self.session)
        item.category_id = 1
        api.item_update([item], session=self.session)
        items = api.item_getall(session=self.session)
        self.assertEqual(len(items), 1)
        self.assertEqual(items[0].blob, "new blob")
        self.assertEqual(items[0].category_id, 1)
        self.assertIsNotNone(items[0].category)

        # Clean up and verify
        categories = api.category_getall(session=self.session)
        api.category_delete(categories, True, session=self.session)
        categories = api.category_getall(session=self.session)
        self.assertEqual(len(categories), 0)
        items = api.item_getall(session=self.session)
        self.assertEqual(len(items), 0)
Beispiel #4
0
def update_phrase(config, u, p, old_phrase, new_phrase):
    if len(new_phrase) < 6:
        sys.exit("Error: passphrase must be at least 6 characters long!")
    try:
        old_cipher = aescipher.AESCipher(old_phrase)
        new_cipher = aescipher.AESCipher(new_phrase)
        s = api.get_scoped_session(config.conf)
        with s.begin():
            user = api.user_get_by_username(s, u)
            if not user:
                sys.exit("Error: user does not exist!")
            if not utils.checkpw(p, user.password):
                sys.exit("Error: incorrect password!")
            try:
                if old_cipher.decrypt(user.phrase_check) != "OK":
                    sys.exit("Error: incorrect old passphrase supplied!")
            except UnicodeDecodeError:
                sys.exit("Error: incorrect old passphrase supplied!")

            printv(config, "Updating user information")
            user.phrase_check = new_cipher.encrypt("OK")
            api.user_update(s, user)
            printv(config, "Updating user's categories")
            categories = api.category_getall(s, user)
            for category in categories:
                category.recrypt(old_cipher, new_cipher)
            api.category_update(s, categories)
            printv(config, "Updating user's items")
            items = api.item_getall(s, user)
            for item in items:
                item.recrypt(old_cipher, new_cipher)
            api.item_update(s, items)
            print("All of user's data has been successfuly "
                  "re-encrypted with the new passphrase.")
    except Exception as e:
        sys.exit("Error: %s" % str(e))
Beispiel #5
0
    def _do_post(self, phrase):
        """
        Update a list of items. Similar options to create except that
        item id is required for each item in the list.

        :param phrase: decryption passphrase

        :returns: success result
        """
        payload_dicts = [{
            'name': "items",
            'is_list': True,
            'required': True
        }, {
            'name': "auto_pass",
            'is_list': False,
            'required': False
        }, {
            'name': "unique",
            'is_list': False,
            'required': False
        }, {
            'name': "genopts",
            'is_list': False,
            'required': False
        }]
        item_list, auto_pass, unique, genopts = self._check_payload(
            payload_dicts)

        if auto_pass is True:
            # Retrieve words dictionary
            words = self._get_words(genopts)

            # Generate common password for all items if needed
            if unique is not True:
                common_password = self._gen_pwd(words, genopts)

        cipher = aescipher.AESCipher(phrase)
        items = []
        for row in item_list:
            # Make sure item id is parsed from request
            try:
                item_id = row['id']
            except KeyError:
                raise bh.OppError("Missing item id in list!")
            if not item_id:
                raise bh.OppError("Empty item id in list!")

            if auto_pass is True:
                if unique is True:
                    password = self._gen_pwd(words, genopts)
                else:
                    password = common_password
            else:
                password = None

            items.append(self.make_item(row, cipher, password, item_id))

        try:
            api.item_update(self.session, items)
            response = []
            for item in items:
                response.append(item.extract(cipher))
            return {'result': 'success', 'items': response}
        except Exception:
            raise bh.OppError("Unable to update items in the database!")