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:46:25 +02:00
"log"
2023-07-05 22:07:10 +02:00
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 {
req := changePasswordReq{}
if err := c.BodyParser(&req); err != nil {
return RenderError(c, fmt.Errorf("id is empty"), defaultErrMessage)
}
2023-07-07 23:46:25 +02:00
log.Println("req.User, req.CurrentPassword, req.NewPassword", req.User, req.CurrentPassword, req.NewPassword)
2023-07-05 22:07:10 +02:00
if err := handler.prosodyService.ChangePassword(req.User, req.CurrentPassword, req.NewPassword); err != nil {
2023-07-07 23:31: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{}, "")
}