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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Run(ctx context.Context) {
|
|
|
|
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-16 15:06:49 +02:00
|
|
|
|
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 {
|
|
|
|
http.Error(w, fmt.Sprintf("Failed to create challenge : %s", err), http.StatusInternalServerError)
|
2024-09-16 15:06:49 +02:00
|
|
|
slog.Error("Failed to create challenge,", "error", err)
|
2024-09-11 09:54:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeJSON(w, challenge)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) submitHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var payload map[string]interface{}
|
2024-09-16 15:06:49 +02:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&payload)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("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-16 15:06:49 +02:00
|
|
|
|
2024-09-11 09:54:55 +02:00
|
|
|
verified, err := s.client.VerifySolution(payload)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-09-16 15:06:49 +02:00
|
|
|
slog.Error("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
|
|
|
}
|
|
|
|
|
2024-09-16 15:06:49 +02:00
|
|
|
if !verified {
|
|
|
|
slog.Error("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
|
|
|
}
|
2024-09-16 15:06:49 +02:00
|
|
|
|
|
|
|
writeJSON(w, map[string]interface{}{
|
|
|
|
"success": true,
|
|
|
|
"data": payload,
|
|
|
|
})
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeJSON(w http.ResponseWriter, data interface{}) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if err := json.NewEncoder(w).Encode(data); err != nil {
|
|
|
|
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formToMap(r *http.Request) (map[string][]string, error) {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Form, nil
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}, nil
|
2024-09-11 09:54:55 +02:00
|
|
|
}
|