prosody-password/internal/api/handler/prosody_hdl.go

45 lines
1.3 KiB
Go
Raw Normal View History

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-08-04 23:15:22 +02:00
req := changePasswordReq{}
if err := c.BodyParser(&req); err != nil {
return RenderError(c, fmt.Errorf(" c.BodyParser(&req): %w", err), defaultErrMessage)
2023-07-05 22:07:10 +02:00
}
2023-08-09 20:04:01 +02:00
req.CurrentPassword = c.FormValue("current_password")
req.NewPassword = c.FormValue("new_password")
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{}, "")
}