-
Notifications
You must be signed in to change notification settings - Fork 3
/
truststore.go
124 lines (103 loc) · 2.76 KB
/
truststore.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
package jess
import (
"errors"
"fmt"
"sync"
)
// TrustStore filter options.
const (
FilterAny uint8 = iota
FilterSignetOnly
FilterRecipientOnly
)
// TrustStore errors.
var (
ErrSignetNotFound = errors.New("could not find signet")
ErrEnvelopeNotFound = errors.New("could not find envelope")
)
// TrustStore holds a set of trusted Signets and Recipients.
type TrustStore interface {
// GetSignet returns the Signet with the given ID.
GetSignet(id string, recipient bool) (*Signet, error)
}
// MemTrustStore is a simple trust store using a Go map as backend.
type MemTrustStore struct {
lock sync.Mutex
storage map[string]*Signet
}
// GetSignet returns the Signet with the given ID.
func (mts *MemTrustStore) GetSignet(id string, recipient bool) (*Signet, error) {
mts.lock.Lock()
defer mts.lock.Unlock()
// get from storage
signet, ok := mts.storage[makeStorageID(id, recipient)]
if !ok {
return nil, ErrSignetNotFound
}
return signet, nil
}
// StoreSignet stores a Signet in the TrustStore.
func (mts *MemTrustStore) StoreSignet(signet *Signet) error {
// check for ID
if signet.ID == "" {
return errors.New("signets require an ID to be stored in a trust store")
}
mts.lock.Lock()
defer mts.lock.Unlock()
// store
mts.storage[makeStorageID(signet.ID, signet.Public)] = signet
return nil
}
// DeleteSignet deletes the Signet or Recipient with the given ID.
func (mts *MemTrustStore) DeleteSignet(id string, recipient bool) error {
mts.lock.Lock()
defer mts.lock.Unlock()
// delete
delete(mts.storage, makeStorageID(id, recipient))
return nil
}
// SelectSignets returns a selection of the signets in the trust store. Results are filtered by tool/algorithm and whether it you're looking for a signet (private key) or a recipient (public key).
func (mts *MemTrustStore) SelectSignets(filter uint8, schemes ...string) ([]*Signet, error) {
mts.lock.Lock()
defer mts.lock.Unlock()
var selection []*Signet //nolint:prealloc
for _, signet := range mts.storage {
// check signet scheme
if len(schemes) > 0 && !stringInSlice(signet.Scheme, schemes) {
return nil, nil
}
// check type filter
switch filter {
case FilterSignetOnly:
if signet.Public {
continue
}
case FilterRecipientOnly:
if !signet.Public {
continue
}
}
selection = append(selection, signet)
}
return selection, nil
}
// NewMemTrustStore returns a new in-memory TrustStore.
func NewMemTrustStore() *MemTrustStore {
return &MemTrustStore{
storage: make(map[string]*Signet),
}
}
func makeStorageID(id string, recipient bool) string {
if recipient {
return fmt.Sprintf("%s.recipient", id)
}
return fmt.Sprintf("%s.signet", id)
}
func stringInSlice(s string, a []string) bool {
for _, entry := range a {
if entry == s {
return true
}
}
return false
}