Skip to main content

A python API to query a 1Password account using the 'op' command-line tool

Project description

PYONEPASSWORD

Description

A Python API to sign into and query a 1Password account using the op command.

Requirements

Example usage

Intitial sign-in

import getpass
from pyonepassword.pyonepassword import (
    OP,
    OPSigninException
)


def do_initial_signin():
    my_signin_address = "my-1p-account.1password.com"
    my_email_address = "my-1p-email@email.com"
    my_secret_key = getpass.getpass(prompt="1Password secret key:\n")
    my_password = getpass.getpass(prompt="1Password master password:\n")
    try:
        op = OP(signin_address=my_signin_address,
                email_address=my_email_address,
                secret_key=my_secret_key,
                password=my_password)
    except OPSigninException as ope:
        print("1Password initial signin failed: {}".format(ope))
        print(ope.err_output)
        exit(1)
    print("1Password is signed in and ready for lookups")
    return op


if __name__ == "__main__":
    op = do_initial_signin()
    # op is ready to use and call lookup() on
    print("Signed in.")
$ python3 ./example.py
1Password secret key:
1Password master password:
Performing initial 1Password sign-in to my-1p-account.1password.com as my-1p-email@email.com
Signed in.

Subsequent sign-in and lookup

import getpass
from pyonepassword import (
    OP,
    OPSigninException
)
def do_signin():
    # If you've already signed in at least once, you don't need to provide all
    # account details on future sign-ins. Just master password
    my_password = getpass.getpass(prompt="1Password master password:\n")
    try:
        op = OP(password=my_password)
    except OPSigninException as ope:
        print("1Password initial signin failed: {}".format(ope))
        print(ope.err_output)
        exit(1)
    return op


if __name__ == "__main__":
    op = do_signin()
    print("Signed in.")
    print("Looking up \"Example Login\"...")
    print(op.lookup("Example Login"))
    print("")
    print("\"Example Login\" can also be looked up by its uuid")
    print("")
    print("Looking up uuid \"ykhsbhhv2vf6hn2u4qwblfrmg4\"...")
    print(op.lookup("ykhsbhhv2vf6hn2u4qwblfrmg4"))
$ python3 ./examples/example-signin-lookup.py
1Password master password:

Doing normal (non-initial) 1Password sign-in

Signed in.
Looking up "Example Login"...
doth-parrot-hid-tussock-veldt

"Example Login" can also be looked up by its uuid

Looking up uuid "ykhsbhhv2vf6hn2u4qwblfrmg4"...
doth-parrot-hid-tussock-veldt

Notes

  • This has been lightly tested, and only on my Mac. I don't know if it works on other systems.
  • This has been tested with op version 0.5.6.
  • You need the op 1Password command-line tool. On a Mac with homebrew, you can do brew install 1password-cli.

TODO

  • Detect if op is/is not installed, and be helpful
  • API to get complete or partial JSON for a vault item, not just a specific field's value
  • Maybe one day 1Password.com will have an API and this module won't have to use op

Project details


Download files

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

Files for pyonepassword, version 0.1.0b3
Filename, size File type Python version Upload date Hashes
Filename, size pyonepassword-0.1.0b3-py2-none-any.whl (6.3 kB) File type Wheel Python version py2 Upload date Hashes View hashes
Filename, size pyonepassword-0.1.0b3.tar.gz (4.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page