chore: update session and challenge repo -- using Redis for caching session and challenge verifying -- added api handler docs
This commit is contained in:
@@ -13,7 +13,6 @@ type ChallengeResponse struct {
|
||||
type AuthenticateRequest struct {
|
||||
PubKey string `json:"pubKey" validate:"required,eth_pubkey"`
|
||||
Signature string `json:"signature" validate:"required,eth_signature"`
|
||||
Message string `json:"message" validate:"required,uuid"`
|
||||
}
|
||||
|
||||
type AuthenticateResponse struct {
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -2,26 +2,40 @@ package http
|
||||
|
||||
import (
|
||||
"backend/config"
|
||||
"backend/docs"
|
||||
httpHandlers "backend/internal/api/http/handlers"
|
||||
"backend/internal/app"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/gofiber/adaptor/v2"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
httpSwagger "github.com/swaggo/http-swagger"
|
||||
)
|
||||
|
||||
func Run(cfg config.Server, app *app.AppContainer) {
|
||||
fiberApp := fiber.New()
|
||||
api := fiberApp.Group("/api")
|
||||
// register routes here
|
||||
router := fiber.New()
|
||||
|
||||
api := router.Group("/api")
|
||||
|
||||
registerPublicRoutes(api, app)
|
||||
|
||||
log.Fatal(fiberApp.Listen(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)))
|
||||
docs.SwaggerInfo.Host = ""
|
||||
docs.SwaggerInfo.Schemes = []string{}
|
||||
docs.SwaggerInfo.BasePath = "/api"
|
||||
|
||||
router.Get("/swagger/*", adaptor.HTTPHandler(httpSwagger.Handler()))
|
||||
|
||||
log.Fatal(router.Listen(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)))
|
||||
}
|
||||
|
||||
func registerPublicRoutes(router fiber.Router, app *app.AppContainer) {
|
||||
authgroup := router.Group("/auth")
|
||||
|
||||
//TODO: implement handlers
|
||||
authgroup.Post("/challenge")
|
||||
authgroup.Post("/authenticate")
|
||||
authService := app.AuthService()
|
||||
authHandler := httpHandlers.NewAuthHandler(&authService)
|
||||
|
||||
// Register auth routes
|
||||
authgroup.Post("/challenge", authHandler.GenerateChallenge)
|
||||
authgroup.Post("/authenticate", authHandler.Authenticate)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user