working, tested verify endpoint (#50)

This commit is contained in:
Michael Quigley 2022-09-19 16:26:54 -04:00
parent 08aba670ab
commit 6190ac0c60
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
10 changed files with 51 additions and 28 deletions

View File

@ -68,6 +68,25 @@ func (self *createAccountHandler) handleVerifiedCreate(params identity.CreateAcc
logrus.Error(err) logrus.Error(err)
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) 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() return identity.NewCreateAccountCreated()
} }

View File

@ -1,7 +1,6 @@
package store package store
import ( import (
"database/sql"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -14,7 +13,7 @@ type AccountRequest struct {
SourceAddress string 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 (?, ?, ?)") stmt, err := tx.Prepare("insert into account_requests (token, email, source_address) values (?, ?, ?)")
if err != nil { if err != nil {
return 0, errors.Wrap(err, "error preparing account_requests insert statement") return 0, errors.Wrap(err, "error preparing account_requests insert statement")

View File

@ -16,16 +16,21 @@ func newVerifyHandler(cfg *Config) *verifyHandler {
} }
func (self *verifyHandler) Handle(params identity.VerifyParams) middleware.Responder { func (self *verifyHandler) Handle(params identity.VerifyParams) middleware.Responder {
logrus.Infof("received verify request for token '%v'", params.Body.Token) if params.Body != nil {
tx, err := str.Begin() logrus.Infof("received verify request for token '%v'", params.Body.Token)
if err != nil { tx, err := str.Begin()
logrus.Errorf("error starting transaction: %v", err) if err != nil {
return identity.NewVerifyInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) 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})
} }

View File

@ -207,7 +207,7 @@ func (a *Client) Verify(params *VerifyParams, opts ...ClientOption) (*VerifyOK,
} }
op := &runtime.ClientOperation{ op := &runtime.ClientOperation{
ID: "verify", ID: "verify",
Method: "GET", Method: "POST",
PathPattern: "/verify", PathPattern: "/verify",
ProducesMediaTypes: []string{"application/zrok.v1+json"}, ProducesMediaTypes: []string{"application/zrok.v1+json"},
ConsumesMediaTypes: []string{"application/zrok.v1+json"}, ConsumesMediaTypes: []string{"application/zrok.v1+json"},

View File

@ -86,11 +86,11 @@ func (o *VerifyOK) IsCode(code int) bool {
} }
func (o *VerifyOK) Error() string { 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 { 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 { func (o *VerifyOK) GetPayload() *rest_model_zrok.VerifyResponse {
@ -148,11 +148,11 @@ func (o *VerifyNotFound) IsCode(code int) bool {
} }
func (o *VerifyNotFound) Error() string { 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 { 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 { 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 { 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 { 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 { func (o *VerifyInternalServerError) GetPayload() rest_model_zrok.ErrorMessage {

View File

@ -287,7 +287,7 @@ func init() {
} }
}, },
"/verify": { "/verify": {
"get": { "post": {
"tags": [ "tags": [
"identity" "identity"
], ],
@ -844,7 +844,7 @@ func init() {
} }
}, },
"/verify": { "/verify": {
"get": { "post": {
"tags": [ "tags": [
"identity" "identity"
], ],

View File

@ -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 Verify verify API
*/ */

View File

@ -377,10 +377,10 @@ func (o *ZrokAPI) initHandlerCache() {
o.handlers["DELETE"] = make(map[string]http.Handler) o.handlers["DELETE"] = make(map[string]http.Handler)
} }
o.handlers["DELETE"]["/untunnel"] = tunnel.NewUntunnel(o.context, o.TunnelUntunnelHandler) o.handlers["DELETE"]["/untunnel"] = tunnel.NewUntunnel(o.context, o.TunnelUntunnelHandler)
if o.handlers["GET"] == nil { if o.handlers["POST"] == nil {
o.handlers["GET"] = make(map[string]http.Handler) 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 { if o.handlers["GET"] == nil {
o.handlers["GET"] = make(map[string]http.Handler) o.handlers["GET"] = make(map[string]http.Handler)
} }

View File

@ -169,7 +169,7 @@ paths:
$ref: "#/definitions/errorMessage" $ref: "#/definitions/errorMessage"
/verify: /verify:
get: post:
tags: tags:
- identity - identity
operationId: verify operationId: verify

View File

@ -114,5 +114,5 @@ const loginOperation = {
const verifyOperation = { const verifyOperation = {
path: '/verify', path: '/verify',
contentTypes: ['application/zrok.v1+json'], contentTypes: ['application/zrok.v1+json'],
method: 'get' method: 'post'
} }