altcha-server/internal/client/client.go
2024-09-13 09:12:16 +02:00

64 lines
1.6 KiB
Go

package client
import (
"errors"
"time"
"github.com/altcha-org/altcha-lib-go"
)
type Client struct {
hmacKey string
maxNumber int64
algorithm altcha.Algorithm
salt string
expire time.Duration
checkExpire bool
}
func New(hmacKey string, maxNumber int64, algorithm string, salt string, expire time.Duration, checkExpire bool) (*Client, error) {
if len(hmacKey) == 0 {
return &Client{}, errors.New("HMAC key not found")
}
return &Client {
hmacKey: hmacKey,
maxNumber: maxNumber,
algorithm: altcha.Algorithm(algorithm),
salt: salt,
expire: expire,
checkExpire: checkExpire,
}, nil
}
func (c *Client) Generate() (altcha.Challenge, error) {
expiration := time.Now().Add(c.expire)
options := altcha.ChallengeOptions{
HMACKey: c.hmacKey,
MaxNumber: c.maxNumber,
Algorithm: c.algorithm,
Expires: &expiration,
}
if len(c.salt) > 0 {
options.Salt = c.salt
}
return altcha.CreateChallenge(options)
}
func (c *Client) Solve(challenge string) (*altcha.Solution, error) {
return altcha.SolveChallenge(challenge, c.salt, c.algorithm, int(c.maxNumber), 0, make(<-chan struct{}))
}
func (c *Client) VerifySolution(payload interface{}) (bool, error) {
return altcha.VerifySolution(payload, c.hmacKey, c.checkExpire)
}
func (c *Client) VerifyServerSignature(payload interface{}) (bool, altcha.ServerSignatureVerificationData, error) {
return altcha.VerifyServerSignature(payload, c.hmacKey)
}
func (c *Client) VerifyFieldsHash(formData map[string][]string, fields []string, fieldsHash string) (bool, error) {
return altcha.VerifyFieldsHash(formData, fields, fieldsHash, c.algorithm)
}