Announcing `pyonepassword` 3.3, now with item creation (again)

Options
zcutlip
zcutlip
Community Member

Hello!

I'm excited to announce pyonepassword version 3.3!

You can install it from PyPI:

pip3 install pyonepassword

If you're new to pyonepassword, check out the readme, as well as the many examples.

For this version, here's a brief overview of its new features.

Item Creation

The marque feature is item creation. I had this in the legacy version of pyonepassword that worked with op version 1.x. I removed it during the rewrite for op version 2.x.

It's now back, and I think it works a lot better and makes more sense.

Currently only login items (optionally including a TOTP field) are supported, but more are on the way.

Here are a few examples:

Basic login Item creation

In the simplest case, use a convenience method on a signed-in OP object. Currently there is one for creating login items, but more will be added over time. Use op.login_item_create().

from pyonepassword import OP
from pyonepassword.api.object_types import OPLoginItem

def main():
    # see README.md for sign-in process
    op: OP = do_signin()

    title = "Example Login Item"
    username = "test_username"
    great_password = "really-great-password"

    login_url = "https://website.example"

    new_item: OPLoginItem = op.login_item_create(title,
                                                 username,
                                                 url=login_url,
                                                 password=great_password,
                                                 vault="Test Data")

You can also have a password generated by providing a password recipe, rather than a password. This involves the OPPasswordRecipe class.

from pyonepassword import OP
from pyonepassword.api.constants import LETTERS_DIGITS_SYMBOLS_20
from pyonepassword.api.object_types import OPLoginItem, OPPasswordRecipe


def main():
    # see README.md for sign-in process
    op: OP = do_signin()

    title = "Example Login Item"
    username = "test_username"
    login_url = "https://website.example"

    # Explicitly specify a recpie
    # in this case, a 40-character alphabetic-only password
    recipe = OPPasswordRecipe(length=40, digits=False, symbols=False)
    # ... or use one of the predefined constants (pyonepassword.api.constants)
    # 20-character letters/digits/symbols password
    recipe = LETTERS_DIGITS_SYMBOLS_20

    new_item: OPLoginItem = op.login_item_create(title,
                                                 username,
                                                 url=login_url,
                                                 # If 'password' is a recipe rather than a string
                                                 # the '--generate-password=<recipe>' CLI option will be used
                                                 # to auto-generate a password for this login item
                                                 password=recipe,
                                                 vault="Test Data")

Advanced Item Creation

If you want more control over the new item, such as adding custom fields or sections, you can create an item template object, and pass that object to the more generic op.item_create().

from pyonepassword import OP
from pyonepassword.api.constants import LETTERS_DIGITS_SYMBOLS_20
from pyonepassword.api.object_types import (
    OPLoginItem,
    OPLoginItemTemplate,
    OPNewSection,
    OPNewStringField
)



def main():
    # see README.md for sign-in process

    username = "test_username"
    title = "Test Login Item"
    login_url = "https://website.example"

    section_label = "Example Section"
    field_label = "Example Field"
    field_value = "Exampe field text."
    # Section ID will be randomly generated if not provided
    new_section = OPNewSection(section_label)

    # Field ID will be randomly generated if not provided
    # If section is provided, this field will be part of that section
    new_field = OPNewStringField(field_label, field_value, section=new_section)

    # add custom field and section to the new login item template
    item_template = OPLoginItemTemplate(title, username, url=login_url,
                                   sections=[new_section], fields=[new_field])

    op: OP = do_signin()

    # NOTE: only password recipes, not literal password strings are supported via item_create()
    #       however, a password string can be provided to OPLoginItemTemplate()
    recipe = LETTERS_DIGITS_SYMBOLS_20

    # Use generic 'item_create()' API to pass in an item template object
    # NOTE: item_create() will raise an exception if a password recipe is provided
    # for item types that don't support passwords
    new_item: OPLoginItem = op.item_create(item_template, password_recipe=recipe)
    return new_item

Item Deletion

Additionally, it's now possible to delete items. The API is very straightforward; here's an example:

from pyonepassword import OP
from pyonepassword.api.exceptions import OPItemDeleteException


def main():
    op: OP()
    try:
        # op.item_delete() can take any identifier accepted by the 'op' command:
        # Usage:  op item delete [{ <itemName> | <itemID> | <shareLink> | - }] [flags]
        deleted_uuid = op.item_delete("Example Login")  # noqa: F841
        # if desired inspect resulting UUID to ensure it's what was
        # Expected
    except OPItemDeleteException as ope:
        # 'op' command can fail for a few reaons, including
        # - item not found
        # - duplicate item names
        # Inspect the error message from the command
        print(ope.err_output)

Try it out and let me know if you have any questions or problems. GitHub issues are always welcome!

Cheers!


1Password Version: Not Provided
Extension Version: Not Provided
OS Version: Not Provided
Browser:_ Not Provided

This discussion has been closed.