From 242a8bee7ba7e78ed3f5ebcb73360288e4591c2a Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Tue, 20 Sep 2022 16:01:46 -0400 Subject: [PATCH] controller registration endpoint improvements (#50) --- controller/account.go | 2 +- controller/controller.go | 1 + controller/register.go | 59 +++++++++++++++++++ .../identity/register_responses.go | 16 ++++- rest_model_zrok/register_response.go | 50 ++++++++++++++++ rest_server_zrok/embedded_spec.go | 26 +++++++- .../operations/identity/register_responses.go | 24 +++++++- specs/zrok.yml | 7 +++ ui/src/api/identity.js | 2 +- ui/src/api/types.js | 7 +++ 10 files changed, 186 insertions(+), 8 deletions(-) create mode 100644 controller/register.go create mode 100644 rest_model_zrok/register_response.go diff --git a/controller/account.go b/controller/account.go index e59da0e7..a1f8a02e 100644 --- a/controller/account.go +++ b/controller/account.go @@ -17,11 +17,11 @@ func newCreateAccountHandler(cfg *Config) *createAccountHandler { } func (self *createAccountHandler) Handle(params identity.CreateAccountParams) middleware.Responder { - logrus.Infof("received account request for email '%v'", params.Body.Email) if params.Body == nil || params.Body.Email == "" { logrus.Errorf("missing email") return identity.NewCreateAccountBadRequest().WithPayload("missing email") } + logrus.Infof("received account request for email '%v'", params.Body.Email) token := createToken() ar := &store.AccountRequest{ diff --git a/controller/controller.go b/controller/controller.go index aea52ba8..8e11cd96 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -27,6 +27,7 @@ func Run(cfg *Config) error { api.IdentityEnableHandler = newEnableHandler(cfg) api.IdentityDisableHandler = newDisableHandler(cfg) api.IdentityLoginHandler = identity.LoginHandlerFunc(loginHandler) + api.IdentityRegisterHandler = newRegisterHandler() api.IdentityVerifyHandler = newVerifyHandler(cfg) api.MetadataOverviewHandler = metadata.OverviewHandlerFunc(overviewHandler) api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler) diff --git a/controller/register.go b/controller/register.go new file mode 100644 index 00000000..bf6db902 --- /dev/null +++ b/controller/register.go @@ -0,0 +1,59 @@ +package controller + +import ( + "github.com/go-openapi/runtime/middleware" + "github.com/openziti-test-kitchen/zrok/controller/store" + "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity" + "github.com/sirupsen/logrus" +) + +type registerHandler struct{} + +func newRegisterHandler() *registerHandler { + return ®isterHandler{} +} +func (self *registerHandler) Handle(params identity.RegisterParams) middleware.Responder { + if params.Body == nil || params.Body.Token == "" || params.Body.Password == "" { + logrus.Error("missing token or password") + return identity.NewRegisterNotFound() + } + logrus.Infof("received register request for token '%v'", params.Body.Token) + + tx, err := str.Begin() + if err != nil { + logrus.Error(err) + return identity.NewRegisterInternalServerError() + } + defer func() { _ = tx.Rollback() }() + + ar, err := str.FindAccountRequestWithToken(params.Body.Token, tx) + if err != nil { + logrus.Error(err) + return identity.NewRegisterNotFound() + } + + a := &store.Account{ + Email: ar.Email, + Password: params.Body.Password, + Token: createToken(), + } + if _, err := str.CreateAccount(a, tx); err != nil { + logrus.Error(err) + return identity.NewRegisterInternalServerError() + } + + if err := str.DeleteAccountRequest(ar.Id, tx); err != nil { + logrus.Error(err) + return identity.NewRegisterInternalServerError() + } + + if err := tx.Commit(); err != nil { + logrus.Error(err) + return identity.NewCreateAccountInternalServerError() + } + + logrus.Infof("created account '%v' with token '%v'", a.Email, a.Token) + + return identity.NewRegisterOK().WithPayload(&rest_model_zrok.RegisterResponse{Token: a.Token}) +} diff --git a/rest_client_zrok/identity/register_responses.go b/rest_client_zrok/identity/register_responses.go index b9068146..035f9c3c 100644 --- a/rest_client_zrok/identity/register_responses.go +++ b/rest_client_zrok/identity/register_responses.go @@ -57,6 +57,7 @@ RegisterOK describes a response with status code 200, with default header values account created */ type RegisterOK struct { + Payload *rest_model_zrok.RegisterResponse } // IsSuccess returns true when this register o k response has a 2xx status code @@ -85,15 +86,26 @@ func (o *RegisterOK) IsCode(code int) bool { } func (o *RegisterOK) Error() string { - return fmt.Sprintf("[POST /register][%d] registerOK ", 200) + return fmt.Sprintf("[POST /register][%d] registerOK %+v", 200, o.Payload) } func (o *RegisterOK) String() string { - return fmt.Sprintf("[POST /register][%d] registerOK ", 200) + return fmt.Sprintf("[POST /register][%d] registerOK %+v", 200, o.Payload) +} + +func (o *RegisterOK) GetPayload() *rest_model_zrok.RegisterResponse { + return o.Payload } func (o *RegisterOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + o.Payload = new(rest_model_zrok.RegisterResponse) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + return nil } diff --git a/rest_model_zrok/register_response.go b/rest_model_zrok/register_response.go new file mode 100644 index 00000000..619af9c4 --- /dev/null +++ b/rest_model_zrok/register_response.go @@ -0,0 +1,50 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package rest_model_zrok + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + + "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" +) + +// RegisterResponse register response +// +// swagger:model registerResponse +type RegisterResponse struct { + + // token + Token string `json:"token,omitempty"` +} + +// Validate validates this register response +func (m *RegisterResponse) Validate(formats strfmt.Registry) error { + return nil +} + +// ContextValidate validates this register response based on context it is used +func (m *RegisterResponse) ContextValidate(ctx context.Context, formats strfmt.Registry) error { + return nil +} + +// MarshalBinary interface implementation +func (m *RegisterResponse) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *RegisterResponse) UnmarshalBinary(b []byte) error { + var res RegisterResponse + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/rest_server_zrok/embedded_spec.go b/rest_server_zrok/embedded_spec.go index d5004105..16d87291 100644 --- a/rest_server_zrok/embedded_spec.go +++ b/rest_server_zrok/embedded_spec.go @@ -222,7 +222,10 @@ func init() { ], "responses": { "200": { - "description": "account created" + "description": "account created", + "schema": { + "$ref": "#/definitions/registerResponse" + } }, "404": { "description": "request not found" @@ -520,6 +523,14 @@ func init() { } } }, + "registerResponse": { + "type": "object", + "properties": { + "token": { + "type": "string" + } + } + }, "service": { "type": "object", "properties": { @@ -821,7 +832,10 @@ func init() { ], "responses": { "200": { - "description": "account created" + "description": "account created", + "schema": { + "$ref": "#/definitions/registerResponse" + } }, "404": { "description": "request not found" @@ -1119,6 +1133,14 @@ func init() { } } }, + "registerResponse": { + "type": "object", + "properties": { + "token": { + "type": "string" + } + } + }, "service": { "type": "object", "properties": { diff --git a/rest_server_zrok/operations/identity/register_responses.go b/rest_server_zrok/operations/identity/register_responses.go index 3fedebff..38c9e511 100644 --- a/rest_server_zrok/operations/identity/register_responses.go +++ b/rest_server_zrok/operations/identity/register_responses.go @@ -22,6 +22,11 @@ RegisterOK account created swagger:response registerOK */ type RegisterOK struct { + + /* + In: Body + */ + Payload *rest_model_zrok.RegisterResponse `json:"body,omitempty"` } // NewRegisterOK creates RegisterOK with default headers values @@ -30,12 +35,27 @@ func NewRegisterOK() *RegisterOK { return &RegisterOK{} } +// WithPayload adds the payload to the register o k response +func (o *RegisterOK) WithPayload(payload *rest_model_zrok.RegisterResponse) *RegisterOK { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the register o k response +func (o *RegisterOK) SetPayload(payload *rest_model_zrok.RegisterResponse) { + o.Payload = payload +} + // WriteResponse to the client func (o *RegisterOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { - rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses - rw.WriteHeader(200) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } } // RegisterNotFoundCode is the HTTP code returned for type RegisterNotFound diff --git a/specs/zrok.yml b/specs/zrok.yml index 1c146a99..a3d9c695 100644 --- a/specs/zrok.yml +++ b/specs/zrok.yml @@ -118,6 +118,8 @@ paths: responses: 200: description: account created + schema: + $ref: "#/definitions/registerResponse" 404: description: request not found 500: @@ -335,6 +337,11 @@ definitions: type: string password: type: string + registerResponse: + type: object + properties: + token: + type: string services: type: array diff --git a/ui/src/api/identity.js b/ui/src/api/identity.js index 2a2b3d66..b4bd2bfe 100644 --- a/ui/src/api/identity.js +++ b/ui/src/api/identity.js @@ -65,7 +65,7 @@ export function login(options) { /** * @param {object} options Optional options * @param {module:types.registerRequest} [options.body] - * @return {Promise} account created + * @return {Promise} account created */ export function register(options) { if (!options) options = {} diff --git a/ui/src/api/types.js b/ui/src/api/types.js index be7e210c..58fa33d9 100644 --- a/ui/src/api/types.js +++ b/ui/src/api/types.js @@ -93,6 +93,13 @@ * @property {string} password */ +/** + * @typedef registerResponse + * @memberof module:types + * + * @property {string} token + */ + /** * @typedef service * @memberof module:types