forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certfileuserstore.go
78 lines (68 loc) · 2.23 KB
/
certfileuserstore.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package msp
import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/keyvaluestore"
"github.com/pkg/errors"
)
// CertFileUserStore stores each user in a separate file.
// Only user's enrollment cert is stored, in pem format.
// File naming is <user>@<org>-cert.pem
type CertFileUserStore struct {
store core.KVStore
}
func storeKeyFromUserIdentifier(key msp.IdentityIdentifier) string {
return key.ID + "@" + key.MSPID + "-cert.pem"
}
// NewCertFileUserStore1 creates a new instance of CertFileUserStore
func NewCertFileUserStore1(store core.KVStore) (*CertFileUserStore, error) {
return &CertFileUserStore{
store: store,
}, nil
}
// NewCertFileUserStore creates a new instance of CertFileUserStore
func NewCertFileUserStore(path string) (*CertFileUserStore, error) {
if path == "" {
return nil, errors.New("path is empty")
}
store, err := keyvaluestore.New(&keyvaluestore.FileKeyValueStoreOptions{
Path: path,
})
if err != nil {
return nil, errors.WithMessage(err, "user store creation failed")
}
return NewCertFileUserStore1(store)
}
// Load returns the User stored in the store for a key.
func (s *CertFileUserStore) Load(key msp.IdentityIdentifier) (*msp.UserData, error) {
cert, err := s.store.Load(storeKeyFromUserIdentifier(key))
if err != nil {
if err == core.ErrKeyValueNotFound {
return nil, msp.ErrUserNotFound
}
return nil, err
}
certBytes, ok := cert.([]byte)
if !ok {
return nil, errors.New("user is not of proper type")
}
userData := &msp.UserData{
MSPID: key.MSPID,
ID: key.ID,
EnrollmentCertificate: certBytes,
}
return userData, nil
}
// Store stores a User into store
func (s *CertFileUserStore) Store(user *msp.UserData) error {
key := storeKeyFromUserIdentifier(msp.IdentityIdentifier{MSPID: user.MSPID, ID: user.ID})
return s.store.Store(key, user.EnrollmentCertificate)
}
// Delete deletes a User from store
func (s *CertFileUserStore) Delete(key msp.IdentityIdentifier) error {
return s.store.Delete(storeKeyFromUserIdentifier(key))
}