-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
221 lines (194 loc) · 6 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
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
/*
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.
*/
package lib
import (
"errors"
"fmt"
"net/http"
"github.com/cloudflare/cfssl/log"
"github.com/hyperledger/fabric-ca/api"
"github.com/hyperledger/fabric-ca/util"
"github.com/hyperledger/fabric/bccsp"
)
func newIdentity(client *Client, name string, key bccsp.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 = util.GetDefaultBCCSP()
}
return id
}
// Identity is fabric-ca's implementation of an identity
type Identity struct {
name string
ecert *Signer
client *Client
CSP bccsp.BCCSP
}
// GetName returns the identity name
func (i *Identity) GetName() string {
return i.name
}
// GetClient returns the client associated with this identity
func (i *Identity) GetClient() *Client {
return i.client
}
// GetECert returns the enrollment certificate signer for this identity
func (i *Identity) GetECert() *Signer {
return i.ecert
}
// GetTCertBatch returns a batch of TCerts for this identity
func (i *Identity) GetTCertBatch(req *api.GetTCertBatchRequest) ([]*Signer, error) {
reqBody, err := util.Marshal(req, "GetTCertBatchRequest")
if err != nil {
return nil, err
}
err = i.Post("tcert", reqBody, nil)
if err != nil {
return nil, err
}
// Ignore the contents of the response for now. They will be processed in the future when we need to
// support the Go SDK. We currently have Node and Java SDKs which process this and they are the
// priority.
return nil, nil
}
// 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")
}
if req.Affiliation == "" {
return nil, errors.New("Registration request does not have an affiliation")
}
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)
if err != nil {
return nil, err
}
log.Debug("The register request completely successfully")
return resp, nil
}
// RegisterAndEnroll registers and enrolls an identity and returns the identity
func (i *Identity) RegisterAndEnroll(req *api.RegistrationRequest) (*Identity, error) {
if i.client == nil {
return nil, errors.New("No client is associated with this identity")
}
rresp, err := i.Register(req)
if err != nil {
return nil, fmt.Errorf("Failed to register %s: %s", req.Name, err)
}
eresp, err := i.client.Enroll(&api.EnrollmentRequest{
Name: req.Name,
Secret: rresp.Secret,
})
if err != nil {
return nil, fmt.Errorf("Failed to enroll %s: %s", req.Name, err)
}
return eresp.Identity, 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", req)
csrPEM, key, err := i.client.GenCSR(req.CSR, i.GetName())
if err != nil {
return nil, err
}
reqNet := &api.ReenrollmentRequestNet{
CAName: req.CAName,
}
// 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)
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) error {
log.Debugf("Entering identity.Revoke %+v", req)
reqBody, err := util.Marshal(req, "RevocationRequest")
if err != nil {
return err
}
err = i.Post("revoke", reqBody, nil)
if err != nil {
return err
}
log.Debugf("Successfully revoked %+v", req)
return nil
}
// RevokeSelf revokes the current identity and all certificates
func (i *Identity) RevokeSelf() error {
name := i.GetName()
log.Debugf("RevokeSelf %s", name)
req := &api.RevocationRequest{
Name: name,
}
return i.Revoke(req)
}
// Store writes my identity info to disk
func (i *Identity) Store() error {
if i.client == nil {
return fmt.Errorf("An identity with no client may not be stored")
}
return i.client.StoreMyIdentity(i.ecert.cert)
}
// Post sends arbtrary 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{}) error {
req, err := i.client.newPost(endpoint, reqBody)
if err != nil {
return err
}
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 fmt.Errorf("Failed to add token authorization header: %s", err)
}
req.Header.Set("authorization", token)
return nil
}