Compare commits

..

6 Commits

4 changed files with 74 additions and 13 deletions

View File

@ -10,6 +10,7 @@ import (
"forge.cadoles.com/cadoles/altcha-server/internal/client"
"forge.cadoles.com/cadoles/altcha-server/internal/config"
"github.com/altcha-org/altcha-lib-go"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
@ -66,14 +67,14 @@ func (s *Server) requestHandler(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) submitHandler(w http.ResponseWriter, r *http.Request) {
var payload map[string]interface{}
var payload altcha.Payload
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
slog.Debug("Failed to parse Altcha payload,", "error", err)
http.Error(w, "Failed to parse Altcha payload", http.StatusBadRequest)
return
}
verified, err := s.client.VerifySolution(payload)
if err != nil {
@ -82,9 +83,9 @@ func (s *Server) submitHandler(w http.ResponseWriter, r *http.Request) {
return
}
if !verified {
if !verified && !s.config.DisableValidation {
slog.Debug("Invalid solution")
http.Error(w, "Invalid solution,", http.StatusBadRequest)
http.Error(w, "Invalid solution", http.StatusBadRequest)
return
}

View File

@ -1,13 +1,14 @@
package config
type Config struct {
BaseUrl string `env:"ALTCHA_BASE_URL" envDefault:""`
Port string `env:"ALTCHA_PORT" envDefault:"3333"`
HmacKey string `env:"ALTCHA_HMAC_KEY"`
MaxNumber int64 `env:"ALTCHA_MAX_NUMBER" envDefault:"1000000"`
Algorithm string `env:"ALTCHA_ALGORITHM" envDefault:"SHA-256"`
Salt string `env:"ALTCHA_SALT"`
Expire string `env:"ALTCHA_EXPIRE" envDefault:"600"`
CheckExpire bool `env:"ALTCHA_CHECK_EXPIRE" envDefault:"1"`
Debug bool `env:"ALTCHA_DEBUG" envDefault:"false"`
BaseUrl string `env:"ALTCHA_BASE_URL" envDefault:""`
Port string `env:"ALTCHA_PORT" envDefault:"3333"`
HmacKey string `env:"ALTCHA_HMAC_KEY"`
MaxNumber int64 `env:"ALTCHA_MAX_NUMBER" envDefault:"1000000"`
Algorithm string `env:"ALTCHA_ALGORITHM" envDefault:"SHA-256"`
Salt string `env:"ALTCHA_SALT"`
Expire string `env:"ALTCHA_EXPIRE" envDefault:"600"`
CheckExpire bool `env:"ALTCHA_CHECK_EXPIRE" envDefault:"1"`
Debug bool `env:"ALTCHA_DEBUG" envDefault:"false"`
DisableValidation bool `env:"ALTCHA_DISABLE_VALIDATION" envDefault:"false"`
}

9
misc/k6/README.md Normal file
View File

@ -0,0 +1,9 @@
# K6 - Load Test
Very basic load testing script for [k6](https://k6.io/).
## How to run
```shell
k6 run -e LOAD_TEST_URL={altcha-api-url} TestChallenge.js
```

50
misc/k6/TestChallenge.js Normal file
View File

@ -0,0 +1,50 @@
import { check, group } from 'k6';
import http from 'k6/http'
export const options = {
scenarios: {
load: {
vus: 10,
iterations: 10,
executor: 'per-vu-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
}
};
http.setResponseCallback(
http.expectedStatuses(200)
);
export default function () {
let response;
group('Request challenge', function () {
response = http.get(`${__ENV.LOAD_TEST_URL}request`);
check(response, {
'is status 200': (r) => r.status === 200,
'Contenu souhaité': r => r.body.includes('algorithm') && r.body.includes('challenge') && r.body.includes('maxNumber') && r.body.includes('salt') && r.body.includes('signature'),
});
})
group('Verify challenge', function () {
const data = {"challenge":"d5656d52c5eadce5117024fbcafc706aad397c7befa17804d73c992d966012a8","salt":"8ec1b7ed694331baeb7416d9?expires=1727963398","signature":"781014d0a7ace7e7ae9d12e2d5c0204b60a8dbf42daa352ab40ab582b03a9dc6","number":219718,};
response = http.post(`${__ENV.LOAD_TEST_URL}verify`, JSON.stringify(data),
{
headers: { 'Content-Type': 'application/json' },
});
check(response, {
'is status 200': (r) => r.status === 200,
'Challenge verified': (r) => r.body.includes('true'),
});
})
}