Passport/pkg/internal/server/api/factors_api.go

84 lines
2.2 KiB
Go
Raw Normal View History

package api
2024-01-26 17:11:32 +00:00
import (
"fmt"
2024-06-30 09:01:39 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
2024-01-26 17:11:32 +00:00
"github.com/gofiber/fiber/v2"
)
func getAvailableFactors(c *fiber.Ctx) error {
ticketId := c.QueryInt("ticketId", 0)
if ticketId <= 0 {
return fiber.NewError(fiber.StatusBadRequest, "must provide ticket id as a query parameter")
}
ticket, err := services.GetTicket(uint(ticketId))
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("ticket was not found: %v", err))
}
factors, err := services.ListUserFactor(ticket.AccountID)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(factors)
}
2024-01-26 17:11:32 +00:00
func requestFactorToken(c *fiber.Ctx) error {
id, _ := c.ParamsInt("factorId", 0)
2024-04-20 11:04:33 +00:00
factor, err := services.GetFactor(uint(id))
2024-01-26 17:11:32 +00:00
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
2024-01-29 08:11:59 +00:00
if sent, err := services.GetFactorCode(factor); err != nil {
2024-01-26 17:11:32 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
2024-01-27 16:05:19 +00:00
} else if !sent {
return c.SendStatus(fiber.StatusNoContent)
} else {
return c.SendStatus(fiber.StatusOK)
2024-01-26 17:11:32 +00:00
}
}
2024-06-30 09:01:39 +00:00
func requestResetPassword(c *fiber.Ctx) error {
var data struct {
UserID uint `json:"user_id" validate:"required"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
user, err := services.GetAccount(data.UserID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
if err = services.CheckAbleToResetPassword(user); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else if err = services.RequestResetPassword(user); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}
func confirmResetPassword(c *fiber.Ctx) error {
var data struct {
Code string `json:"code" validate:"required"`
NewPassword string `json:"new_password" validate:"required"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
if err := services.ConfirmResetPassword(data.Code, data.NewPassword); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}