diff --git a/internal/httpclient/client/common/get_self_service_registration_flow_parameters.go b/internal/httpclient/client/common/get_self_service_registration_flow_parameters.go new file mode 100644 index 000000000000..1aa1dd305f9c --- /dev/null +++ b/internal/httpclient/client/common/get_self_service_registration_flow_parameters.go @@ -0,0 +1,142 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package common + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + "net/http" + "time" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" + cr "github.com/go-openapi/runtime/client" + "github.com/go-openapi/strfmt" +) + +// NewGetSelfServiceRegistrationFlowParams creates a new GetSelfServiceRegistrationFlowParams object +// with the default values initialized. +func NewGetSelfServiceRegistrationFlowParams() *GetSelfServiceRegistrationFlowParams { + var () + return &GetSelfServiceRegistrationFlowParams{ + + timeout: cr.DefaultTimeout, + } +} + +// NewGetSelfServiceRegistrationFlowParamsWithTimeout creates a new GetSelfServiceRegistrationFlowParams object +// with the default values initialized, and the ability to set a timeout on a request +func NewGetSelfServiceRegistrationFlowParamsWithTimeout(timeout time.Duration) *GetSelfServiceRegistrationFlowParams { + var () + return &GetSelfServiceRegistrationFlowParams{ + + timeout: timeout, + } +} + +// NewGetSelfServiceRegistrationFlowParamsWithContext creates a new GetSelfServiceRegistrationFlowParams object +// with the default values initialized, and the ability to set a context for a request +func NewGetSelfServiceRegistrationFlowParamsWithContext(ctx context.Context) *GetSelfServiceRegistrationFlowParams { + var () + return &GetSelfServiceRegistrationFlowParams{ + + Context: ctx, + } +} + +// NewGetSelfServiceRegistrationFlowParamsWithHTTPClient creates a new GetSelfServiceRegistrationFlowParams object +// with the default values initialized, and the ability to set a custom HTTPClient for a request +func NewGetSelfServiceRegistrationFlowParamsWithHTTPClient(client *http.Client) *GetSelfServiceRegistrationFlowParams { + var () + return &GetSelfServiceRegistrationFlowParams{ + HTTPClient: client, + } +} + +/*GetSelfServiceRegistrationFlowParams contains all the parameters to send to the API endpoint +for the get self service registration flow operation typically these are written to a http.Request +*/ +type GetSelfServiceRegistrationFlowParams struct { + + /*Flow + The Registration Flow ID + + The value for this parameter comes from `flow` URL Query parameter sent to your + application (e.g. `/registration?flow=abcde`). + + */ + Flow string + + timeout time.Duration + Context context.Context + HTTPClient *http.Client +} + +// WithTimeout adds the timeout to the get self service registration flow params +func (o *GetSelfServiceRegistrationFlowParams) WithTimeout(timeout time.Duration) *GetSelfServiceRegistrationFlowParams { + o.SetTimeout(timeout) + return o +} + +// SetTimeout adds the timeout to the get self service registration flow params +func (o *GetSelfServiceRegistrationFlowParams) SetTimeout(timeout time.Duration) { + o.timeout = timeout +} + +// WithContext adds the context to the get self service registration flow params +func (o *GetSelfServiceRegistrationFlowParams) WithContext(ctx context.Context) *GetSelfServiceRegistrationFlowParams { + o.SetContext(ctx) + return o +} + +// SetContext adds the context to the get self service registration flow params +func (o *GetSelfServiceRegistrationFlowParams) SetContext(ctx context.Context) { + o.Context = ctx +} + +// WithHTTPClient adds the HTTPClient to the get self service registration flow params +func (o *GetSelfServiceRegistrationFlowParams) WithHTTPClient(client *http.Client) *GetSelfServiceRegistrationFlowParams { + o.SetHTTPClient(client) + return o +} + +// SetHTTPClient adds the HTTPClient to the get self service registration flow params +func (o *GetSelfServiceRegistrationFlowParams) SetHTTPClient(client *http.Client) { + o.HTTPClient = client +} + +// WithFlow adds the flow to the get self service registration flow params +func (o *GetSelfServiceRegistrationFlowParams) WithFlow(flow string) *GetSelfServiceRegistrationFlowParams { + o.SetFlow(flow) + return o +} + +// SetFlow adds the flow to the get self service registration flow params +func (o *GetSelfServiceRegistrationFlowParams) SetFlow(flow string) { + o.Flow = flow +} + +// WriteToRequest writes these params to a swagger request +func (o *GetSelfServiceRegistrationFlowParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error { + + if err := r.SetTimeout(o.timeout); err != nil { + return err + } + var res []error + + // query param flow + qrFlow := o.Flow + qFlow := qrFlow + if qFlow != "" { + if err := r.SetQueryParam("flow", qFlow); err != nil { + return err + } + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/internal/httpclient/client/common/get_self_service_registration_flow_responses.go b/internal/httpclient/client/common/get_self_service_registration_flow_responses.go new file mode 100644 index 000000000000..8ad5a2cd0ad3 --- /dev/null +++ b/internal/httpclient/client/common/get_self_service_registration_flow_responses.go @@ -0,0 +1,225 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package common + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "fmt" + "io" + + "github.com/go-openapi/runtime" + "github.com/go-openapi/strfmt" + + "github.com/ory/kratos/internal/httpclient/models" +) + +// GetSelfServiceRegistrationFlowReader is a Reader for the GetSelfServiceRegistrationFlow structure. +type GetSelfServiceRegistrationFlowReader struct { + formats strfmt.Registry +} + +// ReadResponse reads a server response into the received o. +func (o *GetSelfServiceRegistrationFlowReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) { + switch response.Code() { + case 200: + result := NewGetSelfServiceRegistrationFlowOK() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return result, nil + case 403: + result := NewGetSelfServiceRegistrationFlowForbidden() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result + case 404: + result := NewGetSelfServiceRegistrationFlowNotFound() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result + case 410: + result := NewGetSelfServiceRegistrationFlowGone() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result + case 500: + result := NewGetSelfServiceRegistrationFlowInternalServerError() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result + + default: + return nil, runtime.NewAPIError("response status code does not match any response statuses defined for this endpoint in the swagger spec", response, response.Code()) + } +} + +// NewGetSelfServiceRegistrationFlowOK creates a GetSelfServiceRegistrationFlowOK with default headers values +func NewGetSelfServiceRegistrationFlowOK() *GetSelfServiceRegistrationFlowOK { + return &GetSelfServiceRegistrationFlowOK{} +} + +/*GetSelfServiceRegistrationFlowOK handles this case with default header values. + +registrationFlow +*/ +type GetSelfServiceRegistrationFlowOK struct { + Payload *models.RegistrationFlow +} + +func (o *GetSelfServiceRegistrationFlowOK) Error() string { + return fmt.Sprintf("[GET /self-service/registration/flows][%d] getSelfServiceRegistrationFlowOK %+v", 200, o.Payload) +} + +func (o *GetSelfServiceRegistrationFlowOK) GetPayload() *models.RegistrationFlow { + return o.Payload +} + +func (o *GetSelfServiceRegistrationFlowOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.RegistrationFlow) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} + +// NewGetSelfServiceRegistrationFlowForbidden creates a GetSelfServiceRegistrationFlowForbidden with default headers values +func NewGetSelfServiceRegistrationFlowForbidden() *GetSelfServiceRegistrationFlowForbidden { + return &GetSelfServiceRegistrationFlowForbidden{} +} + +/*GetSelfServiceRegistrationFlowForbidden handles this case with default header values. + +genericError +*/ +type GetSelfServiceRegistrationFlowForbidden struct { + Payload *models.GenericError +} + +func (o *GetSelfServiceRegistrationFlowForbidden) Error() string { + return fmt.Sprintf("[GET /self-service/registration/flows][%d] getSelfServiceRegistrationFlowForbidden %+v", 403, o.Payload) +} + +func (o *GetSelfServiceRegistrationFlowForbidden) GetPayload() *models.GenericError { + return o.Payload +} + +func (o *GetSelfServiceRegistrationFlowForbidden) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.GenericError) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} + +// NewGetSelfServiceRegistrationFlowNotFound creates a GetSelfServiceRegistrationFlowNotFound with default headers values +func NewGetSelfServiceRegistrationFlowNotFound() *GetSelfServiceRegistrationFlowNotFound { + return &GetSelfServiceRegistrationFlowNotFound{} +} + +/*GetSelfServiceRegistrationFlowNotFound handles this case with default header values. + +genericError +*/ +type GetSelfServiceRegistrationFlowNotFound struct { + Payload *models.GenericError +} + +func (o *GetSelfServiceRegistrationFlowNotFound) Error() string { + return fmt.Sprintf("[GET /self-service/registration/flows][%d] getSelfServiceRegistrationFlowNotFound %+v", 404, o.Payload) +} + +func (o *GetSelfServiceRegistrationFlowNotFound) GetPayload() *models.GenericError { + return o.Payload +} + +func (o *GetSelfServiceRegistrationFlowNotFound) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.GenericError) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} + +// NewGetSelfServiceRegistrationFlowGone creates a GetSelfServiceRegistrationFlowGone with default headers values +func NewGetSelfServiceRegistrationFlowGone() *GetSelfServiceRegistrationFlowGone { + return &GetSelfServiceRegistrationFlowGone{} +} + +/*GetSelfServiceRegistrationFlowGone handles this case with default header values. + +genericError +*/ +type GetSelfServiceRegistrationFlowGone struct { + Payload *models.GenericError +} + +func (o *GetSelfServiceRegistrationFlowGone) Error() string { + return fmt.Sprintf("[GET /self-service/registration/flows][%d] getSelfServiceRegistrationFlowGone %+v", 410, o.Payload) +} + +func (o *GetSelfServiceRegistrationFlowGone) GetPayload() *models.GenericError { + return o.Payload +} + +func (o *GetSelfServiceRegistrationFlowGone) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.GenericError) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} + +// NewGetSelfServiceRegistrationFlowInternalServerError creates a GetSelfServiceRegistrationFlowInternalServerError with default headers values +func NewGetSelfServiceRegistrationFlowInternalServerError() *GetSelfServiceRegistrationFlowInternalServerError { + return &GetSelfServiceRegistrationFlowInternalServerError{} +} + +/*GetSelfServiceRegistrationFlowInternalServerError handles this case with default header values. + +genericError +*/ +type GetSelfServiceRegistrationFlowInternalServerError struct { + Payload *models.GenericError +} + +func (o *GetSelfServiceRegistrationFlowInternalServerError) Error() string { + return fmt.Sprintf("[GET /self-service/registration/flows][%d] getSelfServiceRegistrationFlowInternalServerError %+v", 500, o.Payload) +} + +func (o *GetSelfServiceRegistrationFlowInternalServerError) GetPayload() *models.GenericError { + return o.Payload +} + +func (o *GetSelfServiceRegistrationFlowInternalServerError) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.GenericError) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} diff --git a/internal/httpclient/client/common/initialize_self_service_registration_via_api_flow_parameters.go b/internal/httpclient/client/common/initialize_self_service_registration_via_api_flow_parameters.go new file mode 100644 index 000000000000..1db94e563cd6 --- /dev/null +++ b/internal/httpclient/client/common/initialize_self_service_registration_via_api_flow_parameters.go @@ -0,0 +1,112 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package common + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + "net/http" + "time" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" + cr "github.com/go-openapi/runtime/client" + "github.com/go-openapi/strfmt" +) + +// NewInitializeSelfServiceRegistrationViaAPIFlowParams creates a new InitializeSelfServiceRegistrationViaAPIFlowParams object +// with the default values initialized. +func NewInitializeSelfServiceRegistrationViaAPIFlowParams() *InitializeSelfServiceRegistrationViaAPIFlowParams { + + return &InitializeSelfServiceRegistrationViaAPIFlowParams{ + + timeout: cr.DefaultTimeout, + } +} + +// NewInitializeSelfServiceRegistrationViaAPIFlowParamsWithTimeout creates a new InitializeSelfServiceRegistrationViaAPIFlowParams object +// with the default values initialized, and the ability to set a timeout on a request +func NewInitializeSelfServiceRegistrationViaAPIFlowParamsWithTimeout(timeout time.Duration) *InitializeSelfServiceRegistrationViaAPIFlowParams { + + return &InitializeSelfServiceRegistrationViaAPIFlowParams{ + + timeout: timeout, + } +} + +// NewInitializeSelfServiceRegistrationViaAPIFlowParamsWithContext creates a new InitializeSelfServiceRegistrationViaAPIFlowParams object +// with the default values initialized, and the ability to set a context for a request +func NewInitializeSelfServiceRegistrationViaAPIFlowParamsWithContext(ctx context.Context) *InitializeSelfServiceRegistrationViaAPIFlowParams { + + return &InitializeSelfServiceRegistrationViaAPIFlowParams{ + + Context: ctx, + } +} + +// NewInitializeSelfServiceRegistrationViaAPIFlowParamsWithHTTPClient creates a new InitializeSelfServiceRegistrationViaAPIFlowParams object +// with the default values initialized, and the ability to set a custom HTTPClient for a request +func NewInitializeSelfServiceRegistrationViaAPIFlowParamsWithHTTPClient(client *http.Client) *InitializeSelfServiceRegistrationViaAPIFlowParams { + + return &InitializeSelfServiceRegistrationViaAPIFlowParams{ + HTTPClient: client, + } +} + +/*InitializeSelfServiceRegistrationViaAPIFlowParams contains all the parameters to send to the API endpoint +for the initialize self service registration via API flow operation typically these are written to a http.Request +*/ +type InitializeSelfServiceRegistrationViaAPIFlowParams struct { + timeout time.Duration + Context context.Context + HTTPClient *http.Client +} + +// WithTimeout adds the timeout to the initialize self service registration via API flow params +func (o *InitializeSelfServiceRegistrationViaAPIFlowParams) WithTimeout(timeout time.Duration) *InitializeSelfServiceRegistrationViaAPIFlowParams { + o.SetTimeout(timeout) + return o +} + +// SetTimeout adds the timeout to the initialize self service registration via API flow params +func (o *InitializeSelfServiceRegistrationViaAPIFlowParams) SetTimeout(timeout time.Duration) { + o.timeout = timeout +} + +// WithContext adds the context to the initialize self service registration via API flow params +func (o *InitializeSelfServiceRegistrationViaAPIFlowParams) WithContext(ctx context.Context) *InitializeSelfServiceRegistrationViaAPIFlowParams { + o.SetContext(ctx) + return o +} + +// SetContext adds the context to the initialize self service registration via API flow params +func (o *InitializeSelfServiceRegistrationViaAPIFlowParams) SetContext(ctx context.Context) { + o.Context = ctx +} + +// WithHTTPClient adds the HTTPClient to the initialize self service registration via API flow params +func (o *InitializeSelfServiceRegistrationViaAPIFlowParams) WithHTTPClient(client *http.Client) *InitializeSelfServiceRegistrationViaAPIFlowParams { + o.SetHTTPClient(client) + return o +} + +// SetHTTPClient adds the HTTPClient to the initialize self service registration via API flow params +func (o *InitializeSelfServiceRegistrationViaAPIFlowParams) SetHTTPClient(client *http.Client) { + o.HTTPClient = client +} + +// WriteToRequest writes these params to a swagger request +func (o *InitializeSelfServiceRegistrationViaAPIFlowParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error { + + if err := r.SetTimeout(o.timeout); err != nil { + return err + } + var res []error + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/internal/httpclient/client/common/initialize_self_service_registration_via_api_flow_responses.go b/internal/httpclient/client/common/initialize_self_service_registration_via_api_flow_responses.go new file mode 100644 index 000000000000..e2cf4bddb8d3 --- /dev/null +++ b/internal/httpclient/client/common/initialize_self_service_registration_via_api_flow_responses.go @@ -0,0 +1,147 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package common + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "fmt" + "io" + + "github.com/go-openapi/runtime" + "github.com/go-openapi/strfmt" + + "github.com/ory/kratos/internal/httpclient/models" +) + +// InitializeSelfServiceRegistrationViaAPIFlowReader is a Reader for the InitializeSelfServiceRegistrationViaAPIFlow structure. +type InitializeSelfServiceRegistrationViaAPIFlowReader struct { + formats strfmt.Registry +} + +// ReadResponse reads a server response into the received o. +func (o *InitializeSelfServiceRegistrationViaAPIFlowReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) { + switch response.Code() { + case 200: + result := NewInitializeSelfServiceRegistrationViaAPIFlowOK() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return result, nil + case 400: + result := NewInitializeSelfServiceRegistrationViaAPIFlowBadRequest() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result + case 500: + result := NewInitializeSelfServiceRegistrationViaAPIFlowInternalServerError() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result + + default: + return nil, runtime.NewAPIError("response status code does not match any response statuses defined for this endpoint in the swagger spec", response, response.Code()) + } +} + +// NewInitializeSelfServiceRegistrationViaAPIFlowOK creates a InitializeSelfServiceRegistrationViaAPIFlowOK with default headers values +func NewInitializeSelfServiceRegistrationViaAPIFlowOK() *InitializeSelfServiceRegistrationViaAPIFlowOK { + return &InitializeSelfServiceRegistrationViaAPIFlowOK{} +} + +/*InitializeSelfServiceRegistrationViaAPIFlowOK handles this case with default header values. + +registrationFlow +*/ +type InitializeSelfServiceRegistrationViaAPIFlowOK struct { + Payload *models.RegistrationFlow +} + +func (o *InitializeSelfServiceRegistrationViaAPIFlowOK) Error() string { + return fmt.Sprintf("[GET /self-service/registration/api][%d] initializeSelfServiceRegistrationViaApiFlowOK %+v", 200, o.Payload) +} + +func (o *InitializeSelfServiceRegistrationViaAPIFlowOK) GetPayload() *models.RegistrationFlow { + return o.Payload +} + +func (o *InitializeSelfServiceRegistrationViaAPIFlowOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.RegistrationFlow) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} + +// NewInitializeSelfServiceRegistrationViaAPIFlowBadRequest creates a InitializeSelfServiceRegistrationViaAPIFlowBadRequest with default headers values +func NewInitializeSelfServiceRegistrationViaAPIFlowBadRequest() *InitializeSelfServiceRegistrationViaAPIFlowBadRequest { + return &InitializeSelfServiceRegistrationViaAPIFlowBadRequest{} +} + +/*InitializeSelfServiceRegistrationViaAPIFlowBadRequest handles this case with default header values. + +genericError +*/ +type InitializeSelfServiceRegistrationViaAPIFlowBadRequest struct { + Payload *models.GenericError +} + +func (o *InitializeSelfServiceRegistrationViaAPIFlowBadRequest) Error() string { + return fmt.Sprintf("[GET /self-service/registration/api][%d] initializeSelfServiceRegistrationViaApiFlowBadRequest %+v", 400, o.Payload) +} + +func (o *InitializeSelfServiceRegistrationViaAPIFlowBadRequest) GetPayload() *models.GenericError { + return o.Payload +} + +func (o *InitializeSelfServiceRegistrationViaAPIFlowBadRequest) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.GenericError) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} + +// NewInitializeSelfServiceRegistrationViaAPIFlowInternalServerError creates a InitializeSelfServiceRegistrationViaAPIFlowInternalServerError with default headers values +func NewInitializeSelfServiceRegistrationViaAPIFlowInternalServerError() *InitializeSelfServiceRegistrationViaAPIFlowInternalServerError { + return &InitializeSelfServiceRegistrationViaAPIFlowInternalServerError{} +} + +/*InitializeSelfServiceRegistrationViaAPIFlowInternalServerError handles this case with default header values. + +genericError +*/ +type InitializeSelfServiceRegistrationViaAPIFlowInternalServerError struct { + Payload *models.GenericError +} + +func (o *InitializeSelfServiceRegistrationViaAPIFlowInternalServerError) Error() string { + return fmt.Sprintf("[GET /self-service/registration/api][%d] initializeSelfServiceRegistrationViaApiFlowInternalServerError %+v", 500, o.Payload) +} + +func (o *InitializeSelfServiceRegistrationViaAPIFlowInternalServerError) GetPayload() *models.GenericError { + return o.Payload +} + +func (o *InitializeSelfServiceRegistrationViaAPIFlowInternalServerError) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.GenericError) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} diff --git a/internal/httpclient/client/public/initialize_self_service_registration_via_browser_flow_parameters.go b/internal/httpclient/client/public/initialize_self_service_registration_via_browser_flow_parameters.go new file mode 100644 index 000000000000..067667fd4c75 --- /dev/null +++ b/internal/httpclient/client/public/initialize_self_service_registration_via_browser_flow_parameters.go @@ -0,0 +1,112 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package public + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + "net/http" + "time" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" + cr "github.com/go-openapi/runtime/client" + "github.com/go-openapi/strfmt" +) + +// NewInitializeSelfServiceRegistrationViaBrowserFlowParams creates a new InitializeSelfServiceRegistrationViaBrowserFlowParams object +// with the default values initialized. +func NewInitializeSelfServiceRegistrationViaBrowserFlowParams() *InitializeSelfServiceRegistrationViaBrowserFlowParams { + + return &InitializeSelfServiceRegistrationViaBrowserFlowParams{ + + timeout: cr.DefaultTimeout, + } +} + +// NewInitializeSelfServiceRegistrationViaBrowserFlowParamsWithTimeout creates a new InitializeSelfServiceRegistrationViaBrowserFlowParams object +// with the default values initialized, and the ability to set a timeout on a request +func NewInitializeSelfServiceRegistrationViaBrowserFlowParamsWithTimeout(timeout time.Duration) *InitializeSelfServiceRegistrationViaBrowserFlowParams { + + return &InitializeSelfServiceRegistrationViaBrowserFlowParams{ + + timeout: timeout, + } +} + +// NewInitializeSelfServiceRegistrationViaBrowserFlowParamsWithContext creates a new InitializeSelfServiceRegistrationViaBrowserFlowParams object +// with the default values initialized, and the ability to set a context for a request +func NewInitializeSelfServiceRegistrationViaBrowserFlowParamsWithContext(ctx context.Context) *InitializeSelfServiceRegistrationViaBrowserFlowParams { + + return &InitializeSelfServiceRegistrationViaBrowserFlowParams{ + + Context: ctx, + } +} + +// NewInitializeSelfServiceRegistrationViaBrowserFlowParamsWithHTTPClient creates a new InitializeSelfServiceRegistrationViaBrowserFlowParams object +// with the default values initialized, and the ability to set a custom HTTPClient for a request +func NewInitializeSelfServiceRegistrationViaBrowserFlowParamsWithHTTPClient(client *http.Client) *InitializeSelfServiceRegistrationViaBrowserFlowParams { + + return &InitializeSelfServiceRegistrationViaBrowserFlowParams{ + HTTPClient: client, + } +} + +/*InitializeSelfServiceRegistrationViaBrowserFlowParams contains all the parameters to send to the API endpoint +for the initialize self service registration via browser flow operation typically these are written to a http.Request +*/ +type InitializeSelfServiceRegistrationViaBrowserFlowParams struct { + timeout time.Duration + Context context.Context + HTTPClient *http.Client +} + +// WithTimeout adds the timeout to the initialize self service registration via browser flow params +func (o *InitializeSelfServiceRegistrationViaBrowserFlowParams) WithTimeout(timeout time.Duration) *InitializeSelfServiceRegistrationViaBrowserFlowParams { + o.SetTimeout(timeout) + return o +} + +// SetTimeout adds the timeout to the initialize self service registration via browser flow params +func (o *InitializeSelfServiceRegistrationViaBrowserFlowParams) SetTimeout(timeout time.Duration) { + o.timeout = timeout +} + +// WithContext adds the context to the initialize self service registration via browser flow params +func (o *InitializeSelfServiceRegistrationViaBrowserFlowParams) WithContext(ctx context.Context) *InitializeSelfServiceRegistrationViaBrowserFlowParams { + o.SetContext(ctx) + return o +} + +// SetContext adds the context to the initialize self service registration via browser flow params +func (o *InitializeSelfServiceRegistrationViaBrowserFlowParams) SetContext(ctx context.Context) { + o.Context = ctx +} + +// WithHTTPClient adds the HTTPClient to the initialize self service registration via browser flow params +func (o *InitializeSelfServiceRegistrationViaBrowserFlowParams) WithHTTPClient(client *http.Client) *InitializeSelfServiceRegistrationViaBrowserFlowParams { + o.SetHTTPClient(client) + return o +} + +// SetHTTPClient adds the HTTPClient to the initialize self service registration via browser flow params +func (o *InitializeSelfServiceRegistrationViaBrowserFlowParams) SetHTTPClient(client *http.Client) { + o.HTTPClient = client +} + +// WriteToRequest writes these params to a swagger request +func (o *InitializeSelfServiceRegistrationViaBrowserFlowParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error { + + if err := r.SetTimeout(o.timeout); err != nil { + return err + } + var res []error + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/internal/httpclient/client/public/initialize_self_service_registration_via_browser_flow_responses.go b/internal/httpclient/client/public/initialize_self_service_registration_via_browser_flow_responses.go new file mode 100644 index 000000000000..1400f4000332 --- /dev/null +++ b/internal/httpclient/client/public/initialize_self_service_registration_via_browser_flow_responses.go @@ -0,0 +1,97 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package public + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "fmt" + "io" + + "github.com/go-openapi/runtime" + "github.com/go-openapi/strfmt" + + "github.com/ory/kratos/internal/httpclient/models" +) + +// InitializeSelfServiceRegistrationViaBrowserFlowReader is a Reader for the InitializeSelfServiceRegistrationViaBrowserFlow structure. +type InitializeSelfServiceRegistrationViaBrowserFlowReader struct { + formats strfmt.Registry +} + +// ReadResponse reads a server response into the received o. +func (o *InitializeSelfServiceRegistrationViaBrowserFlowReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) { + switch response.Code() { + case 302: + result := NewInitializeSelfServiceRegistrationViaBrowserFlowFound() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result + case 500: + result := NewInitializeSelfServiceRegistrationViaBrowserFlowInternalServerError() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result + + default: + return nil, runtime.NewAPIError("response status code does not match any response statuses defined for this endpoint in the swagger spec", response, response.Code()) + } +} + +// NewInitializeSelfServiceRegistrationViaBrowserFlowFound creates a InitializeSelfServiceRegistrationViaBrowserFlowFound with default headers values +func NewInitializeSelfServiceRegistrationViaBrowserFlowFound() *InitializeSelfServiceRegistrationViaBrowserFlowFound { + return &InitializeSelfServiceRegistrationViaBrowserFlowFound{} +} + +/*InitializeSelfServiceRegistrationViaBrowserFlowFound handles this case with default header values. + +Empty responses are sent when, for example, resources are deleted. The HTTP status code for empty responses is +typically 201. +*/ +type InitializeSelfServiceRegistrationViaBrowserFlowFound struct { +} + +func (o *InitializeSelfServiceRegistrationViaBrowserFlowFound) Error() string { + return fmt.Sprintf("[GET /self-service/registration/browser][%d] initializeSelfServiceRegistrationViaBrowserFlowFound ", 302) +} + +func (o *InitializeSelfServiceRegistrationViaBrowserFlowFound) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + return nil +} + +// NewInitializeSelfServiceRegistrationViaBrowserFlowInternalServerError creates a InitializeSelfServiceRegistrationViaBrowserFlowInternalServerError with default headers values +func NewInitializeSelfServiceRegistrationViaBrowserFlowInternalServerError() *InitializeSelfServiceRegistrationViaBrowserFlowInternalServerError { + return &InitializeSelfServiceRegistrationViaBrowserFlowInternalServerError{} +} + +/*InitializeSelfServiceRegistrationViaBrowserFlowInternalServerError handles this case with default header values. + +genericError +*/ +type InitializeSelfServiceRegistrationViaBrowserFlowInternalServerError struct { + Payload *models.GenericError +} + +func (o *InitializeSelfServiceRegistrationViaBrowserFlowInternalServerError) Error() string { + return fmt.Sprintf("[GET /self-service/registration/browser][%d] initializeSelfServiceRegistrationViaBrowserFlowInternalServerError %+v", 500, o.Payload) +} + +func (o *InitializeSelfServiceRegistrationViaBrowserFlowInternalServerError) GetPayload() *models.GenericError { + return o.Payload +} + +func (o *InitializeSelfServiceRegistrationViaBrowserFlowInternalServerError) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.GenericError) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + return nil +} diff --git a/internal/httpclient/models/registration_flow.go b/internal/httpclient/models/registration_flow.go new file mode 100644 index 000000000000..e99afeff9b45 --- /dev/null +++ b/internal/httpclient/models/registration_flow.go @@ -0,0 +1,227 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "github.com/go-openapi/errors" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" + "github.com/go-openapi/validate" +) + +// RegistrationFlow registration flow +// +// swagger:model registrationFlow +type RegistrationFlow struct { + + // active + Active CredentialsType `json:"active,omitempty"` + + // ExpiresAt is the time (UTC) when the flow expires. If the user still wishes to log in, + // a new flow has to be initiated. + // Required: true + // Format: date-time + ExpiresAt *strfmt.DateTime `json:"expires_at"` + + // id + // Required: true + // Format: uuid4 + ID UUID `json:"id"` + + // IssuedAt is the time (UTC) when the flow occurred. + // Required: true + // Format: date-time + IssuedAt *strfmt.DateTime `json:"issued_at"` + + // messages + Messages Messages `json:"messages,omitempty"` + + // Methods contains context for all enabled registration methods. If a registration flow has been + // processed, but for example the password is incorrect, this will contain error messages. + // Required: true + Methods map[string]RegistrationRequestMethod `json:"methods"` + + // RequestURL is the initial URL that was requested from ORY Kratos. It can be used + // to forward information contained in the URL's path or query for example. + // Required: true + RequestURL *string `json:"request_url"` + + // type + Type Type `json:"type,omitempty"` +} + +// Validate validates this registration flow +func (m *RegistrationFlow) Validate(formats strfmt.Registry) error { + var res []error + + if err := m.validateActive(formats); err != nil { + res = append(res, err) + } + + if err := m.validateExpiresAt(formats); err != nil { + res = append(res, err) + } + + if err := m.validateID(formats); err != nil { + res = append(res, err) + } + + if err := m.validateIssuedAt(formats); err != nil { + res = append(res, err) + } + + if err := m.validateMessages(formats); err != nil { + res = append(res, err) + } + + if err := m.validateMethods(formats); err != nil { + res = append(res, err) + } + + if err := m.validateRequestURL(formats); err != nil { + res = append(res, err) + } + + if err := m.validateType(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *RegistrationFlow) validateActive(formats strfmt.Registry) error { + + if swag.IsZero(m.Active) { // not required + return nil + } + + if err := m.Active.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("active") + } + return err + } + + return nil +} + +func (m *RegistrationFlow) validateExpiresAt(formats strfmt.Registry) error { + + if err := validate.Required("expires_at", "body", m.ExpiresAt); err != nil { + return err + } + + if err := validate.FormatOf("expires_at", "body", "date-time", m.ExpiresAt.String(), formats); err != nil { + return err + } + + return nil +} + +func (m *RegistrationFlow) validateID(formats strfmt.Registry) error { + + if err := m.ID.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("id") + } + return err + } + + return nil +} + +func (m *RegistrationFlow) validateIssuedAt(formats strfmt.Registry) error { + + if err := validate.Required("issued_at", "body", m.IssuedAt); err != nil { + return err + } + + if err := validate.FormatOf("issued_at", "body", "date-time", m.IssuedAt.String(), formats); err != nil { + return err + } + + return nil +} + +func (m *RegistrationFlow) validateMessages(formats strfmt.Registry) error { + + if swag.IsZero(m.Messages) { // not required + return nil + } + + if err := m.Messages.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("messages") + } + return err + } + + return nil +} + +func (m *RegistrationFlow) validateMethods(formats strfmt.Registry) error { + + for k := range m.Methods { + + if err := validate.Required("methods"+"."+k, "body", m.Methods[k]); err != nil { + return err + } + if val, ok := m.Methods[k]; ok { + if err := val.Validate(formats); err != nil { + return err + } + } + + } + + return nil +} + +func (m *RegistrationFlow) validateRequestURL(formats strfmt.Registry) error { + + if err := validate.Required("request_url", "body", m.RequestURL); err != nil { + return err + } + + return nil +} + +func (m *RegistrationFlow) validateType(formats strfmt.Registry) error { + + if swag.IsZero(m.Type) { // not required + return nil + } + + if err := m.Type.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("type") + } + return err + } + + return nil +} + +// MarshalBinary interface implementation +func (m *RegistrationFlow) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *RegistrationFlow) UnmarshalBinary(b []byte) error { + var res RegistrationFlow + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/internal/testhelpers/login.go b/internal/testhelpers/login.go index 097242afb96d..8e30bf5cb71b 100644 --- a/internal/testhelpers/login.go +++ b/internal/testhelpers/login.go @@ -24,3 +24,14 @@ func NewLoginUIFlowEchoServer(t *testing.T, reg driver.Registry) *httptest.Serve t.Cleanup(ts.Close) return ts } + +func NewRegistrationUIFlowEchoServer(t *testing.T, reg driver.Registry) *httptest.Server { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + e, err := reg.RegistrationFlowPersister().GetRegistrationFlow(r.Context(), x.ParseUUID(r.URL.Query().Get("flow"))) + require.NoError(t, err) + reg.Writer().Write(w, r, e) + })) + viper.Set(configuration.ViperKeySelfServiceRegistrationUI, ts.URL+"/registration-ts") + t.Cleanup(ts.Close) + return ts +} diff --git a/selfservice/flow/login/handler_test.go b/selfservice/flow/login/handler_test.go index c7096e1e1a5f..222bebce1d3f 100644 --- a/selfservice/flow/login/handler_test.go +++ b/selfservice/flow/login/handler_test.go @@ -22,6 +22,7 @@ import ( "github.com/ory/kratos/identity" "github.com/ory/kratos/internal" "github.com/ory/kratos/internal/testhelpers" + "github.com/ory/kratos/selfservice/flow" "github.com/ory/kratos/selfservice/flow/login" "github.com/ory/kratos/x" ) @@ -175,6 +176,7 @@ func TestGetFlow(t *testing.T) { IssuedAt: time.Now().Add(-time.Minute * 2), RequestURL: public.URL + login.RouteInitBrowserFlow, CSRFToken: x.FakeCSRFToken, + Type: flow.TypeBrowser, } } diff --git a/selfservice/flow/registration/flow.go b/selfservice/flow/registration/flow.go index a0e4aeedc43c..f6c2dc511c60 100644 --- a/selfservice/flow/registration/flow.go +++ b/selfservice/flow/registration/flow.go @@ -2,6 +2,7 @@ package registration import ( "net/http" + "net/url" "time" "github.com/gobuffalo/pop/v5" @@ -135,3 +136,7 @@ func (r *Flow) Valid() error { } return nil } + +func (f *Flow) AppendTo(src *url.URL) *url.URL { + return urlx.CopyWithQuery(src, url.Values{"flow": {f.ID.String()}}) +} diff --git a/selfservice/flow/registration/handler.go b/selfservice/flow/registration/handler.go index 9e89a2330e95..81e5ca26b687 100644 --- a/selfservice/flow/registration/handler.go +++ b/selfservice/flow/registration/handler.go @@ -2,7 +2,6 @@ package registration import ( "net/http" - "net/url" "time" "github.com/julienschmidt/httprouter" @@ -150,7 +149,7 @@ func (h *Handler) initBrowserFlow(w http.ResponseWriter, r *http.Request, ps htt return } - redirTo := urlx.CopyWithQuery(h.c.SelfServiceFlowRegisterUI(), url.Values{"request": {a.ID.String()}}).String() + redirTo := a.AppendTo(h.c.SelfServiceFlowRegisterUI()).String() if _, err := h.d.SessionManager().FetchFromRequest(r.Context(), r); err == nil { redirTo = h.c.SelfServiceBrowserDefaultReturnTo().String() } @@ -167,7 +166,7 @@ type getSelfServiceRegistrationFlow struct { // // required: true // in: query - Flow string `json:"flow"` + ID string `json:"id"` } // swagger:route GET /self-service/registration/flows common public admin getSelfServiceRegistrationFlow @@ -190,7 +189,7 @@ type getSelfServiceRegistrationFlow struct { // 410: genericError // 500: genericError func (h *Handler) fetchFlow(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - ar, err := h.d.RegistrationFlowPersister().GetRegistrationFlow(r.Context(), x.ParseUUID(r.URL.Query().Get("flow"))) + ar, err := h.d.RegistrationFlowPersister().GetRegistrationFlow(r.Context(), x.ParseUUID(r.URL.Query().Get("id"))) if err != nil { h.d.Writer().WriteError(w, r, err) return diff --git a/selfservice/flow/registration/handler_test.go b/selfservice/flow/registration/handler_test.go index 92487abdf99c..4334e90fe2b2 100644 --- a/selfservice/flow/registration/handler_test.go +++ b/selfservice/flow/registration/handler_test.go @@ -2,13 +2,14 @@ package registration_test import ( "context" + "encoding/json" + "io/ioutil" "net/http" - "net/http/cookiejar" "net/http/httptest" "testing" "time" - "github.com/justinas/nosurf" + "github.com/ory/x/assertx" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/tidwall/gjson" @@ -19,6 +20,7 @@ import ( "github.com/ory/kratos/identity" "github.com/ory/kratos/internal" "github.com/ory/kratos/internal/testhelpers" + "github.com/ory/kratos/selfservice/flow" "github.com/ory/kratos/selfservice/flow/registration" "github.com/ory/kratos/x" ) @@ -33,40 +35,108 @@ func TestHandlerRedirectOnAuthenticated(t *testing.T) { router := x.NewRouterPublic() ts, _ := testhelpers.NewKratosServerWithRouters(t, reg, router, x.NewRouterAdmin()) - testhelpers.NewRedirTS(t, "already authenticated") + redirTS := testhelpers.NewRedirTS(t, "already authenticated") viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/identity.schema.json") t.Run("does redirect to default on authenticated request", func(t *testing.T) { - body, _ := testhelpers.MockMakeAuthenticatedRequest(t, reg, conf, router.Router, x.NewTestHTTPRequest(t, "GET", ts.URL+registration.RouteInitBrowserFlow, nil)) + body, res := testhelpers.MockMakeAuthenticatedRequest(t, reg, conf, router.Router, x.NewTestHTTPRequest(t, "GET", ts.URL+registration.RouteInitBrowserFlow, nil)) + assert.Contains(t, res.Request.URL.String(), redirTS.URL) assert.EqualValues(t, "already authenticated", string(body)) }) + + t.Run("does redirect to default on authenticated request", func(t *testing.T) { + body, res := testhelpers.MockMakeAuthenticatedRequest(t, reg, conf, router.Router, x.NewTestHTTPRequest(t, "GET", ts.URL+registration.RouteInitAPIFlow, nil)) + assert.Contains(t, res.Request.URL.String(), registration.RouteInitAPIFlow) + assertx.EqualAsJSON(t, registration.ErrAlreadyLoggedIn, json.RawMessage(gjson.GetBytes(body, "error").Raw)) + }) } -func TestRegistrationHandler(t *testing.T) { +func TestInitFlow(t *testing.T) { + conf, reg := internal.NewFastRegistryWithMocks(t) + viper.Set(configuration.ViperKeySelfServiceStrategyConfig+"."+string(identity.CredentialsTypePassword), + map[string]interface{}{"enabled": true}) + + router := x.NewRouterPublic() + publicTS, _ := testhelpers.NewKratosServerWithRouters(t, reg, router, x.NewRouterAdmin()) + registrationTS := testhelpers.NewRegistrationUIFlowEchoServer(t, reg) + + viper.Set(configuration.ViperKeySelfServiceBrowserDefaultReturnTo, "https://www.ory.sh") + viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/login.schema.json") + + assertion := func(body []byte, isForced, isApi bool) { + if isApi { + assert.Equal(t, "api", gjson.GetBytes(body, "type").String()) + } else { + assert.Equal(t, "browser", gjson.GetBytes(body, "type").String()) + } + } + + initAuthenticatedFlow := func(t *testing.T, isAPI bool) (*http.Response, []byte) { + route := registration.RouteInitBrowserFlow + if isAPI { + route = registration.RouteInitAPIFlow + } + req := x.NewTestHTTPRequest(t, "GET", publicTS.URL+route, nil) + body, res := testhelpers.MockMakeAuthenticatedRequest(t, reg, conf, router.Router, req) + return res, body + } + + initFlow := func(t *testing.T, isAPI bool) (*http.Response, []byte) { + route := registration.RouteInitBrowserFlow + if isAPI { + route = registration.RouteInitAPIFlow + } + c := publicTS.Client() + res, err := c.Get(publicTS.URL + route) + require.NoError(t, err) + defer res.Body.Close() + body, err := ioutil.ReadAll(res.Body) + require.NoError(t, err) + return res, body + } + + t.Run("flow=api", func(t *testing.T) { + t.Run("case=creates a new flow on unauthenticated request", func(t *testing.T) { + res, body := initFlow(t,true) + assert.Contains(t, res.Request.URL.String(), registration.RouteInitAPIFlow) + assertion(body, false, true) + }) + + t.Run("case=fails on authenticated request", func(t *testing.T) { + res, body := initAuthenticatedFlow(t, true) + assert.Equal(t, http.StatusBadRequest, res.StatusCode) + assertx.EqualAsJSON(t, registration.ErrAlreadyLoggedIn, json.RawMessage(gjson.GetBytes(body, "error").Raw), "%s", body) + }) + }) + + t.Run("flow=browser", func(t *testing.T) { + t.Run("case=does not set forced flag on unauthenticated request", func(t *testing.T) { + res, body := initFlow(t, false) + assertion(body, false, false) + assert.Contains(t, res.Request.URL.String(), registrationTS.URL) + }) + t.Run("case=does not set forced flag on authenticated request with refresh=false", func(t *testing.T) { + res, _ := initAuthenticatedFlow(t, false) + assert.Contains(t, res.Request.URL.String(), "https://www.ory.sh") + }) + }) +} + +func TestGetFlow(t *testing.T) { _, reg := internal.NewFastRegistryWithMocks(t) + viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/registration.schema.json") - public, admin := func() (*httptest.Server, *httptest.Server) { - public := x.NewRouterPublic() - admin := x.NewRouterAdmin() - reg.RegistrationHandler().RegisterPublicRoutes(public) - reg.RegistrationHandler().RegisterAdminRoutes(admin) - reg.RegistrationStrategies().RegisterPublicRoutes(public) - return httptest.NewServer(x.NewTestCSRFHandler(public, reg)), httptest.NewServer(admin) - }() - defer public.Close() - defer admin.Close() - - redirTS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusNoContent) - })) - defer redirTS.Close() + public, admin := testhelpers.NewKratosServerWithCSRF(t, reg) + _ = testhelpers.NewErrorTestServer(t, reg) + _ = testhelpers.NewRedirTS(t, "") newRegistrationTS := func(t *testing.T, upstream string, c *http.Client) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if c == nil { c = http.DefaultClient } - _, _ = w.Write(x.EasyGetBody(t, c, upstream+registration.RouteGetFlow+"?request="+r.URL.Query().Get("request"))) + _, err := w.Write(x.EasyGetBody(t, c, upstream+registration.RouteGetFlow+"?id="+r.URL.Query().Get("flow"))) + require.NoError(t, err) })) } @@ -84,84 +154,41 @@ func TestRegistrationHandler(t *testing.T) { assert.Equal(t, public.URL+registration.RouteInitBrowserFlow, gjson.GetBytes(body, "error.details.redirect_to").String(), "%s", body) } - newExpiredFLow := func() *registration.Flow { + newExpiredFlow := func() *registration.Flow { return ®istration.Flow{ ID: x.NewUUID(), ExpiresAt: time.Now().Add(-time.Minute), IssuedAt: time.Now().Add(-time.Minute * 2), RequestURL: public.URL + registration.RouteInitBrowserFlow, CSRFToken: x.FakeCSRFToken, + Type: flow.TypeBrowser, } } - errTS := testhelpers.NewErrorTestServer(t, reg) - defer errTS.Close() - - viper.Set(configuration.ViperKeyPublicBaseURL, public.URL) - viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/registration.schema.json") - viper.Set(configuration.ViperKeySelfServiceErrorUI, errTS.URL) - viper.Set(configuration.ViperKeySelfServiceStrategyConfig+"."+string(identity.CredentialsTypePassword), map[string]interface{}{ - "enabled": true}) - - t.Run("daemon=admin", func(t *testing.T) { - regTS := newRegistrationTS(t, admin.URL, nil) - defer regTS.Close() - viper.Set(configuration.ViperKeySelfServiceRegistrationUI, regTS.URL) + run := func(t *testing.T, endpoint *httptest.Server) { + registrationTS := newRegistrationTS(t, endpoint.URL, nil) + defer registrationTS.Close() + viper.Set(configuration.ViperKeySelfServiceRegistrationUI, registrationTS.URL) + viper.Set(configuration.ViperKeySelfServiceStrategyConfig+"."+string(identity.CredentialsTypePassword), + map[string]interface{}{"enabled": true}) t.Run("case=valid", func(t *testing.T) { - assertFlowPayload(t, x.EasyGetBody(t, public.Client(), public.URL+registration.RouteInitBrowserFlow)) + assertFlowPayload(t, x.EasyGetBody(t, endpoint.Client(), public.URL+registration.RouteInitBrowserFlow)) }) t.Run("case=expired", func(t *testing.T) { - rr := newExpiredFLow() - require.NoError(t, reg.RegistrationFlowPersister().CreateRegistrationFlow(context.Background(), rr)) - res, body := x.EasyGet(t, admin.Client(), admin.URL+registration.RouteGetFlow+"?request="+rr.ID.String()) + lr := newExpiredFlow() + require.NoError(t, reg.RegistrationFlowPersister().CreateRegistrationFlow(context.Background(), lr)) + res, body := x.EasyGet(t, admin.Client(), endpoint.URL+registration.RouteGetFlow+"?id="+lr.ID.String()) assertExpiredPayload(t, res, body) }) + } + + t.Run("daemon=admin", func(t *testing.T) { + run(t, admin) }) t.Run("daemon=public", func(t *testing.T) { - t.Run("case=with_csrf", func(t *testing.T) { - j, err := cookiejar.New(nil) - require.NoError(t, err) - hc := &http.Client{Jar: j} - - regTS := newRegistrationTS(t, public.URL, hc) - defer regTS.Close() - viper.Set(configuration.ViperKeySelfServiceRegistrationUI, regTS.URL) - - body := x.EasyGetBody(t, hc, public.URL+registration.RouteInitBrowserFlow) - assertFlowPayload(t, body) - }) - - t.Run("case=without_csrf", func(t *testing.T) { - regTS := newRegistrationTS(t, public.URL, - // using a different client because it doesn't have access to the cookie jar - new(http.Client)) - defer regTS.Close() - viper.Set(configuration.ViperKeySelfServiceRegistrationUI, regTS.URL) - - body := x.EasyGetBody(t, new(http.Client), public.URL+registration.RouteInitBrowserFlow) - assert.Contains(t, gjson.GetBytes(body, "error").String(), "csrf_token", "%s", body) - }) - - t.Run("case=expired", func(t *testing.T) { - reg.WithCSRFTokenGenerator(x.FakeCSRFTokenGenerator) - t.Cleanup(func() { - reg.WithCSRFTokenGenerator(nosurf.Token) - }) - - j, err := cookiejar.New(nil) - require.NoError(t, err) - hc := &http.Client{Jar: j} - - regTS := newRegistrationTS(t, public.URL, hc) - defer regTS.Close() - - rr := newExpiredFLow() - require.NoError(t, reg.RegistrationFlowPersister().CreateRegistrationFlow(context.Background(), rr)) - res, body := x.EasyGet(t, admin.Client(), admin.URL+registration.RouteGetFlow+"?request="+rr.ID.String()) - assertExpiredPayload(t, res, body) - }) + run(t, public) }) }