diff --git a/cmd/fabric-ca-client/main_test.go b/cmd/fabric-ca-client/main_test.go index 6c2d96c9d..7932c58a0 100644 --- a/cmd/fabric-ca-client/main_test.go +++ b/cmd/fabric-ca-client/main_test.go @@ -846,6 +846,15 @@ func testRegisterCommandLine(t *testing.T, srv *lib.Server) { assert.NoError(t, err) assert.Equal(t, "user", user.Type, "Identity type for '%s' should have been 'user'", userName) + // Register an identity with a space in its name + userName = "Test Register5" + err = RunMain([]string{cmdName, "register", "-d", "--id.name", userName, + "--id.affiliation", "hyperledger.org1"}) + assert.NoError(t, err, "Failed to register identity "+userName) + user, err = db.GetUserInfo(userName) + assert.NoError(t, err) + assert.Equal(t, "user", user.Type, "Identity type for '%s' should have been 'user'", userName) + os.Remove(defYaml) // Delete default config file err = RunMain([]string{cmdName, "register", "-u", "http://localhost:7091"}) diff --git a/lib/identity.go b/lib/identity.go index fca15f862..a1f665bbf 100644 --- a/lib/identity.go +++ b/lib/identity.go @@ -100,7 +100,7 @@ func (i *Identity) Register(req *api.RegistrationRequest) (rr *api.RegistrationR return nil, err } - log.Debug("The register request completely successfully") + log.Debug("The register request completed successfully") return resp, nil } diff --git a/lib/server.go b/lib/server.go index a30ada857..bd474eb07 100644 --- a/lib/server.go +++ b/lib/server.go @@ -62,6 +62,7 @@ const ( attrIntermediateCA = "hf.IntermediateCA" attrGenCRL = "hf.GenCRL" attrRegistrarAttr = "hf.Registrar.Attributes" + apiPathPrefix = "/api/v1/" ) // Server is the fabric-ca server @@ -479,7 +480,7 @@ func (s *Server) registerHandlers() { // Register a handler func (s *Server) registerHandler(path string, se *serverEndpoint) { s.mux.Handle("/"+path, se) - s.mux.Handle("/api/v1/"+path, se) + s.mux.Handle(apiPathPrefix+path, se) } // Starting listening and serving diff --git a/lib/serverendpoint.go b/lib/serverendpoint.go index ab23141cf..7d4253ff9 100644 --- a/lib/serverendpoint.go +++ b/lib/serverendpoint.go @@ -27,6 +27,8 @@ import ( type serverEndpoint struct { // The HTTP methods ("GET", "POST", etc) which the function will handle Methods []string + // The HTTP return code for success responses + successRC int // Handler is the handler function for this endpoint Handler func(ctx *serverRequestContext) (interface{}, error) // Server which hosts this endpoint @@ -50,16 +52,16 @@ func (se *serverEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(he.scode) log.Infof(`%s %s %s %d %d "%s"`, r.RemoteAddr, r.Method, r.URL, he.scode, he.lcode, he.lmsg) } else { - w.WriteHeader(200) - log.Infof(`%s %s %s 200 0 "OK"`, r.RemoteAddr, r.Method, r.URL) + w.WriteHeader(se.getSuccessRC()) + log.Infof(`%s %s %s %d 0 "OK"`, r.RemoteAddr, r.Method, r.URL, se.getSuccessRC()) } } else if err == nil { - w.WriteHeader(200) + w.WriteHeader(se.getSuccessRC()) err = api.SendResponse(w, resp) if err != nil { log.Warning("Failed to send response for %s: %+v", url, err) } - log.Infof(`%s %s %s 200 0 "OK"`, r.RemoteAddr, r.Method, r.URL) + log.Infof(`%s %s %s %d 0 "OK"`, r.RemoteAddr, r.Method, r.URL, se.getSuccessRC()) } else { he := getHTTPErr(err) he.writeResponse(w) @@ -68,6 +70,13 @@ func (se *serverEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +func (se *serverEndpoint) getSuccessRC() int { + if se.successRC == 0 { + return 200 + } + return se.successRC +} + // Validate that the HTTP method is supported for this endpoint func (se *serverEndpoint) validateMethod(r *http.Request) error { for _, m := range se.Methods { diff --git a/lib/serverenroll.go b/lib/serverenroll.go index 1c1e5f6e2..09b6f716a 100644 --- a/lib/serverenroll.go +++ b/lib/serverenroll.go @@ -63,17 +63,19 @@ type enrollmentResponseNet struct { func newEnrollEndpoint(s *Server) *serverEndpoint { return &serverEndpoint{ - Methods: []string{"POST"}, - Handler: enrollHandler, - Server: s, + Methods: []string{"POST"}, + Handler: enrollHandler, + Server: s, + successRC: 201, } } func newReenrollEndpoint(s *Server) *serverEndpoint { return &serverEndpoint{ - Methods: []string{"POST"}, - Handler: reenrollHandler, - Server: s, + Methods: []string{"POST"}, + Handler: reenrollHandler, + Server: s, + successRC: 201, } } diff --git a/lib/serverregister.go b/lib/serverregister.go index be82bdec8..89b4e262b 100644 --- a/lib/serverregister.go +++ b/lib/serverregister.go @@ -18,6 +18,7 @@ package lib import ( "fmt" + "net/url" "strings" "github.com/pkg/errors" @@ -31,9 +32,10 @@ import ( func newRegisterEndpoint(s *Server) *serverEndpoint { return &serverEndpoint{ - Methods: []string{"POST"}, - Handler: registerHandler, - Server: s, + Methods: []string{"POST"}, + Handler: registerHandler, + Server: s, + successRC: 201, } } @@ -106,7 +108,8 @@ func registerUser(req *api.RegistrationRequestNet, registrar string, ca *CA, ctx if err != nil { return "", errors.WithMessage(err, fmt.Sprintf("Registration of '%s' failed", req.Name)) } - + // Set the location header to the URI of the identity that was created by the registration request + ctx.resp.Header().Set("Location", fmt.Sprintf("%sidentities/%s", apiPathPrefix, url.PathEscape(req.Name))) return secret, nil } diff --git a/swagger/swagger-fabric-ca.json b/swagger/swagger-fabric-ca.json index c8aee4560..2ce13b556 100644 --- a/swagger/swagger-fabric-ca.json +++ b/swagger/swagger-fabric-ca.json @@ -200,7 +200,7 @@ } ], "responses": { - "200": { + "201": { "description": "Successfully enrolled a new identity", "schema": { "type": "object", @@ -347,7 +347,7 @@ } ], "responses": { - "200": { + "201": { "description": "Successfully reenrolled identity", "schema": { "type": "object",