Repository navigation
Expand file tree
/
Copy pathcli_helpers.go
More file actions
162 lines (151 loc) · 4.19 KB
/
Copy pathcli_helpers.go
File metadata and controls
162 lines (151 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"os"
"sort"
"strings"
src "github.com/aaravmaloo/apm/src"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
func cliFail(format string, args ...any) {
color.Red(format, args...)
os.Exit(1)
}
func saveCLIVault(v *src.Vault, masterPassword string) error {
data, err := src.EncryptVault(v, masterPassword)
if err != nil {
return err
}
return src.SaveVault(vaultPath, data)
}
func unlockForWrite(what string) (string, *src.Vault) {
pass, v, readonly, err := src_unlockVault()
if err != nil {
cliFail("%v", err)
}
if readonly {
cliFail("Vault is in READ-ONLY mode. Cannot %s.", what)
}
return pass, v
}
func findTOTPAccount(v *src.Vault, query string) (string, error) {
query = strings.TrimSpace(query)
exact := []string{}
partial := map[string]bool{}
for _, t := range v.TOTPEntries {
if strings.EqualFold(t.Account, query) {
if t.Account == query {
return t.Account, nil
}
exact = append(exact, t.Account)
} else if query != "" && strings.Contains(strings.ToLower(t.Account), strings.ToLower(query)) {
partial[t.Account] = true
}
}
if len(exact) > 0 {
return exact[0], nil
}
if len(partial) == 1 {
for acc := range partial {
return acc, nil
}
}
if len(partial) > 1 {
names := []string{}
for acc := range partial {
names = append(names, acc)
}
sort.Strings(names)
return "", fmt.Errorf("Several TOTP entries match %q: %s. Use the full name.", query, strings.Join(names, ", "))
}
return "", fmt.Errorf("No TOTP entry matches %q. Run 'pm totp' to see them.", query)
}
func newTOTPLinkCmd() *cobra.Command {
return &cobra.Command{
Use: "link <entry> <domain>",
Short: "Link a TOTP entry to a site so the browser extension offers its code there",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
domain := bridgeHost(args[1])
if domain == "" || strings.ContainsAny(domain, " \t/") || (!strings.Contains(domain, ".") && domain != "localhost") {
cliFail("Give a site like github.com.")
}
pass, v := unlockForWrite("link TOTP entries")
account, err := findTOTPAccount(v, args[0])
if err != nil {
cliFail("%v", err)
}
if v.TOTPDomainLinks == nil {
v.TOTPDomainLinks = map[string]string{}
}
prev := v.TOTPDomainLinks[domain]
v.TOTPDomainLinks[domain] = account
if err := saveCLIVault(v, pass); err != nil {
cliFail("Error saving vault: %v", err)
}
src.LogAction("TOTP_LINKED", fmt.Sprintf("account=%s domain=%s", account, domain))
color.Green("Linked %s to %s. The browser extension offers this code on %s.", account, domain, domain)
if prev != "" && prev != account {
fmt.Printf("%s was linked to %s before.\n", domain, prev)
}
},
}
}
func newTOTPUnlinkCmd() *cobra.Command {
return &cobra.Command{
Use: "unlink <entry> [domain]",
Short: "Remove the site links of a TOTP entry",
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
pass, v := unlockForWrite("unlink TOTP entries")
account, err := findTOTPAccount(v, args[0])
if err != nil {
cliFail("%v", err)
}
only := ""
if len(args) == 2 {
only = bridgeHost(args[1])
}
removed := []string{}
for _, d := range totpDomainsFor(v, account) {
if only != "" && d != only {
continue
}
delete(v.TOTPDomainLinks, d)
removed = append(removed, d)
}
if len(removed) == 0 {
fmt.Printf("%s has no linked site to remove.\n", account)
return
}
if err := saveCLIVault(v, pass); err != nil {
cliFail("Error saving vault: %v", err)
}
src.LogAction("TOTP_UNLINKED", fmt.Sprintf("account=%s domains=%s", account, strings.Join(removed, ",")))
color.Green("Unlinked %s from %s.", account, strings.Join(removed, ", "))
},
}
}
func totpSiteSuffix(v *src.Vault, account string) string {
if v == nil {
return ""
}
if sites := totpDomainsFor(v, account); len(sites) > 0 {
return " (" + strings.Join(sites, ", ") + ")"
}
for _, e := range v.Entries {
if e.Account == account && e.TOTP != "" {
if host := bridgeHost(e.Website); host != "" {
return " (" + host + ")"
}
}
}
return ""
}
func spaceOrDefaultName(s string) string {
if strings.TrimSpace(s) == "" {
return "default"
}
return s
}