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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
package auth
import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/git-bug/git-bug/entity"
"github.com/git-bug/git-bug/repository"
)
const (
keyringKeyPrefix = "auth-"
keyringKeyKind = "kind"
keyringKeyTarget = "target"
keyringKeyCreateTime = "createtime"
keyringKeySalt = "salt"
keyringKeyPrefixMeta = "meta."
MetaKeyLogin = "login"
MetaKeyBaseURL = "base-url"
)
type CredentialKind string
const (
KindToken CredentialKind = "token"
KindLogin CredentialKind = "login"
KindLoginPassword CredentialKind = "login-password"
)
var ErrCredentialNotExist = errors.New("credential doesn't exist")
func NewErrMultipleMatchCredential(matching []entity.Id) *entity.ErrMultipleMatch {
return entity.NewErrMultipleMatch("credential", matching)
}
type Credential interface {
ID() entity.Id
Kind() CredentialKind
Target() string
CreateTime() time.Time
Salt() []byte
Validate() error
Metadata() map[string]string
GetMetadata(key string) (string, bool)
SetMetadata(key string, value string)
// Return all the specific properties of the credential that need to be saved into the configuration.
// This does not include Target, Kind, CreateTime, Metadata or Salt.
toConfig() map[string]string
}
// Load loads a credential from the repo config
func LoadWithId(repo repository.RepoKeyring, id entity.Id) (Credential, error) {
key := fmt.Sprintf("%s%s", keyringKeyPrefix, id)
item, err := repo.Keyring().Get(key)
if err == repository.ErrKeyringKeyNotFound {
return nil, ErrCredentialNotExist
}
if err != nil {
return nil, err
}
return decode(item)
}
// LoadWithPrefix load a credential from the repo config with a prefix
func LoadWithPrefix(repo repository.RepoKeyring, prefix string) (Credential, error) {
keys, err := repo.Keyring().Keys()
if err != nil {
return nil, err
}
// preallocate but empty
matching := make([]Credential, 0, 5)
for _, key := range keys {
if !strings.HasPrefix(key, keyringKeyPrefix+prefix) {
continue
}
item, err := repo.Keyring().Get(key)
if err != nil {
return nil, err
}
cred, err := decode(item)
if err != nil {
return nil, err
}
matching = append(matching, cred)
}
if len(matching) > 1 {
ids := make([]entity.Id, len(matching))
for i, cred := range matching {
ids[i] = cred.ID()
}
return nil, NewErrMultipleMatchCredential(ids)
}
if len(matching) == 0 {
return nil, ErrCredentialNotExist
}
return matching[0], nil
}
// decode is a helper to construct a Credential from the keyring Item
func decode(item repository.Item) (Credential, error) {
data := make(map[string]string)
err := json.Unmarshal(item.Data, &data)
if err != nil {
return nil, err
}
var cred Credential
switch CredentialKind(data[keyringKeyKind]) {
case KindToken:
cred, err = NewTokenFromConfig(data)
case KindLogin:
cred, err = NewLoginFromConfig(data)
case KindLoginPassword:
cred, err = NewLoginPasswordFromConfig(data)
default:
return nil, fmt.Errorf("unknown credential type \"%s\"", data[keyringKeyKind])
}
if err != nil {
return nil, fmt.Errorf("loading credential: %v", err)
}
return cred, nil
}
// List load all existing credentials
func List(repo repository.RepoKeyring, opts ...ListOption) ([]Credential, error) {
keys, err := repo.Keyring().Keys()
if err != nil {
return nil, err
}
matcher := matcher(opts)
var credentials []Credential
for _, key := range keys {
if !strings.HasPrefix(key, keyringKeyPrefix) {
continue
}
item, err := repo.Keyring().Get(key)
if err != nil {
// skip unreadable items, nothing much we can do for them anyway
continue
}
cred, err := decode(item)
if err != nil {
return nil, err
}
if matcher.Match(cred) {
credentials = append(credentials, cred)
}
}
return credentials, nil
}
// IdExist return whether a credential id exist or not
func IdExist(repo repository.RepoKeyring, id entity.Id) bool {
_, err := LoadWithId(repo, id)
return err == nil
}
// PrefixExist return whether a credential id prefix exist or not
func PrefixExist(repo repository.RepoKeyring, prefix string) bool {
_, err := LoadWithPrefix(repo, prefix)
return err == nil
}
// Store stores a credential in the global git config
func Store(repo repository.RepoKeyring, cred Credential) error {
if len(cred.Salt()) != 16 {
panic("credentials need to be salted")
}
confs := cred.toConfig()
confs[keyringKeyKind] = string(cred.Kind())
confs[keyringKeyTarget] = cred.Target()
confs[keyringKeyCreateTime] = strconv.Itoa(int(cred.CreateTime().Unix()))
confs[keyringKeySalt] = base64.StdEncoding.EncodeToString(cred.Salt())
for key, val := range cred.Metadata() {
confs[keyringKeyPrefixMeta+key] = val
}
data, err := json.Marshal(confs)
if err != nil {
return err
}
return repo.Keyring().Set(repository.Item{
Key: keyringKeyPrefix + cred.ID().String(),
Data: data,
})
}
// Remove removes a credential from the global git config
func Remove(repo repository.RepoKeyring, id entity.Id) error {
return repo.Keyring().Remove(keyringKeyPrefix + id.String())
}
/*
* Sorting
*/
type ById []Credential
func (b ById) Len() int {
return len(b)
}
func (b ById) Less(i, j int) bool {
return b[i].ID() < b[j].ID()
}
func (b ById) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
|