2021-11-17 01:28:35 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
|
|
"github.com/alecthomas/jsonschema"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// SignatureType is the type of signature
|
|
|
|
|
type SignatureType int
|
|
|
|
|
|
|
|
|
|
// Supported values for the SignatureType
|
|
|
|
|
const (
|
2021-11-18 21:58:32 +01:00
|
|
|
AWSSignature SignatureType = iota
|
2021-11-17 01:28:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// signatureTypeMappings is a table for conversion of signature type from string.
|
|
|
|
|
var signatureTypeMappings = map[SignatureType]string{
|
|
|
|
|
AWSSignature: "aws",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetSupportedSignaturesTypes() []SignatureType {
|
|
|
|
|
var result []SignatureType
|
2021-11-18 21:58:32 +01:00
|
|
|
for i := 0; SignatureType(i).String() != ""; i++ {
|
|
|
|
|
result = append(result, SignatureType(i))
|
2021-11-17 01:28:35 +01:00
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toSignatureType(valueToMap string) (SignatureType, error) {
|
|
|
|
|
normalizedValue := normalizeValue(valueToMap)
|
|
|
|
|
for key, currentValue := range signatureTypeMappings {
|
|
|
|
|
if normalizedValue == currentValue {
|
|
|
|
|
return key, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1, errors.New("invalid signature type: " + valueToMap)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t SignatureType) String() string {
|
|
|
|
|
return signatureTypeMappings[t]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SignatureTypeHolder is used to hold internal type of the signature
|
|
|
|
|
type SignatureTypeHolder struct {
|
|
|
|
|
Value SignatureType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (holder SignatureTypeHolder) JSONSchemaType() *jsonschema.Type {
|
|
|
|
|
gotType := &jsonschema.Type{
|
|
|
|
|
Type: "string",
|
|
|
|
|
Title: "type of the signature",
|
|
|
|
|
Description: "Type of the signature",
|
|
|
|
|
}
|
|
|
|
|
for _, types := range GetSupportedSignaturesTypes() {
|
|
|
|
|
gotType.Enum = append(gotType.Enum, types.String())
|
|
|
|
|
}
|
|
|
|
|
return gotType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (holder *SignatureTypeHolder) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
|
var marshalledTypes string
|
|
|
|
|
if err := unmarshal(&marshalledTypes); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
computedType, err := toSignatureType(marshalledTypes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
holder.Value = computedType
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (holder *SignatureTypeHolder) MarshalJSON() ([]byte, error) {
|
|
|
|
|
return json.Marshal(holder.Value.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (holder SignatureTypeHolder) MarshalYAML() (interface{}, error) {
|
|
|
|
|
return holder.Value.String(), nil
|
|
|
|
|
}
|