瀏覽代碼

bin: fix get-op-login

Thomas Dy 2 年之前
父節點
當前提交
da66f1f554
共有 1 個文件被更改,包括 31 次插入24 次删除
  1. 31 24
      .local/bin/get-op-login

+ 31 - 24
.local/bin/get-op-login

@@ -1,30 +1,37 @@
-#!/usr/bin/env nix-shell
-#!nix-shell -p expect -i "expect -f"
+#!/usr/bin/env bash
 
-log_user 0
+function log {
+  if [ -t 1 ]; then
+    echo "$@" >&2
+  fi
+}
 
-set db_path "$::env(KEEPASS_FILE)"
-set db_entry "$::env(KEEPASS_OP_ENTRY)"
+function prompt_password {
+  if command -v kdialog >/dev/null; then
+    kdialog --password Password
+  else
+    zenity --password --timeout=60
+  fi
+}
 
-stty -echo
-puts -nonewline stderr "Enter Password: "
-expect_user -re "(.*)\n"
-puts stderr ""
-stty echo
-set PASS $expect_out(1,string)
+function get_from_keepassxc {
+  keepassxc-cli show -q "$KEEPASS_FILE" "$1" -a "$2" <<< "$3"
+}
 
-set op_url [exec keepassxc-cli show -q $db_path "$db_entry" -a URL << $PASS]
-set op_username [exec keepassxc-cli show -q $db_path "$db_entry" -a UserName << $PASS]
-set op_password [exec keepassxc-cli show -q $db_path "$db_entry" -a Password << $PASS]
-set op_secretkey [exec keepassxc-cli show -q $db_path "$db_entry" -a "Secret Key" << $PASS]
+function get_credentials {
+  local opPassword
+  local password
+  if ! password=$(prompt_password 2> /dev/null); then
+    log "Failed to get password"
+    return 1
+  fi
 
-spawn op signin $op_url $op_username $op_secretkey
-expect "Enter the password for $op_username at $op_url:"
-expect -re $
-send "$op_password\r"
-expect eof
-set session $expect_out(buffer)
+  if ! opPassword=$(get_from_keepassxc "$KEEPASS_OP_ENTRY" Password "$password"); then
+    log "Failed to open vault"
+    exit 1
+  fi
 
-log_user 1
-regsub -all {\r} $session {} stripped_session
-puts $stripped_session
+  op signin -f <<<"$opPassword"
+}
+
+get_credentials