fix: added error from error module

This commit is contained in:
mayur.popli
2025-04-03 13:35:49 +05:30
parent a2c27b4fbf
commit a955949857
5 changed files with 25 additions and 24 deletions

View File

@@ -4,11 +4,13 @@ import (
"context"
"crypto/ed25519"
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/beckn/beckn-onix/pkg/model"
"golang.org/x/crypto/blake2b"
)
@@ -32,20 +34,17 @@ func New(ctx context.Context, config *Config) (*validator, func() error, error)
func (v *validator) Validate(ctx context.Context, body []byte, header string, publicKeyBase64 string) error {
createdTimestamp, expiredTimestamp, signature, err := parseAuthHeader(header)
if err != nil {
// TODO: Return appropriate error code when Error Code Handling Module is ready
return fmt.Errorf("error parsing header: %w", err)
return model.NewBadReqErr(err)
}
signatureBytes, err := base64.StdEncoding.DecodeString(signature)
if err != nil {
// TODO: Return appropriate error code when Error Code Handling Module is ready
return fmt.Errorf("error decoding signature: %w", err)
}
currentTime := time.Now().Unix()
if createdTimestamp > currentTime || currentTime > expiredTimestamp {
// TODO: Return appropriate error code when Error Code Handling Module is ready
return fmt.Errorf("signature is expired or not yet valid")
return model.NewSignValidationErr(errors.New("signature is expired or not yet valid"))
}
createdTime := time.Unix(createdTimestamp, 0)
@@ -55,13 +54,11 @@ func (v *validator) Validate(ctx context.Context, body []byte, header string, pu
decodedPublicKey, err := base64.StdEncoding.DecodeString(publicKeyBase64)
if err != nil {
// TODO: Return appropriate error code when Error Code Handling Module is ready
return fmt.Errorf("error decoding public key: %w", err)
return model.NewBadReqErr(err)
}
if !ed25519.Verify(ed25519.PublicKey(decodedPublicKey), []byte(signingString), signatureBytes) {
// TODO: Return appropriate error code when Error Code Handling Module is ready
return fmt.Errorf("signature verification failed")
return model.NewSignValidationErr(errors.New("signature verification failed"))
}
return nil
@@ -91,14 +88,13 @@ func parseAuthHeader(header string) (int64, int64, string, error) {
expiredTimestamp, err := strconv.ParseInt(signatureMap["expires"], 10, 64)
if err != nil {
// TODO: Return appropriate error code when Error Code Handling Module is ready
return 0, 0, "", fmt.Errorf("invalid expires timestamp: %w", err)
return 0, 0, "", model.NewSignValidationErr(err)
}
signature := signatureMap["signature"]
if signature == "" {
// TODO: Return appropriate error code when Error Code Handling Module is ready
return 0, 0, "", fmt.Errorf("signature missing in header")
return 0, 0, "", model.NewSignValidationErr(errors.New("signature missing in header"))
}
return createdTimestamp, expiredTimestamp, signature, nil