Code re-organization
Some checks failed
Go Tests / build (1.22.x) (push) Failing after 1m27s

This commit is contained in:
2025-01-26 18:20:45 -05:00
parent b93d1c29b8
commit 85938a2def
21 changed files with 685 additions and 348 deletions

View File

@@ -16,7 +16,7 @@ func pkcs7pad(data []byte, blockSize int) []byte {
}
func (g *Generator) Decrypt(s string) (string, error) {
c, err := aes.NewCipher(g.config.encryptionKeyBin)
c, err := aes.NewCipher(g.encryptionKey)
if err != nil {
return "", err
}
@@ -36,7 +36,7 @@ func (g *Generator) Decrypt(s string) (string, error) {
}
func (g *Generator) generateBaseAesEncUrl(file []byte) (string, error) {
c, err := aes.NewCipher(g.config.encryptionKeyBin)
c, err := aes.NewCipher(g.encryptionKey)
if err != nil {
return "", err
}

View File

@@ -9,14 +9,14 @@ import (
func (g *Generator) generateSignature(path string) string {
var signature string
if len(g.config.keyBin) == 0 || len(g.config.saltBin) == 0 {
if len(g.keyBin) == 0 || len(g.salt) == 0 {
signature = "insecure"
printer.NewPrinter().LogWarning("Insecure url generated. Provide salt and key to sign and secure url.")
} else {
mac := hmac.New(sha256.New, g.config.keyBin)
mac.Write(g.config.saltBin)
mac := hmac.New(sha256.New, g.keyBin)
mac.Write(g.salt)
mac.Write([]byte(path))
signature = base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
}

View File

@@ -3,42 +3,33 @@ package generator
import (
"encoding/hex"
"fmt"
"github.com/siteworxpro/img-proxy-url-generator/config"
"strings"
)
type Generator struct {
config Config
}
type Config struct {
Salt []byte
saltBin []byte
Key []byte
keyBin []byte
Host string
EncryptionKey *string
encryptionKeyBin []byte
PlainUrl bool
keyBin []byte
salt []byte
encryptionKey []byte
}
var PathPrefix string
func NewGenerator(config Config) (*Generator, error) {
func NewGenerator(config *config.Config) (*Generator, error) {
var err error
gen := new(Generator)
gen.config = config
if gen.config.keyBin, err = hex.DecodeString(string(gen.config.Key)); err != nil {
if gen.keyBin, err = hex.DecodeString(string(config.Generator.Key)); err != nil {
return nil, err
}
if gen.config.saltBin, err = hex.DecodeString(string(gen.config.Salt)); err != nil {
if gen.salt, err = hex.DecodeString(string(config.Generator.Salt)); err != nil {
return nil, err
}
if gen.config.EncryptionKey != nil && *gen.config.EncryptionKey != "" {
if gen.config.encryptionKeyBin, err = hex.DecodeString(*gen.config.EncryptionKey); err != nil {
if config.Generator.EncryptionKey != "" {
if gen.encryptionKey, err = hex.DecodeString(config.Generator.EncryptionKey); err != nil {
return nil, fmt.Errorf("key expected to be hex-encoded string")
}
}
@@ -62,9 +53,9 @@ func (g *Generator) GenerateUrl(file string, params []string, format Format) (st
var url string
var err error
if g.config.PlainUrl {
if config.GetConfig().Generator.PlainUrl {
url, _ = g.generatePlainUrl(file)
} else if g.config.encryptionKeyBin != nil {
} else if g.encryptionKey != nil {
url, err = g.generateBaseAesEncUrl([]byte(file))
} else {
url, _ = g.generateBase64Url([]byte(file))
@@ -82,5 +73,5 @@ func (g *Generator) GenerateUrl(file string, params []string, format Format) (st
signature := g.generateSignature(path)
return fmt.Sprintf("%s/%s%s", g.config.Host, signature, path), nil
return fmt.Sprintf("%s/%s%s", config.GetConfig().Generator.Host, signature, path), nil
}