mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
fe4bfc1c75
This fixes an issue with the mail address decode hook func which previously would attempt to decode any struct type to a mail address.
36 lines
741 B
Go
36 lines
741 B
Go
package configuration
|
|
|
|
import (
|
|
"fmt"
|
|
"net/mail"
|
|
"reflect"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
)
|
|
|
|
// StringToMailAddressFunc decodes a string into a mail.Address.
|
|
func StringToMailAddressFunc() mapstructure.DecodeHookFunc {
|
|
return func(f reflect.Type, t reflect.Type, data interface{}) (value interface{}, err error) {
|
|
if f.Kind() != reflect.String || t != reflect.TypeOf(mail.Address{}) {
|
|
return data, nil
|
|
}
|
|
|
|
dataStr := data.(string)
|
|
|
|
if dataStr == "" {
|
|
return mail.Address{}, nil
|
|
}
|
|
|
|
var (
|
|
mailAddress *mail.Address
|
|
)
|
|
|
|
mailAddress, err = mail.ParseAddress(dataStr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not parse '%s' as a RFC5322 address: %w", dataStr, err)
|
|
}
|
|
|
|
return *mailAddress, nil
|
|
}
|
|
}
|