Skip to main content

A python wrapper for onepassword cli version 2

Project description

OnePassword CLI v2 python wrapper

Quick n dirty python wrapper for the 1password cli version 2.

Setup

  • Install the op cli by following these instructions.
  • Make sure you have op in your PATH and that it is not version 1
$ op --version
2.12.0
  • Have your 1password username, password, and signin url handy
  • pip install onepassword2
    or, for a local install
    make local_install

Usage

CLI

The op cli tool has a lot of options for managing multiple accounts, profiles, etc. Sessions opened with the cli terminate after 10 minutes, requiring the user to re-authenticate interactively. This is good security. However, if you need long running, non-interactive usage, it's a hindrance. onepassword2 comes with a handy CLI to automagify the signin process.

export OP_ACCOUNT='user@example.com'
export OP_PASSWORD="your password"
export OP_HOSTNAME="my.1password.com"

eval $(op-signin)

...

$ op vault list

ID                            NAME
naaizerttzertzefzyhjroeqrq    Private

In python scripts

username = "user@example.com"
password = "your password"
hostname = "yourhost.1password.com"

from onepassword2 import OP2

o = OP2( username, password, hostname)
o.signin()

for v in o.vaults():
    print(v)

List items

for d in o.items():
    print(d)

Get a single item as a python dictionary

item = o.item("my item")
print(item)

Get a single item as an OP2Item object with handy methods to modify fields

from onepassword2 import OP2, OP2Item

item = OP2Item(o, "my item")
item.set("notesPlain", "new value for notes")
item.save()

If more than one item has the same name, you'll get a MultipleMatchesException

from onepassword2 import OP2, OP2Item, MultipleMatchesException

item1 = OP2Item(o)
item1.set("title", "my handy item")
item1.set("notesPlain", "new value for notes")
item1.save()

item2 = OP2Item(o)
item2.set("title", "my handy item")
item2.set("notesPlain", "hahaha same title, different content")
item2.save()

try:
    item = OP2Item(o, "my item")
    print(item)
except MultipleMatchesException:
    for i in o.items("my item"):
        item = OP2Item(o, i["id"])
        ...

Make a new secure note

from onepassword2 import OP2, OP2Item

item = OP2Item(o)
item.set("title", "my note")
item.set("notesPlain", "new value for notes")
item.save()

If you have more than one vault, you'll need to specify which one to save the secure note to:

from onepassword2 import OP2, OP2Item, OP2Vault

v = OP2Vault(o)
v.name("my new vault")
v.save()

item = OP2Item(o)
item.set("title", "my note")
item.set("vault", "my new vault")
item.set("notesPlain", "new value for notes")
item.save()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onepassword2-1.0.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

onepassword2-1.0-py3-none-any.whl (5.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page