aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kavon <me+git@alexkavon.com>2024-01-22 15:01:22 -0500
committerAlexander Kavon <me+git@alexkavon.com>2024-01-22 15:01:22 -0500
commitf3ecdc8c064b6a107e8613aefb780dc0fa989685 (patch)
tree3c1acc64ecfb64d2199e99d4efe18312b118031f
parenta1df83e8b5737a198a3fba4de23ca2c80828f623 (diff)
update hooks to use HashSecret
-rw-r--r--src/user/hooks.go40
1 files changed, 4 insertions, 36 deletions
diff --git a/src/user/hooks.go b/src/user/hooks.go
index 0ab5ab0..1552760 100644
--- a/src/user/hooks.go
+++ b/src/user/hooks.go
@@ -2,21 +2,17 @@ package user
import (
"context"
- "crypto/rand"
- "encoding/base64"
- "fmt"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/volatiletech/sqlboiler/v4/boil"
"gitlab.com/alexkavon/newsstand/src/models"
- "golang.org/x/crypto/argon2"
)
func init() {
models.AddUserHook(boil.BeforeInsertHook, validate)
// should always be last
- models.AddUserHook(boil.BeforeInsertHook, hashSecret)
+ models.AddUserHook(boil.BeforeInsertHook, hashSecretBeforeInsert)
}
func validate(ctx context.Context, exec boil.ContextExecutor, u *models.User) error {
@@ -33,40 +29,12 @@ func validate(ctx context.Context, exec boil.ContextExecutor, u *models.User) er
return nil
}
-func hashSecret(ctx context.Context, exec boil.ContextExecutor, u *models.User) error {
- hashconf := &struct {
- memory uint32
- iterations uint32
- parallelism uint8
- keyLength uint32
- saltLength uint32
- }{64 * 1024, 3, 2, 12, 16}
- salt := make([]byte, hashconf.saltLength)
- _, err := rand.Read(salt)
+func hashSecretBeforeInsert(ctx context.Context, exec boil.ContextExecutor, u *models.User) error {
+ hashed, err := HashSecret(u.Secret)
if err != nil {
return err
}
+ u.Secret = hashed
- hash := argon2.IDKey(
- []byte(u.Secret),
- salt,
- hashconf.iterations,
- hashconf.memory,
- hashconf.parallelism,
- hashconf.keyLength,
- )
- b64Salt := base64.RawStdEncoding.EncodeToString(salt)
- b64Hash := base64.RawStdEncoding.EncodeToString(hash)
- encodedHash := fmt.Sprintf(
- "$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
- argon2.Version,
- hashconf.memory,
- hashconf.iterations,
- hashconf.parallelism,
- b64Salt,
- b64Hash,
- )
-
- u.Secret = encodedHash
return nil
}