chore: update session and challenge repo -- using Redis for caching session and challenge verifying -- added api handler docs

This commit is contained in:
2025-09-09 13:31:41 +03:30
parent dee9ce2f64
commit 6bf6a8ffc2
20 changed files with 1077 additions and 193 deletions
+24 -20
View File
@@ -2,12 +2,9 @@ package http
import (
"backend/internal/api/dto"
"backend/internal/domain"
"backend/internal/usecase"
"time"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)
type AuthHandler struct {
@@ -20,6 +17,17 @@ func NewAuthHandler(authService *usecase.AuthService) *AuthHandler {
}
}
// 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 {
@@ -30,18 +38,29 @@ func (h *AuthHandler) GenerateChallenge(c *fiber.Ctx) error {
challenge, err := h.authService.GenerateChallenge(c.Context(), req.PubKey)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "failed to generate challenge",
"error": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(
dto.ChallengeResponse{
Message: challenge.Message.String(),
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 {
@@ -50,25 +69,10 @@ func (h *AuthHandler) Authenticate(c *fiber.Ctx) error {
})
}
messageUUID, err := uuid.Parse(req.Message)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "invalid message format",
})
}
challenge := &domain.Challenge{
Message: messageUUID,
//TODO: fetch from cache
TimeStamp: time.Now().UTC(),
ExpiresAt: time.Now().Add(5 * time.Minute),
}
userToken, err := h.authService.Authenticate(
c.Context(),
req.PubKey,
req.Signature,
challenge,
// add chainID to cfg
1,
c.IP(),