fix: added error from error module
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/zenazn/pkcs7pad"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/model"
|
||||
)
|
||||
|
||||
// decrypter implements the Decrypter interface and handles the decryption process.
|
||||
@@ -24,18 +26,18 @@ func New(ctx context.Context) (*decrypter, func() error, error) {
|
||||
func (d *decrypter) Decrypt(ctx context.Context, encryptedData, privateKeyBase64, publicKeyBase64 string) (string, error) {
|
||||
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyBase64)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid private key: %w", err)
|
||||
return "", model.NewBadReqErr(err)
|
||||
}
|
||||
|
||||
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyBase64)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid public key: %w", err)
|
||||
return "", model.NewBadReqErr(err)
|
||||
}
|
||||
|
||||
// Decode the Base64 encoded encrypted data.
|
||||
messageByte, err := base64.StdEncoding.DecodeString(encryptedData)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode encrypted data: %w", err)
|
||||
return "", model.NewBadReqErr(err)
|
||||
}
|
||||
|
||||
aesCipher, err := createAESCipher(privateKeyBytes, publicKeyBytes)
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/model"
|
||||
"github.com/zenazn/pkcs7pad"
|
||||
)
|
||||
|
||||
@@ -23,12 +24,12 @@ func New(ctx context.Context) (*encrypter, func() error, error) {
|
||||
func (e *encrypter) Encrypt(ctx context.Context, data string, privateKeyBase64, publicKeyBase64 string) (string, error) {
|
||||
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyBase64)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid private key: %w", err)
|
||||
return "", model.NewBadReqErr(err)
|
||||
}
|
||||
|
||||
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyBase64)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid public key: %w", err)
|
||||
return "", model.NewBadReqErr(err)
|
||||
}
|
||||
|
||||
// Convert the input string to a byte slice.
|
||||
@@ -50,11 +51,11 @@ func createAESCipher(privateKey, publicKey []byte) (cipher.Block, error) {
|
||||
x25519Curve := ecdh.X25519()
|
||||
x25519PrivateKey, err := x25519Curve.NewPrivateKey(privateKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create private key: %w", err)
|
||||
return nil, model.NewBadReqErr(err)
|
||||
}
|
||||
x25519PublicKey, err := x25519Curve.NewPublicKey(publicKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create public key: %w", err)
|
||||
return nil, model.NewBadReqErr(err)
|
||||
}
|
||||
sharedSecret, err := x25519PrivateKey.ECDH(x25519PublicKey)
|
||||
if err != nil {
|
||||
|
||||
@@ -3,6 +3,7 @@ package schemavalidator
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -77,12 +78,12 @@ func (v *schemaValidator) Validate(ctx context.Context, url *url.URL, data []byt
|
||||
// Retrieve the schema from the cache.
|
||||
schema, exists := v.schemaCache[schemaFileName]
|
||||
if !exists {
|
||||
return fmt.Errorf("schema not found for domain: %s", schemaFileName)
|
||||
return model.NewNotFoundErr(errors.New("schema not found for domain"))
|
||||
}
|
||||
|
||||
var jsonData any
|
||||
if err := json.Unmarshal(data, &jsonData); err != nil {
|
||||
return fmt.Errorf("failed to parse JSON data: %v", err)
|
||||
return model.NewBadReqErr(err)
|
||||
}
|
||||
err = schema.Validate(jsonData)
|
||||
if err != nil {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/model"
|
||||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
@@ -32,7 +33,7 @@ func hash(payload []byte, createdAt, expiresAt int64) (string, error) {
|
||||
|
||||
_, err := hasher.Write(payload)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to hash payload: %w", err)
|
||||
return "", model.NewBadReqErr(err)
|
||||
}
|
||||
|
||||
hashSum := hasher.Sum(nil)
|
||||
@@ -45,11 +46,11 @@ func hash(payload []byte, createdAt, expiresAt int64) (string, error) {
|
||||
func generateSignature(signingString []byte, privateKeyBase64 string) ([]byte, error) {
|
||||
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyBase64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error decoding private key: %w", err)
|
||||
return nil, model.NewBadReqErr(err)
|
||||
}
|
||||
|
||||
if len(privateKeyBytes) != ed25519.SeedSize {
|
||||
return nil, errors.New("invalid seed length")
|
||||
return nil, model.NewBadReqErr(errors.New("invalid seed length"))
|
||||
}
|
||||
|
||||
// Generate the private key from the seed
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user