Last active
February 20, 2020 11:03
-
-
Save beezly/095c960eaa3402571ad8f9ef1b5348cd to your computer and use it in GitHub Desktop.
1password CLI wrapper - caches your "my" login token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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