3f5402e2687475d83ec98613dc93373620e5ce57

Author
Ricky Medina <ricky.medina91@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(1password): add plugin for 1password (#8884)

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

 1diff --git a/plugins/1password/1password.plugin.zsh b/plugins/1password/1password.plugin.zsh
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..5af5ebb2bb17bb27ce27ef7423ea305385a9bf4b
 4--- /dev/null
 5+++ b/plugins/1password/1password.plugin.zsh
 6@@ -0,0 +1,46 @@
 7+if (( ${+commands[op]} )); then
 8+  eval "$(op completion zsh)"
 9+  compdef _op op
10+fi
11+
12+# opswd puts the password of the named service into the clipboard. If there's a
13+# one time password, it will be copied into the clipboard after 5 seconds. The
14+# clipboard is cleared after another 10 seconds.
15+function opswd() {
16+  if [[ $# -lt 1 ]]; then
17+    echo "Usage: opswd <service>"
18+    return 1
19+  fi
20+
21+  local service=$1
22+
23+  # If not logged in, print error and return
24+  op list users > /dev/null || return
25+
26+  local password
27+  # Copy the password to the clipboard
28+  if ! password=$(op get item "$service" --fields password 2>/dev/null); then
29+    echo "error: could not obtain password for $service"
30+    return 1
31+  fi
32+
33+  echo -n "$password" | clipcopy
34+  echo "✔ password for $service copied to clipboard"
35+
36+  # If there's a one time password, copy it to the clipboard after 5 seconds
37+  local totp
38+  if totp=$(op get totp "$service" 2>/dev/null) && [[ -n "$totp" ]]; then
39+    sleep 10 && echo -n "$totp" | clipcopy
40+    echo "✔ TOTP for $service copied to clipboard"
41+  fi
42+
43+  (sleep 20 && clipcopy </dev/null 2>/dev/null) &!
44+}
45+
46+function _opswd() {
47+  local -a services
48+  services=("${(@f)$(op list items --categories Login 2>/dev/null | op get item - --fields title 2>/dev/null)}")
49+  [[ -z "$services" ]] || compadd -a -- services
50+}
51+
52+compdef _opswd opswd
53diff --git a/plugins/1password/README.md b/plugins/1password/README.md
54new file mode 100644
55index 0000000000000000000000000000000000000000..97be45437dc7fa76c004af8d6da2b832be85c7ac
56--- /dev/null
57+++ b/plugins/1password/README.md
58@@ -0,0 +1,35 @@
59+# 1Password
60+
61+This plugin adds 1Password functionality to oh-my-zsh.
62+
63+To use, add `1password` to the list of plugins in your `.zshrc` file:
64+
65+```zsh
66+plugins=(... 1password)
67+```
68+
69+Then, you can use the command `opswd` to copy passwords for services into your
70+clipboard.
71+
72+## `opwsd`
73+
74+The `opswd` command is a wrapper around the `op` command. It takes a service
75+name as an argument and copies the password for that service to the clipboard.
76+
77+If the service also contains a TOTP, it is copied to the clipboard after 10 seconds.
78+Finally, after 20 seconds, the clipboard is cleared.
79+
80+The function has completion support, so you can use tab completion to select
81+which service you want to get.
82+
83+For example, `opswd github.com` will put your GitHub password into your clipboard, and if
84+a TOTP is available, it will be copied to the clipboard after 10 seconds.
85+
86+> NOTE: you need to be logged in for `opswd` to work. See:
87+>
88+> - [Sign in or out](https://support.1password.com/command-line/#sign-in-or-out)
89+> - [Session management](https://support.1password.com/command-line/#appendix-session-management)
90+
91+## Requirements
92+
93+- [1Password's command line utility](https://1password.com/downloads/command-line/).