2021-11-30 11:15:21 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
2022-10-03 00:52:29 +00:00
|
|
|
"crypto/ecdsa"
|
2022-10-02 02:07:40 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
2021-11-30 11:15:21 +00:00
|
|
|
"fmt"
|
|
|
|
"net/mail"
|
2022-03-16 05:16:46 +00:00
|
|
|
"net/url"
|
2021-11-30 11:15:21 +00:00
|
|
|
"reflect"
|
2022-04-01 11:38:49 +00:00
|
|
|
"regexp"
|
2023-05-08 03:30:49 +00:00
|
|
|
"strconv"
|
2022-10-20 03:21:45 +00:00
|
|
|
"strings"
|
2022-03-02 06:40:26 +00:00
|
|
|
"time"
|
2021-11-30 11:15:21 +00:00
|
|
|
|
2022-12-04 22:37:08 +00:00
|
|
|
"github.com/go-crypt/crypt/algorithm/plaintext"
|
2021-11-30 11:15:21 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2022-03-02 06:40:26 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-11-30 11:15:21 +00:00
|
|
|
)
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
// StringToMailAddressHookFunc decodes a string into a mail.Address or *mail.Address.
|
2022-03-16 05:16:46 +00:00
|
|
|
func StringToMailAddressHookFunc() mapstructure.DecodeHookFuncType {
|
2022-10-05 05:05:23 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-04-03 12:44:52 +00:00
|
|
|
var ptr bool
|
|
|
|
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:07:40 +00:00
|
|
|
prefixType := ""
|
2022-04-03 12:44:52 +00:00
|
|
|
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
ptr = true
|
2022-10-02 02:07:40 +00:00
|
|
|
prefixType = "*"
|
2022-04-03 12:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedType := reflect.TypeOf(mail.Address{})
|
|
|
|
|
|
|
|
if ptr && t.Elem() != expectedType {
|
|
|
|
return data, nil
|
|
|
|
} else if !ptr && t != expectedType {
|
2021-11-30 11:15:21 +00:00
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dataStr := data.(string)
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
var result *mail.Address
|
|
|
|
|
|
|
|
if dataStr != "" {
|
|
|
|
if result, err = mail.ParseAddress(dataStr); err != nil {
|
2022-10-02 02:07:40 +00:00
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType.String()+" (RFC5322)", err)
|
2022-04-03 12:44:52 +00:00
|
|
|
}
|
2021-11-30 11:15:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
if ptr {
|
|
|
|
return result, nil
|
|
|
|
}
|
2021-11-30 11:15:21 +00:00
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
if result == nil {
|
|
|
|
return mail.Address{}, nil
|
2021-11-30 11:15:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
return *result, nil
|
2021-11-30 11:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-02 06:40:26 +00:00
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
// StringToURLHookFunc converts string types into a url.URL or *url.URL.
|
2022-03-16 05:16:46 +00:00
|
|
|
func StringToURLHookFunc() mapstructure.DecodeHookFuncType {
|
2022-10-05 05:05:23 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-03-16 05:16:46 +00:00
|
|
|
var ptr bool
|
|
|
|
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:07:40 +00:00
|
|
|
prefixType := ""
|
2022-03-16 05:16:46 +00:00
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
ptr = true
|
2022-10-02 02:07:40 +00:00
|
|
|
prefixType = "*"
|
2022-04-03 12:44:52 +00:00
|
|
|
}
|
2022-03-16 05:16:46 +00:00
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
expectedType := reflect.TypeOf(url.URL{})
|
|
|
|
|
|
|
|
if ptr && t.Elem() != expectedType {
|
2022-03-16 05:16:46 +00:00
|
|
|
return data, nil
|
2022-04-03 12:44:52 +00:00
|
|
|
} else if !ptr && t != expectedType {
|
2022-03-16 05:16:46 +00:00
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dataStr := data.(string)
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
var result *url.URL
|
2022-03-16 05:16:46 +00:00
|
|
|
|
|
|
|
if dataStr != "" {
|
2022-04-03 12:44:52 +00:00
|
|
|
if result, err = url.Parse(dataStr); err != nil {
|
2022-10-02 02:07:40 +00:00
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err)
|
2022-03-16 05:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
2022-04-03 12:44:52 +00:00
|
|
|
return result, nil
|
2022-03-16 05:16:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
if result == nil {
|
2022-03-16 05:16:46 +00:00
|
|
|
return url.URL{}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
return *result, nil
|
2022-03-16 05:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToTimeDurationHookFunc converts string and integer types to a time.Duration.
|
2023-05-08 03:30:49 +00:00
|
|
|
//
|
|
|
|
//nolint:gocyclo // Function is necessarily complex though flows well due to switch statement usage.
|
2022-03-16 05:16:46 +00:00
|
|
|
func ToTimeDurationHookFunc() mapstructure.DecodeHookFuncType {
|
2022-10-05 05:05:23 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2023-05-08 03:30:49 +00:00
|
|
|
var (
|
|
|
|
ptr bool
|
|
|
|
prefixType string
|
|
|
|
)
|
2022-03-02 06:40:26 +00:00
|
|
|
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
ptr = true
|
2022-10-02 02:07:40 +00:00
|
|
|
prefixType = "*"
|
2022-04-03 12:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedType := reflect.TypeOf(time.Duration(0))
|
|
|
|
|
|
|
|
if ptr && t.Elem() != expectedType {
|
|
|
|
return data, nil
|
|
|
|
} else if !ptr && t != expectedType {
|
2022-03-02 06:40:26 +00:00
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 03:30:49 +00:00
|
|
|
switch f.Kind() {
|
|
|
|
case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float64:
|
|
|
|
// We only allow string and integer from kinds to match.
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
var result time.Duration
|
2022-03-02 06:40:26 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case f.Kind() == reflect.String:
|
2022-03-05 05:51:41 +00:00
|
|
|
dataStr := data.(string)
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
if result, err = utils.ParseDurationString(dataStr); err != nil {
|
2022-10-02 02:07:40 +00:00
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err)
|
2022-03-05 05:51:41 +00:00
|
|
|
}
|
2022-03-02 06:40:26 +00:00
|
|
|
case f.Kind() == reflect.Int:
|
|
|
|
seconds := data.(int)
|
|
|
|
|
2023-05-08 03:30:49 +00:00
|
|
|
result = time.Second * time.Duration(seconds)
|
|
|
|
case f.Kind() == reflect.Int8:
|
|
|
|
seconds := data.(int8)
|
|
|
|
|
|
|
|
result = time.Second * time.Duration(seconds)
|
|
|
|
case f.Kind() == reflect.Int16:
|
|
|
|
seconds := data.(int16)
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
result = time.Second * time.Duration(seconds)
|
2022-03-02 06:40:26 +00:00
|
|
|
case f.Kind() == reflect.Int32:
|
|
|
|
seconds := data.(int32)
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
result = time.Second * time.Duration(seconds)
|
2023-05-08 03:30:49 +00:00
|
|
|
case f.Kind() == reflect.Float64:
|
|
|
|
fseconds := data.(float64)
|
|
|
|
|
|
|
|
if fseconds > durationMax.Seconds() {
|
|
|
|
result = durationMax
|
|
|
|
} else {
|
|
|
|
seconds, _ := strconv.Atoi(fmt.Sprintf("%.0f", fseconds))
|
|
|
|
|
|
|
|
result = time.Second * time.Duration(seconds)
|
|
|
|
}
|
2022-04-03 12:44:52 +00:00
|
|
|
case f == expectedType:
|
|
|
|
result = data.(time.Duration)
|
2022-03-02 06:40:26 +00:00
|
|
|
case f.Kind() == reflect.Int64:
|
|
|
|
seconds := data.(int64)
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
result = time.Second * time.Duration(seconds)
|
2022-03-02 06:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
2022-04-03 12:44:52 +00:00
|
|
|
return &result, nil
|
2022-03-02 06:40:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
return result, nil
|
2022-03-02 06:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-01 11:38:49 +00:00
|
|
|
|
2022-06-01 23:18:45 +00:00
|
|
|
// StringToRegexpHookFunc decodes a string into a *regexp.Regexp or regexp.Regexp.
|
|
|
|
func StringToRegexpHookFunc() mapstructure.DecodeHookFuncType {
|
2022-10-05 05:05:23 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-04-01 11:38:49 +00:00
|
|
|
var ptr bool
|
|
|
|
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:07:40 +00:00
|
|
|
prefixType := ""
|
2022-04-01 11:38:49 +00:00
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
ptr = true
|
2022-10-02 02:07:40 +00:00
|
|
|
prefixType = "*"
|
2022-04-03 12:44:52 +00:00
|
|
|
}
|
2022-04-01 11:38:49 +00:00
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
expectedType := reflect.TypeOf(regexp.Regexp{})
|
|
|
|
|
|
|
|
if ptr && t.Elem() != expectedType {
|
2022-04-01 11:38:49 +00:00
|
|
|
return data, nil
|
2022-04-03 12:44:52 +00:00
|
|
|
} else if !ptr && t != expectedType {
|
2022-04-01 11:38:49 +00:00
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
dataStr := data.(string)
|
|
|
|
|
|
|
|
var result *regexp.Regexp
|
2022-04-01 11:38:49 +00:00
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
if dataStr != "" {
|
|
|
|
if result, err = regexp.Compile(dataStr); err != nil {
|
2022-10-02 02:07:40 +00:00
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err)
|
2022-04-03 12:44:52 +00:00
|
|
|
}
|
2022-04-01 11:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
2022-04-03 12:44:52 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == nil {
|
2022-10-02 02:07:40 +00:00
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseEmptyValue, prefixType, expectedType, errDecodeNonPtrMustHaveValue)
|
2022-04-01 11:38:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 12:44:52 +00:00
|
|
|
return *result, nil
|
2022-04-01 11:38:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-14 07:20:13 +00:00
|
|
|
|
|
|
|
// StringToAddressHookFunc decodes a string into an Address or *Address.
|
2023-05-07 05:48:26 +00:00
|
|
|
//
|
|
|
|
//nolint:gocyclo // This is an adequately clear function even with the complexity.
|
2022-06-14 07:20:13 +00:00
|
|
|
func StringToAddressHookFunc() mapstructure.DecodeHookFuncType {
|
2022-10-05 05:05:23 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-06-14 07:20:13 +00:00
|
|
|
var ptr bool
|
|
|
|
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:07:40 +00:00
|
|
|
prefixType := ""
|
2022-06-14 07:20:13 +00:00
|
|
|
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
ptr = true
|
2022-10-02 02:07:40 +00:00
|
|
|
prefixType = "*"
|
2022-06-14 07:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedType := reflect.TypeOf(schema.Address{})
|
2023-05-07 05:48:26 +00:00
|
|
|
expectedTypeTCP := reflect.TypeOf(schema.AddressTCP{})
|
|
|
|
expectedTypeUDP := reflect.TypeOf(schema.AddressUDP{})
|
|
|
|
expectedTypeLDAP := reflect.TypeOf(schema.AddressLDAP{})
|
|
|
|
expectedTypeSMTP := reflect.TypeOf(schema.AddressSMTP{})
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2023-05-07 05:48:26 +00:00
|
|
|
switch {
|
|
|
|
case ptr:
|
|
|
|
switch t.Elem() {
|
|
|
|
case expectedType:
|
|
|
|
case expectedTypeTCP:
|
|
|
|
expectedType = expectedTypeTCP
|
|
|
|
case expectedTypeUDP:
|
|
|
|
expectedType = expectedTypeUDP
|
|
|
|
case expectedTypeLDAP:
|
|
|
|
expectedType = expectedTypeLDAP
|
|
|
|
case expectedTypeSMTP:
|
|
|
|
expectedType = expectedTypeSMTP
|
|
|
|
default:
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
switch t {
|
|
|
|
case expectedType:
|
|
|
|
break
|
|
|
|
case expectedTypeTCP:
|
|
|
|
expectedType = expectedTypeTCP
|
|
|
|
case expectedTypeUDP:
|
|
|
|
expectedType = expectedTypeUDP
|
|
|
|
case expectedTypeLDAP:
|
|
|
|
expectedType = expectedTypeLDAP
|
|
|
|
case expectedTypeSMTP:
|
|
|
|
expectedType = expectedTypeSMTP
|
|
|
|
default:
|
|
|
|
return data, nil
|
|
|
|
}
|
2022-06-14 07:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dataStr := data.(string)
|
|
|
|
|
|
|
|
var result *schema.Address
|
|
|
|
|
2023-05-07 05:48:26 +00:00
|
|
|
switch expectedType {
|
|
|
|
case expectedTypeTCP:
|
|
|
|
if result, err = schema.NewAddressDefault(dataStr, schema.AddressSchemeTCP, schema.AddressSchemeUnix); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err)
|
|
|
|
}
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2023-05-07 05:48:26 +00:00
|
|
|
if ptr {
|
|
|
|
return &schema.AddressTCP{Address: *result}, nil
|
|
|
|
}
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2023-05-07 05:48:26 +00:00
|
|
|
return schema.AddressTCP{Address: *result}, nil
|
|
|
|
case expectedTypeUDP:
|
|
|
|
if result, err = schema.NewAddressDefault(dataStr, schema.AddressSchemeUDP, schema.AddressSchemeUnix); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
|
|
|
return &schema.AddressUDP{Address: *result}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema.AddressUDP{Address: *result}, nil
|
|
|
|
case expectedTypeLDAP:
|
2023-05-07 06:39:17 +00:00
|
|
|
if result, err = schema.NewAddressDefault(dataStr, schema.AddressSchemeLDAPS, schema.AddressSchemeLDAPI); err != nil {
|
2023-05-07 05:48:26 +00:00
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
|
|
|
return &schema.AddressLDAP{Address: *result}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema.AddressLDAP{Address: *result}, nil
|
|
|
|
case expectedTypeSMTP:
|
|
|
|
if result, err = schema.NewAddressDefault(dataStr, schema.AddressSchemeSMTP, schema.AddressSchemeUnix); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
|
|
|
return &schema.AddressSMTP{Address: *result}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema.AddressSMTP{Address: *result}, nil
|
|
|
|
default:
|
|
|
|
if result, err = schema.NewAddress(dataStr); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result, nil
|
|
|
|
}
|
2022-06-14 07:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-02 02:07:40 +00:00
|
|
|
|
|
|
|
// StringToX509CertificateHookFunc decodes strings to x509.Certificate's.
|
|
|
|
func StringToX509CertificateHookFunc() mapstructure.DecodeHookFuncType {
|
2023-04-15 12:35:44 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-10-02 02:07:40 +00:00
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Kind() != reflect.Ptr {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedType := reflect.TypeOf(x509.Certificate{})
|
|
|
|
|
|
|
|
if t.Elem() != expectedType {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dataStr := data.(string)
|
|
|
|
|
|
|
|
var result *x509.Certificate
|
|
|
|
|
|
|
|
if dataStr == "" {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-04-15 12:35:44 +00:00
|
|
|
var i any
|
2022-10-02 02:07:40 +00:00
|
|
|
|
|
|
|
if i, err = utils.ParseX509FromPEM([]byte(dataStr)); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "*", expectedType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r := i.(type) {
|
|
|
|
case *x509.Certificate:
|
|
|
|
return r, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "*", expectedType, fmt.Errorf("the data is for a %T not a *%s", r, expectedType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringToX509CertificateChainHookFunc decodes strings to schema.X509CertificateChain's.
|
|
|
|
func StringToX509CertificateChainHookFunc() mapstructure.DecodeHookFuncType {
|
2023-04-15 12:35:44 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-10-02 02:07:40 +00:00
|
|
|
var ptr bool
|
|
|
|
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixType := ""
|
|
|
|
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
ptr = true
|
|
|
|
prefixType = "*"
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedType := reflect.TypeOf(schema.X509CertificateChain{})
|
|
|
|
|
|
|
|
if ptr && t.Elem() != expectedType {
|
|
|
|
return data, nil
|
|
|
|
} else if !ptr && t != expectedType {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dataStr := data.(string)
|
|
|
|
|
|
|
|
var result *schema.X509CertificateChain
|
|
|
|
|
|
|
|
if dataStr == "" && ptr {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, err = schema.NewX509CertificateChain(dataStr); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, prefixType, expectedType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
return schema.X509CertificateChain{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:41:33 +00:00
|
|
|
// StringToTLSVersionHookFunc decodes strings to schema.TLSVersion's.
|
|
|
|
func StringToTLSVersionHookFunc() mapstructure.DecodeHookFuncType {
|
2023-04-15 12:35:44 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-10-21 08:41:33 +00:00
|
|
|
var ptr bool
|
|
|
|
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixType := ""
|
|
|
|
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
ptr = true
|
|
|
|
prefixType = "*"
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedType := reflect.TypeOf(schema.TLSVersion{})
|
|
|
|
|
|
|
|
if ptr && t.Elem() != expectedType {
|
|
|
|
return data, nil
|
|
|
|
} else if !ptr && t != expectedType {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dataStr := data.(string)
|
|
|
|
|
|
|
|
var result *schema.TLSVersion
|
|
|
|
|
|
|
|
if result, err = schema.NewTLSVersion(dataStr); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringToCryptoPrivateKeyHookFunc decodes strings to schema.CryptographicPrivateKey's.
|
|
|
|
func StringToCryptoPrivateKeyHookFunc() mapstructure.DecodeHookFuncType {
|
2023-04-15 12:35:44 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-10-21 08:41:33 +00:00
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
field, _ := reflect.TypeOf(schema.TLSConfig{}).FieldByName("PrivateKey")
|
|
|
|
expectedType := field.Type
|
|
|
|
|
|
|
|
if t != expectedType {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dataStr := data.(string)
|
|
|
|
|
|
|
|
var i any
|
|
|
|
|
|
|
|
if i, err = utils.ParseX509FromPEM([]byte(dataStr)); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "", expectedType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, ok := i.(schema.CryptographicPrivateKey); !ok {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "", expectedType, fmt.Errorf("the data is for a %T not a %s", i, expectedType))
|
|
|
|
} else {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:52:29 +00:00
|
|
|
// StringToPrivateKeyHookFunc decodes strings to rsa.PrivateKey's.
|
|
|
|
func StringToPrivateKeyHookFunc() mapstructure.DecodeHookFuncType {
|
2023-04-15 12:35:44 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-10-02 02:07:40 +00:00
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Kind() != reflect.Ptr {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:52:29 +00:00
|
|
|
expectedTypeRSA := reflect.TypeOf(rsa.PrivateKey{})
|
|
|
|
expectedTypeECDSA := reflect.TypeOf(ecdsa.PrivateKey{})
|
2022-10-02 02:07:40 +00:00
|
|
|
|
2022-10-03 00:52:29 +00:00
|
|
|
var (
|
|
|
|
i any
|
|
|
|
expectedType reflect.Type
|
|
|
|
)
|
2022-10-02 02:07:40 +00:00
|
|
|
|
|
|
|
dataStr := data.(string)
|
|
|
|
|
2022-10-03 00:52:29 +00:00
|
|
|
switch t.Elem() {
|
|
|
|
case expectedTypeRSA:
|
|
|
|
var result *rsa.PrivateKey
|
2022-10-02 02:07:40 +00:00
|
|
|
|
2022-10-03 00:52:29 +00:00
|
|
|
if dataStr == "" {
|
|
|
|
return result, nil
|
|
|
|
}
|
2022-10-02 02:07:40 +00:00
|
|
|
|
2022-10-03 00:52:29 +00:00
|
|
|
expectedType = expectedTypeRSA
|
|
|
|
case expectedTypeECDSA:
|
|
|
|
var result *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
if dataStr == "" {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedType = expectedTypeECDSA
|
|
|
|
default:
|
|
|
|
return data, nil
|
|
|
|
}
|
2022-10-02 02:07:40 +00:00
|
|
|
|
|
|
|
if i, err = utils.ParseX509FromPEM([]byte(dataStr)); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "*", expectedType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r := i.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
2022-10-03 00:52:29 +00:00
|
|
|
if expectedType != expectedTypeRSA {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "*", expectedType, fmt.Errorf("the data is for a %T not a *%s", r, expectedType))
|
|
|
|
}
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
if err = r.Validate(); err != nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "*", expectedType, err)
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:52:29 +00:00
|
|
|
return r, nil
|
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
if expectedType != expectedTypeECDSA {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "*", expectedType, fmt.Errorf("the data is for a %T not a *%s", r, expectedType))
|
|
|
|
}
|
|
|
|
|
2022-10-02 02:07:40 +00:00
|
|
|
return r, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseBasic, "*", expectedType, fmt.Errorf("the data is for a %T not a *%s", r, expectedType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 03:21:45 +00:00
|
|
|
|
|
|
|
// StringToPasswordDigestHookFunc decodes a string into a crypt.Digest.
|
2022-12-04 22:37:08 +00:00
|
|
|
func StringToPasswordDigestHookFunc() mapstructure.DecodeHookFuncType {
|
2023-04-15 12:35:44 +00:00
|
|
|
return func(f reflect.Type, t reflect.Type, data any) (value any, err error) {
|
2022-10-20 03:21:45 +00:00
|
|
|
var ptr bool
|
|
|
|
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixType := ""
|
|
|
|
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
ptr = true
|
|
|
|
prefixType = "*"
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedType := reflect.TypeOf(schema.PasswordDigest{})
|
|
|
|
|
|
|
|
if ptr && t.Elem() != expectedType {
|
|
|
|
return data, nil
|
|
|
|
} else if !ptr && t != expectedType {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dataStr := data.(string)
|
|
|
|
|
|
|
|
var result *schema.PasswordDigest
|
|
|
|
|
|
|
|
if !strings.HasPrefix(dataStr, "$") {
|
2022-12-04 22:37:08 +00:00
|
|
|
dataStr = fmt.Sprintf(plaintext.EncodingFmt, plaintext.AlgIdentifierPlainText, dataStr)
|
2022-10-20 03:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if dataStr != "" {
|
2022-12-04 22:37:08 +00:00
|
|
|
if result, err = schema.DecodePasswordDigest(dataStr); err != nil {
|
2022-10-20 03:21:45 +00:00
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType.String(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ptr {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
return nil, fmt.Errorf(errFmtDecodeHookCouldNotParseEmptyValue, prefixType, expectedType.String(), errDecodeNonPtrMustHaveValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result, nil
|
|
|
|
}
|
|
|
|
}
|