aboutsummaryrefslogtreecommitdiff
path: root/src/user
diff options
context:
space:
mode:
authorAlexander Kavon <me+git@alexkavon.com>2024-01-22 00:37:21 -0500
committerAlexander Kavon <me+git@alexkavon.com>2024-01-22 00:37:21 -0500
commitb3c1584ec4a5bcba84a10cd9b6501d0e978c2457 (patch)
tree87fa8e9f21fd07b1a679a2ed05a3c931a85212cb /src/user
parentd6fdb3a460eb228d7b1cd7870b7ef6c8c7391f0b (diff)
update db adapter, server, routes, and user model to be sqlboiler compatible
Diffstat (limited to 'src/user')
-rw-r--r--src/user/routes.go26
-rw-r--r--src/user/secret.go (renamed from src/user/user.go)45
2 files changed, 25 insertions, 46 deletions
diff --git a/src/user/routes.go b/src/user/routes.go
index d8c8d43..b163fb7 100644
--- a/src/user/routes.go
+++ b/src/user/routes.go
@@ -5,6 +5,8 @@ import (
"net/http"
"github.com/go-playground/validator/v10"
+ "github.com/volatiletech/sqlboiler/v4/boil"
+ "gitlab.com/alexkavon/newsstand/src/models"
"gitlab.com/alexkavon/newsstand/src/server"
"gitlab.com/alexkavon/newsstand/src/sessions"
)
@@ -64,33 +66,33 @@ func Store(s *server.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
- user := &User{
- Db: s.Db,
- Username: r.PostFormValue("username"),
- Secret: r.PostFormValue("secret"),
- Email: r.PostFormValue("email"),
- }
+ var user models.User
+ user.Username = r.PostFormValue("username")
+ user.Secret = r.PostFormValue("secret")
+ user.Email = r.PostFormValue("email")
// Validate User Input
v := validator.New()
err := v.Struct(user)
if err != nil {
- log.Println("Validator failed", err.(validator.ValidationErrors))
+ log.Fatal("Validator failed", err.(validator.ValidationErrors))
}
// Hash secret
- err = user.HashSecret()
+ secret := &Secret{Raw: user.Secret}
+ err = secret.HashSecret()
if err != nil {
- log.Println("Hash failure", err)
+ log.Fatal("Hash failure", err)
}
+ user.Secret = secret.Hash()
// Store user
- err = user.Insert()
+ err = user.Insert(r.Context(), s.Db.ToSqlDb(), boil.Infer())
if err != nil {
- log.Println("Insert Error", err)
+ log.Fatal("Insert Error", err)
}
// Send email validation
// Create cookie session
- sessions.NewSession(w, sessions.SessionValues{"uid": user.Id, "username": user.Username})
+ sessions.NewSession(w, sessions.SessionValues{"uid": user.ID, "username": user.Username})
// Redirect to user profile
http.Redirect(w, r, "/u/me", http.StatusSeeOther)
}
diff --git a/src/user/user.go b/src/user/secret.go
index d9fca6b..55b8bc6 100644
--- a/src/user/user.go
+++ b/src/user/secret.go
@@ -4,43 +4,16 @@ import (
"crypto/rand"
"encoding/base64"
"fmt"
- "time"
- "gitlab.com/alexkavon/newsstand/src/db"
"golang.org/x/crypto/argon2"
)
-type User struct {
- Id int64
- Username string `validate:"required,max=50"`
- Secret string `validate:"required,min=8,max=128"`
- Email string `validate:"required,email"`
- Karma uint64
- UpdatedAt time.Time
- CreatedAt time.Time
- hash string
- Db *db.Database
+type Secret struct {
+ Raw string
+ hash string
}
-func NewUser(d *db.Database) *User {
- return &User{
- Db: d,
- }
-}
-
-func (u *User) Insert() error {
- err := u.Db.InsertTable(
- "users",
- []string{"username", "secret", "email"},
- db.DbValues{"username": u.Username, "secret": string(u.hash), "email": u.Email},
- )
- if err != nil {
- return err
- }
- return nil
-}
-
-func (u *User) HashSecret() error {
+func (s Secret) HashSecret() error {
hashconf := &struct {
memory uint32
iterations uint32
@@ -55,7 +28,7 @@ func (u *User) HashSecret() error {
}
hash := argon2.IDKey(
- []byte(u.Secret),
+ []byte(s.Raw),
salt,
hashconf.iterations,
hashconf.memory,
@@ -73,6 +46,10 @@ func (u *User) HashSecret() error {
b64Salt,
b64Hash,
)
- u.hash = encodedHash
- return nil
+ s.hash = encodedHash
+ return err
+}
+
+func (s *Secret) Hash() string {
+ return s.hash
}