fix: error module implementation
This commit is contained in:
@@ -26,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) {
|
func (d *decrypter) Decrypt(ctx context.Context, encryptedData, privateKeyBase64, publicKeyBase64 string) (string, error) {
|
||||||
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyBase64)
|
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyBase64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", model.NewBadReqErr(err)
|
return "", model.NewBadReqErr(fmt.Errorf("invalid private key: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyBase64)
|
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyBase64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", model.NewBadReqErr(err)
|
return "", model.NewBadReqErr(fmt.Errorf("invalid public key: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the Base64 encoded encrypted data.
|
// Decode the Base64 encoded encrypted data.
|
||||||
messageByte, err := base64.StdEncoding.DecodeString(encryptedData)
|
messageByte, err := base64.StdEncoding.DecodeString(encryptedData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", model.NewBadReqErr(err)
|
return "", model.NewBadReqErr(fmt.Errorf("failed to decode encrypted data: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
aesCipher, err := createAESCipher(privateKeyBytes, publicKeyBytes)
|
aesCipher, err := createAESCipher(privateKeyBytes, publicKeyBytes)
|
||||||
|
|||||||
@@ -24,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) {
|
func (e *encrypter) Encrypt(ctx context.Context, data string, privateKeyBase64, publicKeyBase64 string) (string, error) {
|
||||||
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyBase64)
|
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyBase64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", model.NewBadReqErr(err)
|
return "", model.NewBadReqErr(fmt.Errorf("invalid private key: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyBase64)
|
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyBase64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", model.NewBadReqErr(err)
|
return "", model.NewBadReqErr(fmt.Errorf("invalid public key: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the input string to a byte slice.
|
// Convert the input string to a byte slice.
|
||||||
@@ -51,11 +51,11 @@ func createAESCipher(privateKey, publicKey []byte) (cipher.Block, error) {
|
|||||||
x25519Curve := ecdh.X25519()
|
x25519Curve := ecdh.X25519()
|
||||||
x25519PrivateKey, err := x25519Curve.NewPrivateKey(privateKey)
|
x25519PrivateKey, err := x25519Curve.NewPrivateKey(privateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, model.NewBadReqErr(err)
|
return nil, model.NewBadReqErr(fmt.Errorf("failed to create private key: %w", err))
|
||||||
}
|
}
|
||||||
x25519PublicKey, err := x25519Curve.NewPublicKey(publicKey)
|
x25519PublicKey, err := x25519Curve.NewPublicKey(publicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, model.NewBadReqErr(err)
|
return nil, model.NewBadReqErr(fmt.Errorf("failed to create public key: %w", err))
|
||||||
}
|
}
|
||||||
sharedSecret, err := x25519PrivateKey.ECDH(x25519PublicKey)
|
sharedSecret, err := x25519PrivateKey.ECDH(x25519PublicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/beckn/beckn-onix/pkg/log"
|
||||||
"github.com/beckn/beckn-onix/pkg/model"
|
"github.com/beckn/beckn-onix/pkg/model"
|
||||||
|
|
||||||
"github.com/santhosh-tekuri/jsonschema/v6"
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
||||||
@@ -39,7 +40,7 @@ type Config struct {
|
|||||||
func New(ctx context.Context, config *Config) (*schemaValidator, func() error, error) {
|
func New(ctx context.Context, config *Config) (*schemaValidator, func() error, error) {
|
||||||
// Check if config is nil
|
// Check if config is nil
|
||||||
if config == nil {
|
if config == nil {
|
||||||
return nil, nil, model.NewBadReqErr(errors.New("config cannot be nil"))
|
return nil, nil, fmt.Errorf("config cannot be nil")
|
||||||
}
|
}
|
||||||
v := &schemaValidator{
|
v := &schemaValidator{
|
||||||
config: config,
|
config: config,
|
||||||
@@ -58,7 +59,7 @@ func (v *schemaValidator) Validate(ctx context.Context, url *url.URL, data []byt
|
|||||||
var payloadData payload
|
var payloadData payload
|
||||||
err := json.Unmarshal(data, &payloadData)
|
err := json.Unmarshal(data, &payloadData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to parse JSON payload: %v", err)
|
return model.NewBadReqErr(fmt.Errorf("failed to parse JSON payload: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract domain, version, and endpoint from the payload and uri.
|
// Extract domain, version, and endpoint from the payload and uri.
|
||||||
@@ -67,8 +68,7 @@ func (v *schemaValidator) Validate(ctx context.Context, url *url.URL, data []byt
|
|||||||
version = fmt.Sprintf("v%s", version)
|
version = fmt.Sprintf("v%s", version)
|
||||||
|
|
||||||
endpoint := path.Base(url.String())
|
endpoint := path.Base(url.String())
|
||||||
// ToDo Add debug log here
|
log.Debugf(ctx, "Handling request for endpoint: %s", endpoint)
|
||||||
fmt.Println("Handling request for endpoint:", endpoint)
|
|
||||||
domain := strings.ToLower(cxtDomain)
|
domain := strings.ToLower(cxtDomain)
|
||||||
domain = strings.ReplaceAll(domain, ":", "_")
|
domain = strings.ReplaceAll(domain, ":", "_")
|
||||||
|
|
||||||
@@ -78,12 +78,12 @@ func (v *schemaValidator) Validate(ctx context.Context, url *url.URL, data []byt
|
|||||||
// Retrieve the schema from the cache.
|
// Retrieve the schema from the cache.
|
||||||
schema, exists := v.schemaCache[schemaFileName]
|
schema, exists := v.schemaCache[schemaFileName]
|
||||||
if !exists {
|
if !exists {
|
||||||
return model.NewNotFoundErr(errors.New("schema not found for domain"))
|
return model.NewBadReqErr(errors.New("schema not found for domain"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var jsonData any
|
var jsonData any
|
||||||
if err := json.Unmarshal(data, &jsonData); err != nil {
|
if err := json.Unmarshal(data, &jsonData); err != nil {
|
||||||
return model.NewBadReqErr(err)
|
return model.NewBadReqErr(fmt.Errorf("failed to parse JSON data: %v", err))
|
||||||
}
|
}
|
||||||
err = schema.Validate(jsonData)
|
err = schema.Validate(jsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -105,7 +105,7 @@ func (v *schemaValidator) Validate(ctx context.Context, url *url.URL, data []byt
|
|||||||
// Return the array of schema validation errors
|
// Return the array of schema validation errors
|
||||||
return &model.SchemaValidationErr{Errors: schemaErrors}
|
return &model.SchemaValidationErr{Errors: schemaErrors}
|
||||||
}
|
}
|
||||||
return model.NewBadReqErr(err)
|
return model.NewBadReqErr(fmt.Errorf("validation failed: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return nil if validation succeeds
|
// Return nil if validation succeeds
|
||||||
@@ -190,7 +190,7 @@ func (v *schemaValidator) initialise() error {
|
|||||||
|
|
||||||
// Start processing from the root schema directory.
|
// Start processing from the root schema directory.
|
||||||
if err := processDir(schemaDir); err != nil {
|
if err := processDir(schemaDir); err != nil {
|
||||||
return model.NewNotFoundErr(err)
|
return fmt.Errorf("failed to read schema directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func hash(payload []byte, createdAt, expiresAt int64) (string, error) {
|
|||||||
|
|
||||||
_, err := hasher.Write(payload)
|
_, err := hasher.Write(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", model.NewBadReqErr(err)
|
return "", fmt.Errorf("failed to hash payload: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hashSum := hasher.Sum(nil)
|
hashSum := hasher.Sum(nil)
|
||||||
@@ -46,7 +46,7 @@ func hash(payload []byte, createdAt, expiresAt int64) (string, error) {
|
|||||||
func generateSignature(signingString []byte, privateKeyBase64 string) ([]byte, error) {
|
func generateSignature(signingString []byte, privateKeyBase64 string) ([]byte, error) {
|
||||||
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyBase64)
|
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyBase64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, model.NewBadReqErr(err)
|
return nil, fmt.Errorf("error decoding private key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(privateKeyBytes) != ed25519.SeedSize {
|
if len(privateKeyBytes) != ed25519.SeedSize {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -34,7 +33,7 @@ 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 {
|
func (v *validator) Validate(ctx context.Context, body []byte, header string, publicKeyBase64 string) error {
|
||||||
createdTimestamp, expiredTimestamp, signature, err := parseAuthHeader(header)
|
createdTimestamp, expiredTimestamp, signature, err := parseAuthHeader(header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.NewBadReqErr(err)
|
return model.NewBadReqErr(fmt.Errorf("error parsing header: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
signatureBytes, err := base64.StdEncoding.DecodeString(signature)
|
signatureBytes, err := base64.StdEncoding.DecodeString(signature)
|
||||||
@@ -44,7 +43,7 @@ func (v *validator) Validate(ctx context.Context, body []byte, header string, pu
|
|||||||
|
|
||||||
currentTime := time.Now().Unix()
|
currentTime := time.Now().Unix()
|
||||||
if createdTimestamp > currentTime || currentTime > expiredTimestamp {
|
if createdTimestamp > currentTime || currentTime > expiredTimestamp {
|
||||||
return model.NewSignValidationErr(errors.New("signature is expired or not yet valid"))
|
return model.NewSignValidationErr(fmt.Errorf("signature is expired or not yet valid"))
|
||||||
}
|
}
|
||||||
|
|
||||||
createdTime := time.Unix(createdTimestamp, 0)
|
createdTime := time.Unix(createdTimestamp, 0)
|
||||||
@@ -54,11 +53,11 @@ func (v *validator) Validate(ctx context.Context, body []byte, header string, pu
|
|||||||
|
|
||||||
decodedPublicKey, err := base64.StdEncoding.DecodeString(publicKeyBase64)
|
decodedPublicKey, err := base64.StdEncoding.DecodeString(publicKeyBase64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.NewBadReqErr(err)
|
return model.NewBadReqErr(fmt.Errorf("error decoding public key: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ed25519.Verify(ed25519.PublicKey(decodedPublicKey), []byte(signingString), signatureBytes) {
|
if !ed25519.Verify(ed25519.PublicKey(decodedPublicKey), []byte(signingString), signatureBytes) {
|
||||||
return model.NewSignValidationErr(errors.New("signature verification failed"))
|
return model.NewSignValidationErr(fmt.Errorf("signature verification failed"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -88,13 +87,13 @@ func parseAuthHeader(header string) (int64, int64, string, error) {
|
|||||||
|
|
||||||
expiredTimestamp, err := strconv.ParseInt(signatureMap["expires"], 10, 64)
|
expiredTimestamp, err := strconv.ParseInt(signatureMap["expires"], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, "", model.NewSignValidationErr(err)
|
return 0, 0, "", model.NewSignValidationErr(fmt.Errorf("invalid expires timestamp: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
signature := signatureMap["signature"]
|
signature := signatureMap["signature"]
|
||||||
if signature == "" {
|
if signature == "" {
|
||||||
// TODO: Return appropriate error code when Error Code Handling Module is ready
|
// TODO: Return appropriate error code when Error Code Handling Module is ready
|
||||||
return 0, 0, "", model.NewSignValidationErr(errors.New("signature missing in header"))
|
return 0, 0, "", model.NewSignValidationErr(fmt.Errorf("signature missing in header"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return createdTimestamp, expiredTimestamp, signature, nil
|
return createdTimestamp, expiredTimestamp, signature, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user