controller registration endpoint improvements (#50)

This commit is contained in:
Michael Quigley 2022-09-20 16:01:46 -04:00
parent ec6afaaa3d
commit 242a8bee7b
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
10 changed files with 186 additions and 8 deletions

View File

@ -17,11 +17,11 @@ func newCreateAccountHandler(cfg *Config) *createAccountHandler {
} }
func (self *createAccountHandler) Handle(params identity.CreateAccountParams) middleware.Responder { 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 == "" { if params.Body == nil || params.Body.Email == "" {
logrus.Errorf("missing email") logrus.Errorf("missing email")
return identity.NewCreateAccountBadRequest().WithPayload("missing email") return identity.NewCreateAccountBadRequest().WithPayload("missing email")
} }
logrus.Infof("received account request for email '%v'", params.Body.Email)
token := createToken() token := createToken()
ar := &store.AccountRequest{ ar := &store.AccountRequest{

View File

@ -27,6 +27,7 @@ func Run(cfg *Config) error {
api.IdentityEnableHandler = newEnableHandler(cfg) api.IdentityEnableHandler = newEnableHandler(cfg)
api.IdentityDisableHandler = newDisableHandler(cfg) api.IdentityDisableHandler = newDisableHandler(cfg)
api.IdentityLoginHandler = identity.LoginHandlerFunc(loginHandler) api.IdentityLoginHandler = identity.LoginHandlerFunc(loginHandler)
api.IdentityRegisterHandler = newRegisterHandler()
api.IdentityVerifyHandler = newVerifyHandler(cfg) api.IdentityVerifyHandler = newVerifyHandler(cfg)
api.MetadataOverviewHandler = metadata.OverviewHandlerFunc(overviewHandler) api.MetadataOverviewHandler = metadata.OverviewHandlerFunc(overviewHandler)
api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler) api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler)

59
controller/register.go Normal file
View File

@ -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 &registerHandler{}
}
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})
}

View File

@ -57,6 +57,7 @@ RegisterOK describes a response with status code 200, with default header values
account created account created
*/ */
type RegisterOK struct { type RegisterOK struct {
Payload *rest_model_zrok.RegisterResponse
} }
// IsSuccess returns true when this register o k response has a 2xx status code // 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 { 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 { 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 { 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 return nil
} }

View File

@ -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
}

View File

@ -222,7 +222,10 @@ func init() {
], ],
"responses": { "responses": {
"200": { "200": {
"description": "account created" "description": "account created",
"schema": {
"$ref": "#/definitions/registerResponse"
}
}, },
"404": { "404": {
"description": "request not found" "description": "request not found"
@ -520,6 +523,14 @@ func init() {
} }
} }
}, },
"registerResponse": {
"type": "object",
"properties": {
"token": {
"type": "string"
}
}
},
"service": { "service": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -821,7 +832,10 @@ func init() {
], ],
"responses": { "responses": {
"200": { "200": {
"description": "account created" "description": "account created",
"schema": {
"$ref": "#/definitions/registerResponse"
}
}, },
"404": { "404": {
"description": "request not found" "description": "request not found"
@ -1119,6 +1133,14 @@ func init() {
} }
} }
}, },
"registerResponse": {
"type": "object",
"properties": {
"token": {
"type": "string"
}
}
},
"service": { "service": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -22,6 +22,11 @@ RegisterOK account created
swagger:response registerOK swagger:response registerOK
*/ */
type RegisterOK struct { type RegisterOK struct {
/*
In: Body
*/
Payload *rest_model_zrok.RegisterResponse `json:"body,omitempty"`
} }
// NewRegisterOK creates RegisterOK with default headers values // NewRegisterOK creates RegisterOK with default headers values
@ -30,12 +35,27 @@ func NewRegisterOK() *RegisterOK {
return &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 // WriteResponse to the client
func (o *RegisterOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { func (o *RegisterOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses
rw.WriteHeader(200) 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 // RegisterNotFoundCode is the HTTP code returned for type RegisterNotFound

View File

@ -118,6 +118,8 @@ paths:
responses: responses:
200: 200:
description: account created description: account created
schema:
$ref: "#/definitions/registerResponse"
404: 404:
description: request not found description: request not found
500: 500:
@ -335,6 +337,11 @@ definitions:
type: string type: string
password: password:
type: string type: string
registerResponse:
type: object
properties:
token:
type: string
services: services:
type: array type: array

View File

@ -65,7 +65,7 @@ export function login(options) {
/** /**
* @param {object} options Optional options * @param {object} options Optional options
* @param {module:types.registerRequest} [options.body] * @param {module:types.registerRequest} [options.body]
* @return {Promise<object>} account created * @return {Promise<module:types.registerResponse>} account created
*/ */
export function register(options) { export function register(options) {
if (!options) options = {} if (!options) options = {}

View File

@ -93,6 +93,13 @@
* @property {string} password * @property {string} password
*/ */
/**
* @typedef registerResponse
* @memberof module:types
*
* @property {string} token
*/
/** /**
* @typedef service * @typedef service
* @memberof module:types * @memberof module:types