-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpass.go
184 lines (157 loc) · 3.69 KB
/
pass.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
180
181
182
183
184
package main
import (
"bufio"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/user"
"path"
"path/filepath"
"strings"
"github.com/proglottis/gpgme"
"github.com/rjeczalik/notify"
)
// PasswordStore keeps track of all the passwords
type PasswordStore struct {
passwords []Password
Prefix string
subscribers []Subscriber
}
// Subscriber is a callback for changes in the PasswordStore
type Subscriber func(status string)
// A Password entry in Passwords
type Password struct {
Name string
Path string
}
func (p *Password) decrypt() (io.Reader, error) {
gpgmeMutex.Lock()
defer gpgmeMutex.Unlock()
file, _ := os.Open(p.Path)
defer file.Close()
return gpgme.Decrypt(file)
}
// Raw returns the password in encrypted form
func (p *Password) Raw() string {
file, _ := os.Open(p.Path)
defer file.Close()
data, _ := ioutil.ReadAll(file)
return base64.StdEncoding.EncodeToString(data)
}
// Metadata of the password
func (p *Password) Metadata() string {
out, _ := p.decrypt()
nr := bufio.NewReader(out)
nr.ReadString('\n')
metadata, _ := nr.ReadString('\003')
return metadata
}
func (p *Password) Password() string {
decrypted, _ := p.decrypt()
nr := bufio.NewReader(decrypted)
password, _ := nr.ReadString('\n')
return password
}
// NewPasswordStore creates a new password store
func NewPasswordStore() *PasswordStore {
ps := new(PasswordStore)
path, err := findPasswordStore()
if err != nil {
log.Fatal(err)
}
ps.Prefix = path
ps.indexAll()
ps.watch()
return ps
}
// Query the PasswordStore
func (ps *PasswordStore) Query(q string) []Password {
var hits []Password
for _, p := range ps.passwords {
if match(q, p.Name) {
hits = append(hits, p)
}
}
return hits
}
// Subscribe starts calling cb when anything in the PasswordStore changes
func (ps *PasswordStore) Subscribe(cb Subscriber) {
ps.subscribers = append(ps.subscribers, cb)
}
func (ps *PasswordStore) publishUpdate(status string) {
for _, s := range ps.subscribers {
s(status)
}
}
func match(query, candidate string) bool {
lowerQuery := strings.ToLower(query)
queryParts := strings.Split(lowerQuery, " ")
lowerCandidate := strings.ToLower(candidate)
for _, p := range queryParts {
if !strings.Contains(
strings.ToLower(lowerCandidate),
strings.ToLower(p),
) {
return false
}
}
return true
}
func (ps *PasswordStore) indexFile(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".gpg") {
name := strings.TrimPrefix(path, ps.Prefix)
name = strings.TrimSuffix(name, ".gpg")
name = strings.TrimPrefix(name, "/")
const MaxLen = 40
if len(name) > MaxLen {
name = "..." + name[len(name)-MaxLen:]
}
ps.add(Password{Name: name, Path: path})
}
return nil
}
func (ps *PasswordStore) indexAll() {
filepath.Walk(ps.Prefix, ps.indexFile)
}
func (ps *PasswordStore) watch() {
c := make(chan notify.EventInfo, 1)
if err := notify.Watch(ps.Prefix+"/...", c, notify.All); err != nil {
log.Fatal(err)
}
go func() {
for {
<-c
ps.indexAll()
}
}()
}
func (ps *PasswordStore) add(p Password) {
ps.passwords = append(ps.passwords, p)
ps.publishUpdate(fmt.Sprintf("Indexed %d entries", len(ps.passwords)))
}
func findPasswordStore() (string, error) {
var homeDir string
if usr, err := user.Current(); err == nil {
homeDir = usr.HomeDir
}
pathCandidates := []string{
os.Getenv("PASSWORD_STORE_DIR"),
path.Join(homeDir, ".password-store"),
path.Join(homeDir, "password-store"),
}
for _, p := range pathCandidates {
var err error
if p, err = filepath.EvalSymlinks(p); err != nil {
continue
}
if _, err = os.Stat(p); err != nil {
continue
}
return p, nil
}
return "", errors.New("Couldn't find a valid password store")
}