From 6190ac0c6041e640746ad00ea3eb0cc6f272f48b Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 19 Sep 2022 16:26:54 -0400 Subject: [PATCH] working, tested verify endpoint (#50) --- controller/account.go | 19 +++++++++++++ controller/store/account_request.go | 3 +-- controller/verify.go | 27 +++++++++++-------- rest_client_zrok/identity/identity_client.go | 2 +- rest_client_zrok/identity/verify_responses.go | 12 ++++----- rest_server_zrok/embedded_spec.go | 4 +-- .../operations/identity/verify.go | 2 +- rest_server_zrok/operations/zrok_api.go | 6 ++--- specs/zrok.yml | 2 +- ui/src/api/identity.js | 2 +- 10 files changed, 51 insertions(+), 28 deletions(-) diff --git a/controller/account.go b/controller/account.go index 7c7090b6..23a4a942 100644 --- a/controller/account.go +++ b/controller/account.go @@ -68,6 +68,25 @@ func (self *createAccountHandler) handleVerifiedCreate(params identity.CreateAcc logrus.Error(err) return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) } + ar := &store.AccountRequest{ + Token: token, + Email: params.Body.Email, + SourceAddress: params.HTTPRequest.RemoteAddr, + } + tx, err := str.Begin() + if err != nil { + logrus.Error(err) + return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + defer func() { _ = tx.Rollback() }() + if _, err := str.CreateAccountRequest(ar, tx); err != nil { + logrus.Error(err) + return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + if err := tx.Commit(); err != nil { + logrus.Error(err) + return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } return identity.NewCreateAccountCreated() } diff --git a/controller/store/account_request.go b/controller/store/account_request.go index 43b3dc46..dfc5ad5b 100644 --- a/controller/store/account_request.go +++ b/controller/store/account_request.go @@ -1,7 +1,6 @@ package store import ( - "database/sql" "github.com/jmoiron/sqlx" "github.com/pkg/errors" ) @@ -14,7 +13,7 @@ type AccountRequest struct { SourceAddress string } -func (self *Store) CreateAccountRequest(ar *AccountRequest, tx *sql.Tx) (int, error) { +func (self *Store) CreateAccountRequest(ar *AccountRequest, tx *sqlx.Tx) (int, error) { stmt, err := tx.Prepare("insert into account_requests (token, email, source_address) values (?, ?, ?)") if err != nil { return 0, errors.Wrap(err, "error preparing account_requests insert statement") diff --git a/controller/verify.go b/controller/verify.go index 791ff10a..89bc69ba 100644 --- a/controller/verify.go +++ b/controller/verify.go @@ -16,16 +16,21 @@ func newVerifyHandler(cfg *Config) *verifyHandler { } func (self *verifyHandler) Handle(params identity.VerifyParams) middleware.Responder { - logrus.Infof("received verify request for token '%v'", params.Body.Token) - tx, err := str.Begin() - if err != nil { - logrus.Errorf("error starting transaction: %v", err) - return identity.NewVerifyInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + if params.Body != nil { + logrus.Infof("received verify request for token '%v'", params.Body.Token) + tx, err := str.Begin() + if err != nil { + logrus.Errorf("error starting transaction: %v", err) + return identity.NewVerifyInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + ar, err := str.FindAccountRequestWithToken(params.Body.Token, tx) + if err != nil { + logrus.Errorf("error finding account with token '%v': %v", params.Body.Token, err) + return identity.NewVerifyNotFound() + } + return identity.NewVerifyOK().WithPayload(&rest_model_zrok.VerifyResponse{Email: ar.Email}) + } else { + logrus.Error("empty verification request") + return identity.NewVerifyInternalServerError().WithPayload(rest_model_zrok.ErrorMessage("empty verification request")) } - ar, err := str.FindAccountRequestWithToken(params.Body.Token, tx) - if err != nil { - logrus.Errorf("error finding account with token '%v': %v", params.Body.Token, err) - return identity.NewVerifyNotFound() - } - return identity.NewVerifyOK().WithPayload(&rest_model_zrok.VerifyResponse{Email: ar.Email}) } diff --git a/rest_client_zrok/identity/identity_client.go b/rest_client_zrok/identity/identity_client.go index e1e3b05b..8a42e3d2 100644 --- a/rest_client_zrok/identity/identity_client.go +++ b/rest_client_zrok/identity/identity_client.go @@ -207,7 +207,7 @@ func (a *Client) Verify(params *VerifyParams, opts ...ClientOption) (*VerifyOK, } op := &runtime.ClientOperation{ ID: "verify", - Method: "GET", + Method: "POST", PathPattern: "/verify", ProducesMediaTypes: []string{"application/zrok.v1+json"}, ConsumesMediaTypes: []string{"application/zrok.v1+json"}, diff --git a/rest_client_zrok/identity/verify_responses.go b/rest_client_zrok/identity/verify_responses.go index d2b7ac12..06f15218 100644 --- a/rest_client_zrok/identity/verify_responses.go +++ b/rest_client_zrok/identity/verify_responses.go @@ -86,11 +86,11 @@ func (o *VerifyOK) IsCode(code int) bool { } func (o *VerifyOK) Error() string { - return fmt.Sprintf("[GET /verify][%d] verifyOK %+v", 200, o.Payload) + return fmt.Sprintf("[POST /verify][%d] verifyOK %+v", 200, o.Payload) } func (o *VerifyOK) String() string { - return fmt.Sprintf("[GET /verify][%d] verifyOK %+v", 200, o.Payload) + return fmt.Sprintf("[POST /verify][%d] verifyOK %+v", 200, o.Payload) } func (o *VerifyOK) GetPayload() *rest_model_zrok.VerifyResponse { @@ -148,11 +148,11 @@ func (o *VerifyNotFound) IsCode(code int) bool { } func (o *VerifyNotFound) Error() string { - return fmt.Sprintf("[GET /verify][%d] verifyNotFound ", 404) + return fmt.Sprintf("[POST /verify][%d] verifyNotFound ", 404) } func (o *VerifyNotFound) String() string { - return fmt.Sprintf("[GET /verify][%d] verifyNotFound ", 404) + return fmt.Sprintf("[POST /verify][%d] verifyNotFound ", 404) } func (o *VerifyNotFound) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { @@ -200,11 +200,11 @@ func (o *VerifyInternalServerError) IsCode(code int) bool { } func (o *VerifyInternalServerError) Error() string { - return fmt.Sprintf("[GET /verify][%d] verifyInternalServerError %+v", 500, o.Payload) + return fmt.Sprintf("[POST /verify][%d] verifyInternalServerError %+v", 500, o.Payload) } func (o *VerifyInternalServerError) String() string { - return fmt.Sprintf("[GET /verify][%d] verifyInternalServerError %+v", 500, o.Payload) + return fmt.Sprintf("[POST /verify][%d] verifyInternalServerError %+v", 500, o.Payload) } func (o *VerifyInternalServerError) GetPayload() rest_model_zrok.ErrorMessage { diff --git a/rest_server_zrok/embedded_spec.go b/rest_server_zrok/embedded_spec.go index 40e5f4b8..5ab87572 100644 --- a/rest_server_zrok/embedded_spec.go +++ b/rest_server_zrok/embedded_spec.go @@ -287,7 +287,7 @@ func init() { } }, "/verify": { - "get": { + "post": { "tags": [ "identity" ], @@ -844,7 +844,7 @@ func init() { } }, "/verify": { - "get": { + "post": { "tags": [ "identity" ], diff --git a/rest_server_zrok/operations/identity/verify.go b/rest_server_zrok/operations/identity/verify.go index 7652aec7..917e7985 100644 --- a/rest_server_zrok/operations/identity/verify.go +++ b/rest_server_zrok/operations/identity/verify.go @@ -30,7 +30,7 @@ func NewVerify(ctx *middleware.Context, handler VerifyHandler) *Verify { } /* - Verify swagger:route GET /verify identity verify + Verify swagger:route POST /verify identity verify Verify verify API */ diff --git a/rest_server_zrok/operations/zrok_api.go b/rest_server_zrok/operations/zrok_api.go index 36790825..b4dac62a 100644 --- a/rest_server_zrok/operations/zrok_api.go +++ b/rest_server_zrok/operations/zrok_api.go @@ -377,10 +377,10 @@ func (o *ZrokAPI) initHandlerCache() { o.handlers["DELETE"] = make(map[string]http.Handler) } o.handlers["DELETE"]["/untunnel"] = tunnel.NewUntunnel(o.context, o.TunnelUntunnelHandler) - if o.handlers["GET"] == nil { - o.handlers["GET"] = make(map[string]http.Handler) + if o.handlers["POST"] == nil { + o.handlers["POST"] = make(map[string]http.Handler) } - o.handlers["GET"]["/verify"] = identity.NewVerify(o.context, o.IdentityVerifyHandler) + o.handlers["POST"]["/verify"] = identity.NewVerify(o.context, o.IdentityVerifyHandler) if o.handlers["GET"] == nil { o.handlers["GET"] = make(map[string]http.Handler) } diff --git a/specs/zrok.yml b/specs/zrok.yml index ff7c369b..fd627e85 100644 --- a/specs/zrok.yml +++ b/specs/zrok.yml @@ -169,7 +169,7 @@ paths: $ref: "#/definitions/errorMessage" /verify: - get: + post: tags: - identity operationId: verify diff --git a/ui/src/api/identity.js b/ui/src/api/identity.js index 72a534b6..8306d31b 100644 --- a/ui/src/api/identity.js +++ b/ui/src/api/identity.js @@ -114,5 +114,5 @@ const loginOperation = { const verifyOperation = { path: '/verify', contentTypes: ['application/zrok.v1+json'], - method: 'get' + method: 'post' }