Skip to content

Instantly share code, notes, and snippets.

@beezly
Last active February 20, 2020 11:03
Show Gist options
  • Save beezly/095c960eaa3402571ad8f9ef1b5348cd to your computer and use it in GitHub Desktop.
Save beezly/095c960eaa3402571ad8f9ef1b5348cd to your computer and use it in GitHub Desktop.
1password CLI wrapper - caches your "my" login token
#!/bin/bash -e
# You can use this script in two ways
# 1. Source it in to an existing script to get access to your credentials
# e.g.
# #!/bin/bash
# . ~/bin/op-wrapper
# details=$(op get item 'Top Secret Password')
# 2. Wrap another script or command with this script
# e.g.
# ~/bin/op-wrapper ./myscript up down left right
OP_WRAPPER_SESSION_FILE=~/.opwrapper
OP_DEFAULT_SESSION_LENGTH=1800
if [[ -f $OP_WRAPPER_SESSION_FILE ]]; then
. "${OP_WRAPPER_SESSION_FILE}"
fi
if [[ $OP_SESSION_my_expiry -lt $(date +%s) ]]; then
unset OP_SESSION_my
unset OP_SESSION_my_expiry
fi
if [[ -z $OP_SESSION_my ]]; then
ret_SESSION=$(op signin my --output raw)
if [[ $? -eq 0 ]]; then
echo "OP_SESSION_my=${ret_SESSION}" > ${OP_WRAPPER_SESSION_FILE}
echo "OP_SESSION_my_expiry=$(date -v +${OP_DEFAULT_SESSION_LENGTH}S +%s)" >> ${OP_WRAPPER_SESSION_FILE}
OP_SESSION_my=${ret_SESSION}
fi
fi
export OP_SESSION_my
if [[ $# -gt 0 ]]; then
"$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment