2017-06-17 04:04:55 +00:00
|
|
|
package api
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-03-24 17:58:03 +00:00
|
|
|
"net/http"
|
2018-08-14 19:29:22 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
2018-07-24 22:49:55 +00:00
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
func (c *Sys) CORSStatus() (*CORSResponse, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.CORSStatusWithContext(context.Background())
|
|
|
|
}
|
2018-07-24 22:49:55 +00:00
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
func (c *Sys) CORSStatusWithContext(ctx context.Context) (*CORSResponse, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-24 22:49:55 +00:00
|
|
|
defer cancelFunc()
|
2022-03-23 21:47:43 +00:00
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodGet, "/v1/sys/config/cors")
|
2022-03-23 21:47:43 +00:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2017-06-17 04:04:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
var result CORSResponse
|
2018-08-14 19:29:22 +00:00
|
|
|
err = mapstructure.Decode(secret.Data, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
return &result, err
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:31:25 +00:00
|
|
|
func (c *Sys) ConfigureCORS(req *CORSRequest) error {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.ConfigureCORSWithContext(context.Background(), req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) ConfigureCORSWithContext(ctx context.Context, req *CORSRequest) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/sys/config/cors")
|
2017-06-17 04:04:55 +00:00
|
|
|
if err := r.SetJSONBody(req); err != nil {
|
2020-12-03 13:31:25 +00:00
|
|
|
return err
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2020-12-03 13:31:25 +00:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
2018-08-14 19:29:22 +00:00
|
|
|
}
|
2020-12-03 13:31:25 +00:00
|
|
|
return err
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 13:31:25 +00:00
|
|
|
func (c *Sys) DisableCORS() error {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.DisableCORSWithContext(context.Background())
|
|
|
|
}
|
2017-06-17 04:04:55 +00:00
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
func (c *Sys) DisableCORSWithContext(ctx context.Context) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-24 22:49:55 +00:00
|
|
|
defer cancelFunc()
|
2022-03-23 21:47:43 +00:00
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodDelete, "/v1/sys/config/cors")
|
2022-03-23 21:47:43 +00:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2020-12-03 13:31:25 +00:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
2018-08-14 19:29:22 +00:00
|
|
|
}
|
2020-12-03 13:31:25 +00:00
|
|
|
return err
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CORSRequest struct {
|
2020-12-03 13:31:25 +00:00
|
|
|
AllowedOrigins []string `json:"allowed_origins" mapstructure:"allowed_origins"`
|
|
|
|
AllowedHeaders []string `json:"allowed_headers" mapstructure:"allowed_headers"`
|
|
|
|
Enabled bool `json:"enabled" mapstructure:"enabled"`
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CORSResponse struct {
|
2020-12-03 13:31:25 +00:00
|
|
|
AllowedOrigins []string `json:"allowed_origins" mapstructure:"allowed_origins"`
|
|
|
|
AllowedHeaders []string `json:"allowed_headers" mapstructure:"allowed_headers"`
|
|
|
|
Enabled bool `json:"enabled" mapstructure:"enabled"`
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|