diff --git a/bitwarden.sh b/bitwarden.sh index 5545648..ce9ad9d 100644 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -24,3 +24,26 @@ bitwarden_is_locked() { fi } +bitwarden_login() { + # Login to Bitwarden + if ! bitwarden_is_authenticated; then + if [ ! -z "${BW_CLIENTID}" ] && [ ! -z "${BW_CLIENTSECRET}" ]; then + echo "Login in to Bitwarden..." + BW_CLIENTID="${BW_CLIENTID}" BW_CLIENTSECRET="${BW_CLIENTSECRET}" BW login --apikey >/dev/null + if [ $? -ne 0 ]; then + echo -e "${BRed}Could not login to Bitwarden. Skipping.${NC}" + fi + else + echo "Skipping Bitwarden authentication, no credentials provided." + fi + fi + if bitwarden_is_authenticated && bitwarden_is_locked; then + if [ ! -z "${BW_PASSWORD}" ]; then + echo "Unlocking Bitwarden vault..." + export BW_SESSION=$(BW unlock --raw ${BW_PASSWORD}) + if [ -z "${BW_SESSION}" ]; then + echo -e "${BRed}Could not unlock Bitwarden vault. Skipping.${NC}" + fi + fi + fi +} diff --git a/mymachine.sh b/mymachine.sh index b5ab68d..d48ef15 100755 --- a/mymachine.sh +++ b/mymachine.sh @@ -244,27 +244,7 @@ if [ $? -ne 0 ]; then echo -e "${BRed}Failed to generate grub configuration. Skipping. Be careful !${NC}" fi -# Login to Bitwarden -if ! bitwarden_is_authenticated; then - if [ ! -z "${BW_CLIENTID}" ] && [ ! -z "${BW_CLIENTSECRET}" ]; then - echo "Login in to Bitwarden..." - BW_CLIENTID="${BW_CLIENTID}" BW_CLIENTSECRET="${BW_CLIENTSECRET}" BW login --apikey >/dev/null - if [ $? -ne 0 ]; then - echo -e "${BRed}Could not login to Bitwarden. Skipping.${NC}" - fi - else - echo "Skipping Bitwarden authentication, no credentials provided." - fi -fi -if bitwarden_is_authenticated && bitwarden_is_locked; then - if [ ! -z "${BW_PASSWORD}" ]; then - echo "Unlocking Bitwarden vault..." - export BW_SESSION=$(BW unlock --raw ${BW_PASSWORD}) - if [ -z "${BW_SESSION}" ]; then - echo -e "${BRed}Could not unlock Bitwarden vault. Skipping.${NC}" - fi - fi -fi +bitwarden_login if ! bitwarden_is_locked; then echo -n "Authenticating with bitwarden... "