2023-07-05 22:07:10 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-07-07 23:31:32 +02:00
|
|
|
"gitea.urkob.com/urko/prosody-password/internal/services/fail2ban"
|
2023-07-05 22:07:10 +02:00
|
|
|
"gitea.urkob.com/urko/prosody-password/internal/services/prosody"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2023-07-07 23:31:32 +02:00
|
|
|
type ProsodyHandler struct {
|
|
|
|
prosodyService *prosody.Prosody
|
|
|
|
fail2banSrv *fail2ban.Fail2Ban
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProsodyHandler(prosodyService *prosody.Prosody, fail2banSrv *fail2ban.Fail2Ban) ProsodyHandler {
|
2023-07-05 22:07:10 +02:00
|
|
|
return ProsodyHandler{
|
|
|
|
prosodyService: prosodyService,
|
2023-07-07 23:31:32 +02:00
|
|
|
fail2banSrv: fail2banSrv,
|
2023-07-05 22:07:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-07-07 23:50:28 +02:00
|
|
|
req := changePasswordReq{
|
|
|
|
CurrentPassword: c.FormValue("current_password", ""),
|
|
|
|
NewPassword: c.FormValue("new_password", ""),
|
|
|
|
User: c.FormValue("user", ""),
|
2023-07-05 22:07:10 +02:00
|
|
|
}
|
|
|
|
if err := handler.prosodyService.ChangePassword(req.User, req.CurrentPassword, req.NewPassword); err != nil {
|
2023-07-23 11:13:32 +02:00
|
|
|
// for _, ip := range c.IPs() {
|
|
|
|
// handler.fail2banSrv.FailedAttempt(ip)
|
|
|
|
// }
|
2023-07-05 22:07:10 +02:00
|
|
|
return RenderError(c, fmt.Errorf("ChangePassword: %w", err), defaultErrMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Render("success", fiber.Map{}, "")
|
|
|
|
}
|