Compare commits

...

4 Commits

Author SHA1 Message Date
9473c83679 feat: hash utility 2025-05-21 21:17:55 +02:00
0b8c03e8c5 feat: hash user's password on register 2025-05-21 21:17:50 +02:00
55eb4c9862 feat: hash admin's password before creation 2025-05-21 21:17:41 +02:00
de28470432 feat: check passwords on login 2025-05-21 21:17:30 +02:00
4 changed files with 37 additions and 2 deletions

View File

@ -81,6 +81,11 @@ func (h *AuthHandler) login(w http.ResponseWriter, r *http.Request) {
return return
} }
if !util.VerifyPassword(params.Password, user.PasswordHash) {
web.Error(w, "username or/and password are incorrect", http.StatusBadRequest)
return
}
claims := types.UserClaims{ claims := types.UserClaims{
UserID: user.ID.String(), UserID: user.ID.String(),
RegisteredClaims: jwt.RegisteredClaims{ RegisteredClaims: jwt.RegisteredClaims{

View File

@ -2,10 +2,12 @@ package user
import ( import (
"context" "context"
"fmt"
"log" "log"
"os" "os"
"gitea.local/admin/hspguard/internal/repository" "gitea.local/admin/hspguard/internal/repository"
"gitea.local/admin/hspguard/internal/util"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -35,10 +37,16 @@ func EnsureAdminUser(ctx context.Context, repo *repository.Queries) {
} }
func createAdmin(ctx context.Context, name, email, password string, repo *repository.Queries) (uuid.UUID, error) { func createAdmin(ctx context.Context, name, email, password string, repo *repository.Queries) (uuid.UUID, error) {
hash, err := util.HashPassword(password)
if err != nil {
var id uuid.UUID
return id, fmt.Errorf("failed to hash the admin password")
}
return repo.InsertUser(ctx, repository.InsertUserParams{ return repo.InsertUser(ctx, repository.InsertUserParams{
FullName: name, FullName: name,
Email: email, Email: email,
PasswordHash: password, PasswordHash: hash,
IsAdmin: true, IsAdmin: true,
}) })
} }

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"gitea.local/admin/hspguard/internal/repository" "gitea.local/admin/hspguard/internal/repository"
"gitea.local/admin/hspguard/internal/util"
"gitea.local/admin/hspguard/internal/web" "gitea.local/admin/hspguard/internal/web"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
@ -51,10 +52,16 @@ func (h *UserHandler) register(w http.ResponseWriter, r *http.Request) {
return return
} }
hash, err := util.HashPassword(params.Password)
if err != nil {
web.Error(w, "failed to create user account", http.StatusInternalServerError)
return
}
id, err := h.repo.InsertUser(context.Background(), repository.InsertUserParams{ id, err := h.repo.InsertUser(context.Background(), repository.InsertUserParams{
FullName: params.FullName, FullName: params.FullName,
Email: params.Email, Email: params.Email,
PasswordHash: params.Password, PasswordHash: hash,
IsAdmin: false, IsAdmin: false,
}) })
if err != nil { if err != nil {

15
internal/util/hash.go Normal file
View File

@ -0,0 +1,15 @@
package util
import "golang.org/x/crypto/bcrypt"
// HashPassword generates a bcrypt hash for the given password.
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}
// VerifyPassword verifies if the given password matches the stored hash.
func VerifyPassword(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}