Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
111 lines
2.9 KiB
Go
111 lines
2.9 KiB
Go
package woocommerce
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/config"
|
|
)
|
|
|
|
const encPrefix = "enc:v1:"
|
|
|
|
// DeriveKey builds a 32-byte AES key. Prefer explicitKey (hex/base64 of 32 bytes,
|
|
// or any passphrase hashed). When empty, derives from fallbackMaterial so local
|
|
// setups work without a dedicated env var (still at-rest encryption).
|
|
// In production (APP_ENV=production|prod), explicitKey is required; otherwise
|
|
// returns nil so encrypt/decrypt fail closed instead of hashing DATABASE_URL.
|
|
func DeriveKey(explicitKey, fallbackMaterial string) []byte {
|
|
explicitKey = strings.TrimSpace(explicitKey)
|
|
if explicitKey != "" {
|
|
if b, err := decodeKeyMaterial(explicitKey); err == nil {
|
|
return b
|
|
}
|
|
sum := sha256.Sum256([]byte(explicitKey))
|
|
return sum[:]
|
|
}
|
|
if config.IsProductionEnv() {
|
|
return nil
|
|
}
|
|
sum := sha256.Sum256([]byte("descrybe-woo-v1|" + fallbackMaterial))
|
|
return sum[:]
|
|
}
|
|
|
|
func decodeKeyMaterial(s string) ([]byte, error) {
|
|
if b, err := base64.StdEncoding.DecodeString(s); err == nil && len(b) == 32 {
|
|
return b, nil
|
|
}
|
|
if b, err := base64.RawStdEncoding.DecodeString(s); err == nil && len(b) == 32 {
|
|
return b, nil
|
|
}
|
|
if b, err := hex.DecodeString(s); err == nil && len(b) == 32 {
|
|
return b, nil
|
|
}
|
|
return nil, errors.New("invalid key material")
|
|
}
|
|
|
|
func EncryptSecret(key []byte, plaintext string) (string, error) {
|
|
if plaintext == "" {
|
|
return "", nil
|
|
}
|
|
if len(key) != 32 {
|
|
return "", errors.New("encryption key must be 32 bytes")
|
|
}
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
|
return "", err
|
|
}
|
|
sealed := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
|
|
return encPrefix + base64.RawStdEncoding.EncodeToString(sealed), nil
|
|
}
|
|
|
|
func DecryptSecret(key []byte, stored string) (string, error) {
|
|
if stored == "" {
|
|
return "", nil
|
|
}
|
|
if !strings.HasPrefix(stored, encPrefix) {
|
|
if config.IsProductionEnv() {
|
|
return "", errors.New("plaintext secrets are not allowed when APP_ENV=production")
|
|
}
|
|
return stored, nil
|
|
}
|
|
if len(key) != 32 {
|
|
return "", errors.New("encryption key must be 32 bytes")
|
|
}
|
|
raw, err := base64.RawStdEncoding.DecodeString(strings.TrimPrefix(stored, encPrefix))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(raw) < gcm.NonceSize() {
|
|
return "", errors.New("ciphertext too short")
|
|
}
|
|
nonce, ciphertext := raw[:gcm.NonceSize()], raw[gcm.NonceSize():]
|
|
plain, err := gcm.Open(nil, nonce, ciphertext, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(plain), nil
|
|
}
|