aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core/token.go
blob: 28c64f5c4f9ea5edc7fee5fb8f7d01463e58923a (plain) (blame)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package core

import (
	"crypto/sha256"
	"errors"
	"fmt"
	"regexp"
	"sort"
	"strings"
	"time"

	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/repository"
)

const (
	tokenConfigKeyPrefix = "git-bug.token"
	tokenValueKey        = "value"
	tokenTargetKey       = "target"
	tokenCreateTimeKey   = "createtime"
)

var ErrTokenNotExist = errors.New("token doesn't exist")

func NewErrMultipleMatchToken(matching []entity.Id) *entity.ErrMultipleMatch {
	return entity.NewErrMultipleMatch("token", matching)
}

// Token holds an API access token data
type Token struct {
	Value      string
	Target     string
	CreateTime time.Time
}

// NewToken instantiate a new token
func NewToken(value, target string) *Token {
	return &Token{
		Value:      value,
		Target:     target,
		CreateTime: time.Now(),
	}
}

func (t *Token) ID() entity.Id {
	sum := sha256.Sum256([]byte(t.Target + t.Value))
	return entity.Id(fmt.Sprintf("%x", sum))
}

// Validate ensure token important fields are valid
func (t *Token) Validate() error {
	if t.Value == "" {
		return fmt.Errorf("missing value")
	}
	if t.Target == "" {
		return fmt.Errorf("missing target")
	}
	if t.CreateTime.IsZero() || t.CreateTime.Equal(time.Time{}) {
		return fmt.Errorf("missing creation time")
	}
	if !TargetExist(t.Target) {
		return fmt.Errorf("unknown target")
	}
	return nil
}

// LoadToken loads a token from the repo config
func LoadToken(repo repository.RepoCommon, id entity.Id) (*Token, error) {
	keyPrefix := fmt.Sprintf("git-bug.token.%s.", id)

	// read token config pairs
	rawconfigs, err := repo.GlobalConfig().ReadAll(keyPrefix)
	if err != nil {
		// Not exactly right due to the limitation of ReadAll()
		return nil, ErrTokenNotExist
	}

	// trim key prefix
	configs := make(map[string]string)
	for key, value := range rawconfigs {
		newKey := strings.TrimPrefix(key, keyPrefix)
		configs[newKey] = value
	}

	token := &Token{}

	token.Value = configs[tokenValueKey]
	token.Target = configs[tokenTargetKey]
	if createTime, ok := configs[tokenCreateTimeKey]; ok {
		if t, err := repository.ParseTimestamp(createTime); err == nil {
			token.CreateTime = t
		}
	}

	return token, nil
}

// LoadTokenPrefix load a token from the repo config with a prefix
func LoadTokenPrefix(repo repository.RepoCommon, prefix string) (*Token, error) {
	tokens, err := ListTokens(repo)
	if err != nil {
		return nil, err
	}

	// preallocate but empty
	matching := make([]entity.Id, 0, 5)

	for _, id := range tokens {
		if id.HasPrefix(prefix) {
			matching = append(matching, id)
		}
	}

	if len(matching) > 1 {
		return nil, NewErrMultipleMatchToken(matching)
	}

	if len(matching) == 0 {
		return nil, ErrTokenNotExist
	}

	return LoadToken(repo, matching[0])
}

// ListTokens list all existing token ids
func ListTokens(repo repository.RepoCommon) ([]entity.Id, error) {
	configs, err := repo.GlobalConfig().ReadAll(tokenConfigKeyPrefix + ".")
	if err != nil {
		return nil, err
	}

	re, err := regexp.Compile(tokenConfigKeyPrefix + `.([^.]+)`)
	if err != nil {
		panic(err)
	}

	set := make(map[string]interface{})

	for key := range configs {
		res := re.FindStringSubmatch(key)

		if res == nil {
			continue
		}

		set[res[1]] = nil
	}

	result := make([]entity.Id, 0, len(set))
	for key := range set {
		result = append(result, entity.Id(key))
	}

	sort.Sort(entity.Alphabetical(result))

	return result, nil
}

// ListTokensWithTarget list all token ids associated with the target
func ListTokensWithTarget(repo repository.RepoCommon, target string) ([]entity.Id, error) {
	var ids []entity.Id
	tokensIds, err := ListTokens(repo)
	if err != nil {
		return nil, err
	}

	for _, tokenId := range tokensIds {
		token, err := LoadToken(repo, tokenId)
		if err != nil {
			return nil, err
		}

		if token.Target == target {
			ids = append(ids, tokenId)
		}
	}
	return ids, nil
}

// LoadTokens load all existing tokens
func LoadTokens(repo repository.RepoCommon) ([]*Token, error) {
	tokensIds, err := ListTokens(repo)
	if err != nil {
		return nil, err
	}

	var tokens []*Token
	for _, id := range tokensIds {
		token, err := LoadToken(repo, id)
		if err != nil {
			return nil, err
		}
		tokens = append(tokens, token)
	}
	return tokens, nil
}

// LoadTokensWithTarget load all existing tokens for a given target
func LoadTokensWithTarget(repo repository.RepoCommon, target string) ([]*Token, error) {
	tokensIds, err := ListTokens(repo)
	if err != nil {
		return nil, err
	}

	var tokens []*Token
	for _, id := range tokensIds {
		token, err := LoadToken(repo, id)
		if err != nil {
			return nil, err
		}
		if token.Target == target {
			tokens = append(tokens, token)
		}
	}
	return tokens, nil
}

// TokenIdExist return wether token id exist or not
func TokenIdExist(repo repository.RepoCommon, id entity.Id) bool {
	_, err := LoadToken(repo, id)
	return err == nil
}

// TokenExist return wether there is a token with a certain value or not
func TokenExist(repo repository.RepoCommon, value string) bool {
	tokens, err := LoadTokens(repo)
	if err != nil {
		return false
	}
	for _, token := range tokens {
		if token.Value == value {
			return true
		}
	}
	return false
}

// TokenExistWithTarget same as TokenExist but restrict search for a given target
func TokenExistWithTarget(repo repository.RepoCommon, value string, target string) bool {
	tokens, err := LoadTokensWithTarget(repo, target)
	if err != nil {
		return false
	}
	for _, token := range tokens {
		if token.Value == value {
			return true
		}
	}
	return false
}

// StoreToken stores a token in the repo config
func StoreToken(repo repository.RepoCommon, token *Token) error {
	storeValueKey := fmt.Sprintf("git-bug.token.%s.%s", token.ID().String(), tokenValueKey)
	err := repo.GlobalConfig().StoreString(storeValueKey, token.Value)
	if err != nil {
		return err
	}

	storeTargetKey := fmt.Sprintf("git-bug.token.%s.%s", token.ID().String(), tokenTargetKey)
	err = repo.GlobalConfig().StoreString(storeTargetKey, token.Target)
	if err != nil {
		return err
	}

	createTimeKey := fmt.Sprintf("git-bug.token.%s.%s", token.ID().String(), tokenCreateTimeKey)
	return repo.GlobalConfig().StoreTimestamp(createTimeKey, token.CreateTime)
}

// RemoveToken removes a token from the repo config
func RemoveToken(repo repository.RepoCommon, id entity.Id) error {
	keyPrefix := fmt.Sprintf("git-bug.token.%s", id)
	return repo.GlobalConfig().RemoveAll(keyPrefix)
}

// LoadOrCreateToken will try to load a token matching the same value or create it
func LoadOrCreateToken(repo repository.RepoCommon, target, tokenValue string) (*Token, error) {
	tokens, err := LoadTokensWithTarget(repo, target)
	if err != nil {
		return nil, err
	}

	for _, token := range tokens {
		if token.Value == tokenValue {
			return token, nil
		}
	}

	token := NewToken(tokenValue, target)
	err = StoreToken(repo, token)
	if err != nil {
		return nil, err
	}

	return token, nil
}