/
keeperutils.go
179 lines (152 loc) · 4.27 KB
/
keeperutils.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package utils
import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/fatih/color"
)
// KeeperRecord represents a record maintained by Keeper.
type KeeperRecord struct {
UID string
Title string
URL string
Username string
Password string
TOTP string
Note string
}
// keeperConfigPath returns the path of the keeper config file.
func keeperConfigPath() (string, error) {
home, err := GetHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".keeper", "config.json"), nil
}
// CommanderInstalled returns true if keeper
// commander is installed on the current system.
func CommanderInstalled() bool {
return CmdExists("keeper")
}
// KeeperLoggedIn returns true if keeper vault
// is logged in with the input `email`.
// Otherwise, it returns false.
func KeeperLoggedIn() bool {
if !CommanderInstalled() {
err := errors.New(color.RedString(
"keeper commander is not installed - please install and try again"))
fmt.Println(err)
return false
}
configPath, err := keeperConfigPath()
if err != nil {
err := errors.New(color.RedString(
"failed to retrieve keeper config path"))
fmt.Println(err)
return false
}
loggedIn := "My Vault>"
out, err := RunCommandWithTimeout(5, "keeper", "shell", "--config", configPath)
if err != nil {
fmt.Print("failed to check login state "+
"for keeper vault: ", err)
return false
}
if strings.Contains(out, loggedIn) {
return true
}
return false
}
type rawRecord struct {
RecordUID string `json:"record_uid"`
Title string `json:"title"`
Type string `json:"type"`
Fields []struct {
Type string `json:"type"`
Value []string `json:"value"`
} `json:"fields"`
}
// RetrieveKeeperRecord returns the record found with the input keeperPath.
func RetrieveKeeperRecord(keeperUID string) (KeeperRecord, error) {
var record KeeperRecord
// Ensure keeper commander is installed and
// there is a valid keeper session.
if !CommanderInstalled() || !KeeperLoggedIn() {
return record, errors.New("error: ensure keeper commander is installed and a valid keeper session is established")
}
// fmt.Printf("Retrieving record with ID %s from keeper\n", keeperUID)
configPath, err := keeperConfigPath()
if err != nil {
err := errors.New(color.RedString(
"failed to retrieve keeper config path"))
return record, err
}
jsonData, err := RunCommand("keeper", "get", keeperUID, "--unmask", "--format", "json", "--config", configPath)
if err != nil {
return record, err
}
var r rawRecord
if err := json.Unmarshal([]byte(jsonData), &r); err != nil {
return record, err
}
record.UID = r.RecordUID
record.Title = r.Title
for _, field := range r.Fields {
if len(field.Value) > 0 {
switch field.Type {
case "login":
record.Username = field.Value[0]
case "password":
record.Password = field.Value[0]
case "url":
record.URL = field.Value[0]
case "oneTimeCode":
record.TOTP = field.Value[0]
case "note":
record.Note = field.Value[0]
}
}
}
return record, nil
}
// SearchKeeperRecords searches the logged-in user's
// keeper records for records matching the input searchTerm.
//
// The searchTerm can be a string or regex.
//
// Example Inputs:
//
// SearchKeeperRecords("TESTING")
// SearchKeeperRecords("TEST.*RD")
//
// If a searchTerm matches a record, the associated UID is returned.
func SearchKeeperRecords(searchTerm string) (string, error) {
recordUID := ""
// Ensure keeper commander is installed and
// there is a valid keeper session.
if !CommanderInstalled() || !KeeperLoggedIn() {
return recordUID, errors.New("error: ensure keeper commander is installed and a valid keeper session is established")
}
fmt.Printf("Searching keeper for records matching %s, please wait...\n", searchTerm)
configPath, err := keeperConfigPath()
if err != nil {
err := errors.New(color.RedString(
"failed to retrieve keeper config path"))
return recordUID, err
}
searchResults, err := RunCommand("keeper", "search", searchTerm, "--config", configPath)
if err != nil {
return recordUID, err
}
regex := regexp.MustCompile(`UID:\s+([a-zA-Z0-9-_]+)`)
match := regex.FindStringSubmatch(searchResults)
if len(match) == 2 {
recordUID = match[1]
} else {
fmt.Println("No UID found.")
}
return recordUID, nil
}