Skip to content

Commit

Permalink
fix(config): extend config to make all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
vednoc committed Oct 15, 2024
1 parent b1d4537 commit 4863bb7
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 37 deletions.
5 changes: 3 additions & 2 deletions cmd/userstyles-world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func main() {
log.Initialize()
cache.Init()
images.CheckVips()
util.InitCrypto()
util.InitCrypto(config.Secrets)
util.InitPool(config.Secrets)
jwtware.Init()
email.Init()
validator.Init()
Expand All @@ -65,7 +66,7 @@ func main() {
web.Init()

app := fiber.New(fiber.Config{
Views: templates.New(http.FS(web.ViewsDir)),
Views: templates.New(http.FS(web.ViewsDir), config.App),
ViewsLayout: "layouts/main",
ProxyHeader: config.App.ProxyHeader,
JSONEncoder: util.JSONEncoder,
Expand Down
6 changes: 5 additions & 1 deletion handlers/core/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

"github.com/gofiber/fiber/v2"

"userstyles.world/modules/config"
"userstyles.world/modules/templates"
)

Expand Down Expand Up @@ -53,8 +55,10 @@ func Test404Normal(t *testing.T) {
func Test404Pages(t *testing.T) {
t.Parallel()

c := &config.AppConfig{Production: true}

app := fiber.New(fiber.Config{
Views: templates.New(http.Dir("../../web/views")),
Views: templates.New(http.Dir("../../web/views"), c),
})
app.Get("/", func(c *fiber.Ctx) error {
c.Response().Header.SetContentType(fiber.MIMETextHTMLCharsetUTF8)
Expand Down
2 changes: 1 addition & 1 deletion handlers/user/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func EditAccount(c *fiber.Ctx) error {
})
}

pw, err := util.HashPassword(newPassword)
pw, err := util.HashPassword(newPassword, config.Secrets)
if err != nil {
return c.Status(fiber.StatusInternalServerError).Render("err", fiber.Map{
"Title": "Failed to hash password",
Expand Down
2 changes: 1 addition & 1 deletion handlers/user/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func ResetPost(c *fiber.Ctx) error {
})
}

pw, err := util.HashPassword(newPassword)
pw, err := util.HashPassword(newPassword, config.Secrets)
if err != nil {
return c.Status(fiber.StatusInternalServerError).Render("err", fiber.Map{
"Title": "Failed to hash password",
Expand Down
2 changes: 1 addition & 1 deletion handlers/user/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func VerifyGet(c *fiber.Ctx) error {
Email: claims["email"].(string),
}

pw, err := util.HashPassword(u.Password)
pw, err := util.HashPassword(u.Password, config.Secrets)
if err != nil {
return c.Status(fiber.StatusInternalServerError).Render("err", fiber.Map{
"Title": "Failed to hash password",
Expand Down
8 changes: 5 additions & 3 deletions modules/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var (
Storage *StorageConfig
)

// UpdateGitInfo updates copyright year at the start of every year.
func (app *AppConfig) UpdateCopyright() {
if app.Name == "" {
log.Fatal("config: App.Name can't be an empty string")
Expand All @@ -143,8 +144,9 @@ func (app *AppConfig) UpdateGitInfo() {
app.GitSignature = GitSignature
}

// defaultConfig is a set of default configs used in development environment.
func defaultConfig() *config {
// DefaultConfig is a set of default configurations used for development and
// testing environments.
func DefaultConfig() *config {
return &config{
App: AppConfig{
Addr: ":3000",
Expand Down Expand Up @@ -199,7 +201,7 @@ func Load(path string) error {
return err
}

c := defaultConfig()
c := DefaultConfig()
if err = json.Unmarshal(b, &c); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion modules/database/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func seed() {
defer log.Info.Println("Finished seeding mock data.")

pw := func(s string) string {
s, err := util.HashPassword(s)
s, err := util.HashPassword(s, config.Secrets)
if err != nil {
log.Warn.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions modules/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func status() sys {
}
}

func New(views http.FileSystem) *html.Engine {
func New(views http.FileSystem, app *config.AppConfig) *html.Engine {
engine := html.NewFileSystem(views, ".tmpl")

engine.AddFunc("sys", status)
Expand Down Expand Up @@ -145,7 +145,7 @@ func New(views http.FileSystem) *html.Engine {
return string(b)
})

if !config.App.Production {
if !app.Production {
engine.Reload(true)
}

Expand Down
4 changes: 2 additions & 2 deletions modules/util/bcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

// HashPassword generates a hash out of a password.
func HashPassword(pw string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(pw), config.Secrets.PasswordCost)
func HashPassword(pw string, secrets *config.SecretsConfig) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(pw), secrets.PasswordCost)
if err != nil {
return "", err
}
Expand Down
10 changes: 5 additions & 5 deletions modules/util/bcrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const pw = "UserStyles.world"
func TestHashPassword(t *testing.T) {
t.Parallel()

got, err := HashPassword(pw)
got, err := HashPassword(pw, &c.Secrets)
if err != nil {
t.Fatalf("bcrypt failed: %s", err)
t.Fatalf("bcrypt failed to hash a password: %s", err)
}

exp := "$2a$10"
Expand All @@ -23,14 +23,14 @@ func TestHashPassword(t *testing.T) {

func BenchmarkHashPassword(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = HashPassword(pw)
_, _ = HashPassword(pw, &c.Secrets)
}
}

func TestVerifyPassword(t *testing.T) {
t.Parallel()

got, err := HashPassword(pw)
got, err := HashPassword(pw, &c.Secrets)
if err != nil {
t.Fatalf("bcrypt failed: %s", err)
}
Expand All @@ -42,7 +42,7 @@ func TestVerifyPassword(t *testing.T) {
}

func BenchmarkVerifyPassword(b *testing.B) {
hash, err := HashPassword(pw)
hash, err := HashPassword(pw, &c.Secrets)
if err != nil {
b.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions modules/util/chacha20poly1305.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ var (
)

// InitCrypto initializes cryptographic ciphers.
func InitCrypto() {
VerifySigningKey = []byte(config.Secrets.RecoverTokenKey)
OAuthPSigningKey = []byte(config.Secrets.ProviderTokenKey)
func InitCrypto(secrets *config.SecretsConfig) {
VerifySigningKey = []byte(secrets.RecoverTokenKey)
OAuthPSigningKey = []byte(secrets.ProviderTokenKey)

var err error
AEADCrypto, err = chacha20poly1305.NewX([]byte(config.Secrets.CryptoKey))
AEADCrypto, err = chacha20poly1305.NewX([]byte(secrets.CryptoKey))
if err != nil {
log.Warn.Fatalf("Cannot create AEAD_CRYPTO cipher: %s\n", err)
}

AEADOAuth, err = chacha20poly1305.NewX([]byte(config.Secrets.OAuthClientKey))
AEADOAuth, err = chacha20poly1305.NewX([]byte(secrets.OAuthClientKey))
if err != nil {
log.Warn.Fatalf("Cannot create AEAD_OAUTH cipher: %s\n", err)
}

AEADOAuthp, err = chacha20poly1305.NewX([]byte(config.Secrets.OAuthProviderKey))
AEADOAuthp, err = chacha20poly1305.NewX([]byte(secrets.OAuthProviderKey))
if err != nil {
log.Warn.Fatalf("Cannot create AEAD_OAUTHP cipher: %s\n", err)
}
Expand Down
17 changes: 7 additions & 10 deletions modules/util/chacha20poly1305_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"userstyles.world/modules/config"
)

var c = config.DefaultConfig()

func TestSimpleKey(t *testing.T) {
t.Parallel()
InitCrypto()
InitCrypto(&c.Secrets)

jwtToken, err := NewJWT().
SetClaim("email", "vednoc@usw.local").
Expand All @@ -21,13 +23,8 @@ func TestSimpleKey(t *testing.T) {
t.Error(err)
}

scrambleConfig := &config.SecretsConfig{
ScrambleStepSize: 3,
ScrambleBytesPerInsert: 2,
}

sealedText := sealText(jwtToken, AEADCrypto, scrambleConfig)
unSealedText, err := openText(UnsafeString(sealedText), AEADCrypto, scrambleConfig)
sealedText := sealText(jwtToken, AEADCrypto, &c.Secrets)
unSealedText, err := openText(UnsafeString(sealedText), AEADCrypto, &c.Secrets)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -94,7 +91,7 @@ func benchamarkDecodePreparedText(b *testing.B, buf []byte, scrambleConfig *conf
}

func BenchmarkPureChaCha20Poly1305(b *testing.B) {
InitCrypto()
InitCrypto(&c.Secrets)
b.ResetTimer()

scrambleConfig := &config.SecretsConfig{
Expand All @@ -113,7 +110,7 @@ func BenchmarkPureChaCha20Poly1305(b *testing.B) {
}

func BenchmarkPrepareText(b *testing.B) {
InitCrypto()
InitCrypto(&c.Secrets)
b.ResetTimer()

scrambleConfig := &config.SecretsConfig{
Expand Down
8 changes: 6 additions & 2 deletions modules/util/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ var (
return &b
},
}
hmacPool sync.Pool
)

func InitPool(secrets *config.SecretsConfig) {
hmacPool = sync.Pool{
New: func() interface{} {
return hmac.New(sha512.New, []byte(config.Secrets.StatsKey))
return hmac.New(sha512.New, []byte(secrets.StatsKey))
},
}
)
}

// HashIP generates a unique hash for stats.
func HashIP(key string) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions modules/util/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var hashCases = []struct {

func TestHashIP(t *testing.T) {
t.Parallel()
InitPool(&c.Secrets)

for _, c := range hashCases {
actual, err := HashIP(c.ip + " " + c.id)
Expand Down

0 comments on commit 4863bb7

Please sign in to comment.