39 lines
934 B
Go
39 lines
934 B
Go
package plugin
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
const protectedSecretPrefix = "dpapi:v1:"
|
|
|
|
var ErrSecretNotProtected = errors.New("stored secret is not protected")
|
|
|
|
func protectSettingSecret(value string) (string, error) {
|
|
if value == "" || strings.HasPrefix(value, protectedSecretPrefix) {
|
|
return value, nil
|
|
}
|
|
protected, err := protectSecretBytes([]byte(value))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return protectedSecretPrefix + base64.StdEncoding.EncodeToString(protected), nil
|
|
}
|
|
|
|
func unprotectSettingSecret(value string) (string, error) {
|
|
if !strings.HasPrefix(value, protectedSecretPrefix) {
|
|
return "", ErrSecretNotProtected
|
|
}
|
|
encoded := strings.TrimPrefix(value, protectedSecretPrefix)
|
|
protected, err := base64.StdEncoding.DecodeString(encoded)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
plain, err := unprotectSecretBytes(protected)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(plain), nil
|
|
}
|