Skip to content

Commit

Permalink
move functions to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
cormacdalton committed Jan 31, 2024
1 parent 82813dd commit 536bdfe
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 46 deletions.
54 changes: 8 additions & 46 deletions encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package encryption
import (
"github.com/Jeffail/gabs/v2"
"github.com/mastercard/client-encryption-go/jwe"
"strings"
"github.com/mastercard/client-encryption-go/utils"
)

func EncryptPayload(payload string, config jwe.JWEConfig) string {
Expand Down Expand Up @@ -32,9 +32,9 @@ func encryptPayloadPath(jsonPayload *gabs.Container, jsonPathIn string, jsonPath
Kid: config.GetEncryptionKeyFingerprint(),
Cty: "application/json",
}
jsonPathIn = removeJsonRoot(jsonPathIn)
jsonPathOut = removeJsonRoot(jsonPathOut)
payloadToEncrypt := getPayloadToEncrypt(jsonPayload, jsonPathIn)
jsonPathIn = utils.RemoveRoot(jsonPathIn)
jsonPathOut = utils.RemoveRoot(jsonPathOut)
payloadToEncrypt := utils.GetPayloadToEncrypt(jsonPayload, jsonPathIn)
payload, err := jwe.Encrypt(config, payloadToEncrypt, joseHeader)
if err != nil {
panic(err)
Expand All @@ -47,18 +47,15 @@ func encryptPayloadPath(jsonPayload *gabs.Container, jsonPathIn string, jsonPath
if jsonPathOut == "$" {
jsonPayload.SetP(payload, config.GetEncryptedValueFieldName())
} else {
if jsonPathOut[0] == '$' {
jsonPathOut = jsonPathOut[2:]
}
jsonPayload.SetP(payload, jsonPathOut+"."+config.GetEncryptedValueFieldName())
}
return jsonPayload
}

func decryptPayloadPath(jsonPayload *gabs.Container, jsonPathIn string, jsonPathOut string, config jwe.JWEConfig) *gabs.Container {
jsonPathIn = removeJsonRoot(jsonPathIn)
jsonPathOut = removeJsonRoot(jsonPathOut)
encryptedPayload := getPayloadToDecrypt(jsonPayload, jsonPathIn)
jsonPathIn = utils.RemoveRoot(jsonPathIn)
jsonPathOut = utils.RemoveRoot(jsonPathOut)
encryptedPayload := utils.GetPayloadToDecrypt(jsonPayload, jsonPathIn)
jweObject, err := jwe.ParseJWEObject(encryptedPayload)
if err != nil {
panic(err)
Expand All @@ -71,43 +68,8 @@ func decryptPayloadPath(jsonPayload *gabs.Container, jsonPathIn string, jsonPath
if jsonPathOut == "$" {
jsonPayload = jsonDecryptedPayload
} else {
if jsonPathOut[0] == '$' {
jsonPathOut = jsonPathOut[2:]
}
jsonPayload.DeleteP(getParent(jsonPathIn))
jsonPayload.DeleteP(utils.GetParent(jsonPathIn))
jsonPayload.SetP(jsonDecryptedPayload, jsonPathOut)
}
return jsonPayload
}

func getPayloadToEncrypt(jsonPayload *gabs.Container, jsonPathIn string) string {
if jsonPathIn == "$" {
return jsonPayload.String()
} else {
return jsonPayload.Path(jsonPathIn).String()
}
}

func getPayloadToDecrypt(jsonPayload *gabs.Container, jsonPathIn string) string {
if jsonPathIn == "$" {
return jsonPayload.
Children()[0].
Data().(string)
} else {
return jsonPayload.Path(jsonPathIn).
Data().(string)
}
}

func removeJsonRoot(json string) string {
if json[0] == '$' && json != "$" {
return json[2:]
}
return json
}

func getParent(path string) string {
keys := strings.Split(path, ".")
parent := keys[:len(keys)-1]
return strings.Join(parent, ".")
}
20 changes: 20 additions & 0 deletions utils/encryption_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"github.com/Jeffail/gabs/v2"
"golang.org/x/crypto/pkcs12"
"io/ioutil"
"os"
Expand Down Expand Up @@ -73,6 +74,25 @@ func LoadUnencryptedDecryptionKey(keyFilePath string) (*rsa.PrivateKey, error) {
return key.(*rsa.PrivateKey), nil
}

func GetPayloadToEncrypt(jsonPayload *gabs.Container, jsonPathIn string) string {
if jsonPathIn == "$" {
return jsonPayload.String()
} else {
return jsonPayload.Path(jsonPathIn).String()
}
}

func GetPayloadToDecrypt(jsonPayload *gabs.Container, jsonPathIn string) string {
if jsonPathIn == "$" {
return jsonPayload.
Children()[0].
Data().(string)
} else {
return jsonPayload.Path(jsonPathIn).
Data().(string)
}
}

// Read File
func readFile(path string) ([]byte, error) {
file, err := os.Open(path)
Expand Down
33 changes: 33 additions & 0 deletions utils/encryption_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils_test

import (
"github.com/Jeffail/gabs/v2"
"testing"

"github.com/mastercard/client-encryption-go/utils"
Expand Down Expand Up @@ -30,3 +31,35 @@ func TestLoadUnencryptedDecryptionKey(t *testing.T) {
_, err := utils.LoadUnencryptedDecryptionKey(keyPath)
assert.Nil(t, err)
}

func TestGetPayloadToEncryptWithRootPayload_ShouldReturnEntirePayload(t *testing.T) {
jsonPath := "$"
payload := `{"path":{"to":{"foo":{"sensitiveField1":"sensitiveValue1","sensitiveField2":"sensitiveValue2"}}}}`
jsonPayload, _ := gabs.ParseJSON([]byte(payload))
payloadToEncrypt := utils.GetPayloadToEncrypt(jsonPayload, jsonPath)
assert.Equal(t, payload, payloadToEncrypt)
}

func TestGetPayloadToEncryptWithPayloadPath_ShouldReturnPayloadAtPath(t *testing.T) {
jsonPath := "path.to.foo"
payload := `{"path":{"to":{"foo":{"sensitiveField1":"sensitiveValue1","sensitiveField2":"sensitiveValue2"}}}}`
jsonPayload, _ := gabs.ParseJSON([]byte(payload))
payloadToEncrypt := utils.GetPayloadToEncrypt(jsonPayload, jsonPath)
assert.Equal(t, `{"sensitiveField1":"sensitiveValue1","sensitiveField2":"sensitiveValue2"}`, payloadToEncrypt)
}

func TestGetPayloadToDecryptWithRootPayload_ShouldReturnEntirePayload(t *testing.T) {
jsonPath := "$"
payload := `{"encryptedPayload":"abcdefg"}`
jsonPayload, _ := gabs.ParseJSON([]byte(payload))
payloadToDecrypt := utils.GetPayloadToDecrypt(jsonPayload, jsonPath)
assert.Equal(t, "abcdefg", payloadToDecrypt)
}

func TestGetPayloadToDecryptWithRootPayload_ShouldReturnPayloadAtPath(t *testing.T) {
jsonPath := "path.to.encryptedFoo.encryptedData"
payload := `{"path":{"to":{"encryptedFoo":{"encryptedData":"abcdefg"}}}}`
jsonPayload, _ := gabs.ParseJSON([]byte(payload))
payloadToDecrypt := utils.GetPayloadToDecrypt(jsonPayload, jsonPath)
assert.Equal(t, "abcdefg", payloadToDecrypt)
}
16 changes: 16 additions & 0 deletions utils/json_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package utils

import "strings"

func RemoveRoot(json string) string {
if json[0] == '$' && json != "$" {
return json[2:]
}
return json
}

func GetParent(path string) string {
keys := strings.Split(path, ".")
parent := keys[:len(keys)-1]
return strings.Join(parent, ".")
}
24 changes: 24 additions & 0 deletions utils/json_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestRemoveRoot_ShouldRemoveRootIfPresent(t *testing.T) {
path := "$.path.to.foo"
updatedPath := RemoveRoot(path)
assert.Equal(t, "path.to.foo", updatedPath)
}

func TestRemoveRoot_ShouldNotRemoveRootIfItIsARootPath(t *testing.T) {
path := "$"
updatedPath := RemoveRoot(path)
assert.Equal(t, "$", updatedPath)
}

func TestGetParent(t *testing.T) {
path := "$.path.to.foo"
parent := GetParent(path)
assert.Equal(t, "$.path.to", parent)
}

0 comments on commit 536bdfe

Please sign in to comment.