Igris/internal/api/http/handlers/auth_handler.go

194 lines
5.5 KiB
Go

package http
import (
"backend/internal/api/dto"
"backend/internal/api/http/middlewares"
"backend/internal/usecase"
"github.com/gofiber/fiber/v2"
)
type AuthHandler struct {
authService usecase.AuthService
}
func NewAuthHandler(authService *usecase.AuthService) *AuthHandler {
return &AuthHandler{
authService: *authService,
}
}
// GenerateChallenge generates a challenge for authentication
// @Summary Generate authentication challenge
// @Description Generate a challenge message for wallet authentication
// @Tags auth
// @Accept json
// @Produce json
// @Param request body dto.ChallengeRequest true "Challenge Request"
// @Success 200 {object} dto.ChallengeResponse
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /auth/challenge [post]
func (h *AuthHandler) GenerateChallenge(c *fiber.Ctx) error {
var req dto.ChallengeRequest
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "invalid request body",
})
}
challenge, err := h.authService.GenerateChallenge(c.Context(), req.PubKey)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(
dto.ChallengeResponse{
Message: challenge.Message,
TimeStamp: challenge.TimeStamp.String(),
ExpiresAt: challenge.ExpiresAt.String(),
},
)
}
// Authenticate authenticates a user with signed challenge
// @Summary Authenticate user
// @Description Authenticate user with wallet signature
// @Tags auth
// @Accept json
// @Produce json
// @Param request body dto.AuthenticateRequest true "Authentication Request"
// @Success 200 {object} dto.AuthenticateResponse
// @Failure 400 {object} map[string]string
// @Failure 401 {object} map[string]string
// @Router /auth/authenticate [post]
func (h *AuthHandler) Authenticate(c *fiber.Ctx) error {
var req dto.AuthenticateRequest
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "invalid request body",
})
}
userToken, err := h.authService.Authenticate(
c.Context(),
req.PubKey,
req.Signature,
//TODO: add chainID to cfg
1,
c.IP(),
c.Get("User-Agent"),
)
if err != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(dto.AuthenticateResponse{
AuthorizationToken: userToken.AuthorizationToken,
RefreshToken: userToken.RefreshToken,
ExpiresAt: userToken.ExpiresAt,
})
}
// SendOTP sends OTP code to the provided phone number
// @Summary Send OTP code
// @Description Send OTP code to the provided phone number
// @Tags auth
// @Accept json
// @Produce json
// @Param request body dto.OTPProviderReq true "OTP Request"
// @Success 200 {object} dto.OTPProviderResponse
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /auth/otp [post]
func (h *AuthHandler) SendOTP(c *fiber.Ctx) error {
var req dto.OTPProviderReq
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "invalid request body",
})
}
_, err := h.authService.SendOTPCode(c.Context(), req.Receptor)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(dto.OTPProviderResponse{
Message: "OTP code sent successfully",
})
}
// VerifyOTP verifies the OTP code
// @Summary Verify OTP code
// @Description Verify the provided OTP code for the phone number
// @Tags auth
// @Accept json
// @Produce json
// @Param request body dto.OTPVerifyRequest true "OTP Verify Request"
// @Success 200 {object} dto.OTPVerifyResponse
// @Failure 400 {object} map[string]string
// @Failure 401 {object} map[string]string
// @Router /auth/otp [post]
func (h *AuthHandler) VerifyOTP(c *fiber.Ctx) error {
var req dto.OTPVerifyRequest
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "invalid request body",
})
}
err := h.authService.VerifyOTP(c.Context(), req.Phone, req.Code)
if err != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(dto.OTPVerifyResponse{
Message: "OTP verified successfully",
})
}
// VerifyKYC verifies user KYC information
// @Summary Verify user KYC
// @Description Verify user KYC with national ID and birth date
// @Tags auth
// @Accept json
// @Produce json
// @Param request body dto.KYCVerifyRequest true "KYC Verify Request"
// @Success 200 {object} dto.APIResponse
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /auth/kyc [post]
func (h *AuthHandler) VerifyKYC(c *fiber.Ctx) error {
var req dto.KYCVerifyRequest
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "invalid request body",
})
}
claims := middlewares.GetUserClaims(c)
err := h.authService.VerifyKYC(c.Context(), claims.UserID, req.NationalID, req.BirthDate)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(dto.APIResponse{
Success: true,
Message: "KYC verified successfully",
})
}
func (h *AuthHandler) HelloWorld(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"message": "Hello, World!",
})
}