forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
175 lines (152 loc) · 4.95 KB
/
identity.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/
package lib
import (
"net/http"
"github.com/pkg/errors"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/api"
log "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/sdkpatch/logbridge"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/util"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
)
func newIdentity(client *Client, name string, key core.Key, cert []byte) *Identity {
id := new(Identity)
id.name = name
id.ecert = newSigner(key, cert, id)
id.client = client
if client != nil {
id.CSP = client.csp
} else {
id.CSP = nil
}
return id
}
// Identity is fabric-ca's implementation of an identity
type Identity struct {
name string
ecert *Signer
client *Client
CSP core.CryptoSuite
}
// GetName returns the identity name
func (i *Identity) GetName() string {
return i.name
}
// GetECert returns the enrollment certificate signer for this identity
func (i *Identity) GetECert() *Signer {
return i.ecert
}
// Register registers a new identity
// @param req The registration request
func (i *Identity) Register(req *api.RegistrationRequest) (rr *api.RegistrationResponse, err error) {
log.Debugf("Register %+v", req)
if req.Name == "" {
return nil, errors.New("Register was called without a Name set")
}
reqBody, err := util.Marshal(req, "RegistrationRequest")
if err != nil {
return nil, err
}
// Send a post to the "register" endpoint with req as body
resp := &api.RegistrationResponse{}
err = i.Post("register", reqBody, resp, nil)
if err != nil {
return nil, err
}
log.Debug("The register request completed successfully")
return resp, nil
}
// Reenroll reenrolls an existing Identity and returns a new Identity
// @param req The reenrollment request
func (i *Identity) Reenroll(req *api.ReenrollmentRequest) (*EnrollmentResponse, error) {
log.Debugf("Reenrolling %s", util.StructToString(req))
csrPEM, key, err := i.client.GenCSR(req.CSR, i.GetName())
if err != nil {
return nil, err
}
reqNet := &api.ReenrollmentRequestNet{
CAName: req.CAName,
AttrReqs: req.AttrReqs,
}
// Get the body of the request
if req.CSR != nil {
reqNet.SignRequest.Hosts = req.CSR.Hosts
}
reqNet.SignRequest.Request = string(csrPEM)
reqNet.SignRequest.Profile = req.Profile
reqNet.SignRequest.Label = req.Label
body, err := util.Marshal(reqNet, "SignRequest")
if err != nil {
return nil, err
}
var result enrollmentResponseNet
err = i.Post("reenroll", body, &result, nil)
if err != nil {
return nil, err
}
return i.client.newEnrollmentResponse(&result, i.GetName(), key)
}
// Revoke the identity associated with 'id'
func (i *Identity) Revoke(req *api.RevocationRequest) (*api.RevocationResponse, error) {
log.Debugf("Entering identity.Revoke %+v", req)
reqBody, err := util.Marshal(req, "RevocationRequest")
if err != nil {
return nil, err
}
var result revocationResponseNet
err = i.Post("revoke", reqBody, &result, nil)
if err != nil {
return nil, err
}
log.Debugf("Successfully revoked certificates: %+v", req)
crl, err := util.B64Decode(result.CRL)
if err != nil {
return nil, err
}
return &api.RevocationResponse{RevokedCerts: result.RevokedCerts, CRL: crl}, nil
}
// Post sends arbitrary request body (reqBody) to an endpoint.
// This adds an authorization header which contains the signature
// of this identity over the body and non-signature part of the authorization header.
// The return value is the body of the response.
func (i *Identity) Post(endpoint string, reqBody []byte, result interface{}, queryParam map[string]string) error {
req, err := i.client.newPost(endpoint, reqBody)
if err != nil {
return err
}
if queryParam != nil {
for key, value := range queryParam {
addQueryParm(req, key, value)
}
}
err = i.addTokenAuthHdr(req, reqBody)
if err != nil {
return err
}
return i.client.SendReq(req, result)
}
func (i *Identity) addTokenAuthHdr(req *http.Request, body []byte) error {
log.Debug("Adding token-based authorization header")
cert := i.ecert.cert
key := i.ecert.key
token, err := util.CreateToken(i.CSP, cert, key, body)
if err != nil {
return errors.WithMessage(err, "Failed to add token authorization header")
}
req.Header.Set("authorization", token)
return nil
}