altcha-server/internal/command/verify.go

66 lines
1.5 KiB
Go
Raw Normal View History

2024-09-11 09:54:55 +02:00
package command
import (
"fmt"
"strconv"
2024-09-13 09:12:16 +02:00
"time"
2024-09-11 09:54:55 +02:00
"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/caarlos0/env/v11"
"github.com/urfave/cli/v2"
2024-09-13 09:12:16 +02:00
"gitlab.com/wpetit/goweb/logger"
2024-09-11 09:54:55 +02:00
)
func VerifyCommand() *cli.Command {
return &cli.Command{
Name: "verify",
Usage: "verify the solution",
Args: true,
ArgsUsage: "[challenge] [salt] [signature] [solution]",
Action: func(ctx *cli.Context) error {
cfg := config.Config{}
if err := env.Parse(&cfg); err != nil {
2024-09-13 09:12:16 +02:00
logger.Error(ctx.Context, err.Error())
return err
2024-09-11 09:54:55 +02:00
}
challenge := ctx.Args().Get(0)
salt := ctx.Args().Get(1)
signature := ctx.Args().Get(2)
solution, _ := strconv.ParseInt(ctx.Args().Get(3), 10, 64)
2024-09-13 09:12:16 +02:00
expirationDuration, err := time.ParseDuration(cfg.Expire+"s")
if err != nil {
logger.Error(ctx.Context, err.Error())
return err
}
client, err := client.New(cfg.HmacKey, cfg.MaxNumber, cfg.Algorithm, cfg.Salt, expirationDuration, cfg.CheckExpire)
if err != nil {
logger.Error(ctx.Context, err.Error())
return err
}
2024-09-11 09:54:55 +02:00
payload := altcha.Payload{
Algorithm: cfg.Algorithm,
Challenge: challenge,
Number: solution,
Salt: salt,
Signature: signature,
}
2024-09-13 09:12:16 +02:00
verified, err := client.VerifySolution(payload)
2024-09-11 09:54:55 +02:00
if err != nil {
2024-09-13 09:12:16 +02:00
logger.Error(ctx.Context, err.Error())
2024-09-11 09:54:55 +02:00
return err
}
fmt.Print(verified)
return nil
},
}
}