46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"gitea.urkob.com/urko/prosody-password/internal/services/fail2ban"
|
|
"gitea.urkob.com/urko/prosody-password/internal/services/prosody"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type ProsodyHandler struct {
|
|
prosodyService *prosody.Prosody
|
|
fail2banSrv *fail2ban.Fail2Ban
|
|
}
|
|
|
|
func NewProsodyHandler(prosodyService *prosody.Prosody, fail2banSrv *fail2ban.Fail2Ban) ProsodyHandler {
|
|
return ProsodyHandler{
|
|
prosodyService: prosodyService,
|
|
fail2banSrv: fail2banSrv,
|
|
}
|
|
}
|
|
|
|
type changePasswordReq struct {
|
|
CurrentPassword string `json:"current_password"`
|
|
NewPassword string `json:"new_password"`
|
|
User string `json:"user"`
|
|
}
|
|
|
|
func (handler ProsodyHandler) Post(c *fiber.Ctx) error {
|
|
req := changePasswordReq{
|
|
CurrentPassword: c.FormValue("current_password", ""),
|
|
NewPassword: c.FormValue("new_password", ""),
|
|
User: c.FormValue("user", ""),
|
|
}
|
|
log.Println("req.User, req.CurrentPassword, req.NewPassword", req.User, req.CurrentPassword, req.NewPassword)
|
|
if err := handler.prosodyService.ChangePassword(req.User, req.CurrentPassword, req.NewPassword); err != nil {
|
|
for _, ip := range c.IPs() {
|
|
handler.fail2banSrv.FailedAttempt(ip)
|
|
}
|
|
return RenderError(c, fmt.Errorf("ChangePassword: %w", err), defaultErrMessage)
|
|
}
|
|
|
|
return c.Render("success", fiber.Map{}, "")
|
|
}
|