Correct secret parsing

This commit is contained in:
9seconds
2021-03-10 12:26:07 +03:00
parent 015f02c077
commit 783c49db37
5 changed files with 107 additions and 82 deletions
+16 -5
View File
@@ -8,7 +8,7 @@ import (
"strings"
)
const SecretKeyLength = 32
const SecretKeyLength = 16
type Secret struct {
Key []byte
@@ -30,11 +30,19 @@ func (s *Secret) UnmarshalText(data []byte) error {
return ErrSecretEmpty
}
decoded, err := base64.RawStdEncoding.DecodeString(text)
if err != nil && strings.HasPrefix(text, "ee") {
var (
decoded []byte
err error
)
if strings.HasPrefix(text, "ee") {
decoded, err = hex.DecodeString(strings.TrimPrefix(text, "ee"))
}
if err != nil || len(decoded) <= SecretKeyLength {
decoded, err = base64.RawURLEncoding.DecodeString(text)
}
if err != nil {
return fmt.Errorf("incorrect secret format: %w", err)
}
@@ -50,7 +58,10 @@ func (s *Secret) UnmarshalText(data []byte) error {
}
func (s Secret) Base64() string {
return base64.StdEncoding.EncodeToString(append(s.Key[:], s.Host...))
data := append([]byte{238}, s.Key...) // 238 = hex ee
data = append(data, s.Host...)
return base64.RawURLEncoding.EncodeToString(data)
}
func (s Secret) String() string {
@@ -58,7 +69,7 @@ func (s Secret) String() string {
}
func (s Secret) EE() string {
return "ee" + hex.EncodeToString(append(s.Key[:], s.Host...))
return "ee" + hex.EncodeToString(append(s.Key, s.Host...))
}
func GenerateSecret(hostname string) Secret {