altcha-server/internal/api/server.go

138 lines
3.4 KiB
Go
Raw Normal View History

2024-09-11 09:54:55 +02:00
package api
import (
"context"
"encoding/json"
"fmt"
2024-09-13 09:12:16 +02:00
"log/slog"
2024-09-11 09:54:55 +02:00
"net/http"
"time"
"forge.cadoles.com/cadoles/altcha-server/internal/client"
"forge.cadoles.com/cadoles/altcha-server/internal/config"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
"gitlab.com/wpetit/goweb/logger"
)
type Server struct {
2024-09-11 12:09:43 +02:00
baseUrl string
2024-09-11 09:54:55 +02:00
port string
client client.Client
config config.Config
2024-09-11 09:54:55 +02:00
}
func (s *Server) Run(ctx context.Context) {
if s.config.Debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
2024-09-11 09:54:55 +02:00
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(corsMiddleware)
r.Use(render.SetContentType(render.ContentTypeJSON))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("root."))
})
2024-09-11 12:09:43 +02:00
r.Get(s.baseUrl+"/request", s.requestHandler)
2024-09-13 09:12:16 +02:00
r.Post(s.baseUrl+"/verify", s.submitHandler)
2024-09-11 09:54:55 +02:00
logger.Info(ctx, "altcha server listening on port "+s.port)
if err := http.ListenAndServe(":"+s.port, r); err != nil {
logger.Error(ctx, err.Error())
}
}
func (s *Server) requestHandler(w http.ResponseWriter, r *http.Request) {
challenge, err := s.client.Generate()
if err != nil {
slog.Debug("Failed to create challenge,", "error", err)
2024-09-11 09:54:55 +02:00
http.Error(w, fmt.Sprintf("Failed to create challenge : %s", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(challenge)
if err != nil {
slog.Debug("Failed to encode JSON", "error", err)
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
return
}
2024-09-11 09:54:55 +02:00
}
func (s *Server) submitHandler(w http.ResponseWriter, r *http.Request) {
var payload map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
slog.Debug("Failed to parse Altcha payload,", "error", err)
2024-09-11 09:54:55 +02:00
http.Error(w, "Failed to parse Altcha payload", http.StatusBadRequest)
return
}
2024-09-11 09:54:55 +02:00
verified, err := s.client.VerifySolution(payload)
if err != nil {
slog.Debug("Invalid Altcha payload", "error", err)
http.Error(w, "Invalid Altcha payload,", http.StatusBadRequest)
2024-09-13 09:12:16 +02:00
return
2024-09-11 09:54:55 +02:00
}
if !verified {
slog.Debug("Invalid solution")
http.Error(w, "Invalid solution,", http.StatusBadRequest)
2024-09-13 09:12:16 +02:00
return
2024-09-11 09:54:55 +02:00
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"data": payload,
})
if err != nil {
if s.config.Debug {
slog.Debug("Failed to encode JSON", "error", err)
}
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
return
}
2024-09-11 09:54:55 +02:00
}
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "*")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
2024-09-13 09:12:16 +02:00
func NewServer(cfg config.Config) (*Server, error) {
expirationDuration, err := time.ParseDuration(cfg.Expire+"s")
if err != nil {
fmt.Printf("%+v\n", err)
}
client, err := client.New(cfg.HmacKey, cfg.MaxNumber, cfg.Algorithm, cfg.Salt, expirationDuration, cfg.CheckExpire)
if err != nil {
return &Server{}, err
}
2024-09-11 09:54:55 +02:00
return &Server {
2024-09-11 12:09:43 +02:00
baseUrl: cfg.BaseUrl,
2024-09-11 09:54:55 +02:00
port: cfg.Port,
2024-09-13 09:12:16 +02:00
client: *client,
config: cfg,
2024-09-13 09:12:16 +02:00
}, nil
2024-09-11 09:54:55 +02:00
}