Command line 1Password

I spend a lot of time on the cli... Since you're here, I assume you do too. In this post, I am going to show you a couple of my bash functions that greatly simplify my (work) life..

Many of us work with companies that use a password manager. At my $dayjob we use 1Password. I generally use other tools personally, but it is what we use, and it has a cli. As I spend most of my time in a terminal, it is highly beneficial for me to be able to search for entries, and obtain values from them quickly and efficiently. Unfortunately, the commands for interacting with 1P cli can get cumbersome (due to the features.) I decided to simplify it, so that I can look up, and return things very quickly and efficiently.

First, lets look up some items from 1Password:

opsearch() {
  op list items | jq '[.[] | {title: .overview.title, uuid: .uuid}]' | grep -i -A1 "$1"

Now, lets examine what we are doing -- First, we get a list of all the items in 1P, then we hand the array off to jq. With jq, we parse the data, and return a formatted list of titles and uuids. We then want to ensure that we really only return entries that contain the string we are search for, so we grep for that string, and make sure that we return the line after (which will be the uuid). Now that we have the uuid, we can get the password from another function.

oppass() {
  op get item "$1" | jq '.details.fields[]| select(.designation=="password") | {Password: .value}'

As you can see, I love jq too, it's quite handy for parsing json (duh!). Now, lets examine this a bit. First we retrieve the data stored in 1P for the uuid we have retrieved. We then hand it off to jq and parse through the array, selecting only the structure that has "designation": "password" and we return that output as Password in our own json.

Makes it quite handy for when you want to look things up and don't want to jump back to a browser or another window to search 1P for what you need.



Comments powered by Disqus