-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.go
155 lines (113 loc) · 3.52 KB
/
User.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
package HLF_ABAC
import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)
type User struct {
UserID string `json:"userID"`
UserName string `json:"userName"`
Attributes map[string]string `json:"attributes"`
}
func GetSubmittingClientIdentity(ctx contractapi.TransactionContextInterface) (string, error) {
b64ID, err := ctx.GetClientIdentity().GetID()
if err != nil {
return "", fmt.Errorf("Failed to read clientID: %v", err)
}
decodeID, err := base64.StdEncoding.DecodeString(b64ID)
if err != nil {
return "", fmt.Errorf("failed to base64 decode clientID: %v", err)
}
return string(decodeID), nil
}
func GetSubmittingClientPubKey(ctx contractapi.TransactionContextInterface) (string, error) {
Cert, err := ctx.GetClientIdentity().GetX509Certificate()
if err != nil {
return "", fmt.Errorf("Failed to read clientID: %v", err)
}
pubKey := Cert["Certificate"]["Data"]["pub"]
if pubKey != nil {
return "", fmt.Errorf("failed to get client public key")
}
return pubKey, nil
}
func getUAPriv(ctx contractapi.TransactionContextInterface, attrName string) (string, error) {
attrValue, found, err := ctx.GetClientIdentity().GetAttributeValue(ctx.GetStub(), attrName)
if !found {
return "", fmt.Errorf("No attribute with given name")
}
if err {
return "", err
}
return attrValue, nil
}
func registerUAPub(ctx contractapi.TransactionContextInterface, userID string, userName string, attributes string) (string, error) {
err := ctx.GetClientIdentity().AssertAttributeValue(ctx.GetStub(), "admin", "true")
if err {
return "", fmt.Errorf("Invoking client is not an Admin")
}
if len(userID) == 0 {
return "", fmt.Errorf("Please enter valid Subject ID")
}
attrs := strings.Split(attributes, ",")
var attrsmap = make(map[string]string)
for i := 0; i < len(attrs); i++ {
attrPair := strings.Split(attrs[i], ":")
attrsmap[attrPair[0]] = attrPair[1]
}
user := User{
UserID: userID,
UserName: userName,
Attributes: attrsmap,
}
userJSON, err := json.Marshal(user)
if err != nil {
return "", err
}
ctx.GetStub().SetEvent("registerUAPub", userJSON)
return ctx.GetStub().GetTxID(), ctx.GetStub().PutState(userID, userJSON)
}
func updateUAPub(ctx contractapi.TransactionContextInterface, userID string, userName string, attributes string) (string, error) {
err := ctx.GetClientIdentity().AssertAttributeValue(ctx.GetStub(), "admin", "true")
if err {
return "", fmt.Errorf("Invoking client is not an Admin")
}
if len(userID) == 0 {
return "", fmt.Errorf("Please enter valid Subject ID")
}
userJSON, err := ctx.GetStub().GetState(userID)
if err || userJSON == nil {
return "", fmt.Errorf("Attributes for given user not present")
}
attrs := strings.Split(attributes, ",")
var attrsmap = make(map[string]string)
for i := 0; i < len(attrs); i++ {
attrPair := strings.Split(attrs[i], ":")
attrsmap[attrPair[0]] = attrPair[1]
}
user := User{
UserID: userID,
UserName: userName,
Attributes: attrsmap,
}
userJSON, err = json.Marshal(user)
if err != nil {
return "", err
}
ctx.GetStub().SetEvent("updateUAPub", userJSON)
return ctx.GetStub().GetTxID(), ctx.GetStub().PutState(userID, userJSON)
}
func getUAPub(ctx contractapi.TransactionContextInterface, userID string) *User {
if len(userID) == 0 {
return nil
}
userJSON, err := ctx.GetStub().GetState(userID)
if err != nil || userJSON == nil {
return nil
}
user := new(User)
_ = json.Unmarshal(userJSON, user)
return user
}