Compare commits
4 Commits
8ccf9f281c
...
9473c83679
Author | SHA1 | Date | |
---|---|---|---|
9473c83679 | |||
0b8c03e8c5 | |||
55eb4c9862 | |||
de28470432 |
@ -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{
|
||||||
|
@ -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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -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
15
internal/util/hash.go
Normal 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
|
||||||
|
}
|
Reference in New Issue
Block a user