Browse Source

added script to type passwords in password store (https://www.passwordstore.org/)

Fabian Peter Hammerle 2 years ago
parent
commit
9c85b96f10
3 changed files with 59 additions and 1 deletions
  1. 1 1
      config.rasi
  2. 1 0
      scripts/README.md
  3. 57 0
      scripts/password-store-type

+ 1 - 1
config.rasi

@@ -1,5 +1,5 @@
 configuration {
 	/* --hold supported by termite & kitty */
 	ssh-command: "{terminal} --hold -e {ssh-client} {host} [-p {port}]";
-	combi-modi: "drun,ssh,run";
+	combi-modi: "drun,ssh,run,password-store-type:~/.config/rofi/scripts/password-store-type";
 }

+ 1 - 0
scripts/README.md

@@ -0,0 +1 @@
+https://github.com/davatorium/rofi/blob/next/doc/rofi-script.5.markdown

+ 57 - 0
scripts/password-store-type

@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+
+# pylint: disable=invalid-name; script name
+
+import argparse
+import os
+import pathlib
+import subprocess
+import time
+
+
+def _get_password_store_prefix_path() -> pathlib.Path:
+    # $ grep ^PREFIX $(which pass)
+    return pathlib.Path(
+        subprocess.run(
+            ["pass", "git", "rev-parse", "--show-toplevel"],
+            stdout=subprocess.PIPE,
+            check=True,
+        )
+        .stdout.rstrip()
+        .decode()
+    )
+
+
+def _main():
+    argparser = argparse.ArgumentParser(allow_abbrev=False)
+    argparser.add_argument("password_path", nargs="?")
+    args = argparser.parse_args()
+    if not args.password_path:
+        prefix = _get_password_store_prefix_path()
+        for password_path in prefix.rglob("*.gpg"):
+            print(
+                password_path.relative_to(prefix).as_posix()[
+                    : -len(password_path.suffix)
+                ]
+            )
+    else:
+        password = (
+            subprocess.run(
+                ["pass", "show", args.password_path],
+                stdout=subprocess.PIPE,
+                check=True,
+            )
+            .stdout.splitlines()[0]
+            .decode()
+        )
+        assert password.isprintable()
+        assert "'" not in password
+        os.close(1)  # close rofi's window
+        time.sleep(0.1)  # wait for rofi's window to close
+        subprocess.run(
+            ["xdotool", "-"], input=f"type '{password}'".encode(), check=True
+        )
+
+
+if __name__ == "__main__":
+    _main()