Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 2 additions & 37 deletions sdk/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,41 +129,6 @@ func (a Assertion) GetHash() ([]byte, error) {
return ocrypto.SHA256AsHex(transformedJSON), nil
}

func (s *Statement) UnmarshalJSON(data []byte) error {
// Define a custom struct for deserialization
type Alias Statement
aux := &struct {
Value json.RawMessage `json:"value,omitempty"`
*Alias
}{
Alias: (*Alias)(s),
}

if err := json.Unmarshal(data, &aux); err != nil {
return err
}

// Attempt to decode Value as an object
var temp map[string]interface{}
if json.Unmarshal(aux.Value, &temp) == nil {
// Re-encode the object as a string and assign to Value
objAsString, err := json.Marshal(temp)
if err != nil {
return err
}
s.Value = string(objAsString)
} else {
// Assign raw string to Value
var str string
if err := json.Unmarshal(aux.Value, &str); err != nil {
return fmt.Errorf("value is neither a valid JSON object nor a string: %s", string(aux.Value))
}
s.Value = str
}

return nil
}

// Statement includes information applying to the scope of the assertion.
// It could contain rights, handling instructions, or general metadata.
type Statement struct {
Expand All @@ -172,7 +137,7 @@ type Statement struct {
// Schema describes the schema of the payload. (e.g. tdf)
Schema string `json:"schema,omitempty" validate:"required"`
// Value is the payload of the assertion.
Value string `json:"value,omitempty" validate:"required"`
Value json.RawMessage `json:"value,omitempty" validate:"required"`
}

// Binding enforces cryptographic integrity of the assertion.
Expand Down Expand Up @@ -327,7 +292,7 @@ func GetSystemMetadataAssertionConfig() (AssertionConfig, error) {
Statement: Statement{
Format: "json",
Schema: SystemMetadataSchemaV1,
Value: string(metadataJSON),
Value: metadataJSON,
},
}, nil
}
12 changes: 6 additions & 6 deletions sdk/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestTDFWithAssertion(t *testing.T) {
Statement: Statement{
Format: "json+stanag5636",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"ocl\":{\"pol\":\"62c76c68-d73d-4628-8ccc-4c1e18118c22\",\"cls\":\"SECRET\",\"catl\":[{\"type\":\"P\",\"name\":\"Releasable To\",\"vals\":[\"usa\"]}],\"dcr\":\"2024-10-21T20:47:36Z\"},\"context\":{\"@base\":\"urn:nato:stanag:5636:A:1:elements:json\"}}",
Value: []byte("{\"ocl\":{\"pol\":\"62c76c68-d73d-4628-8ccc-4c1e18118c22\",\"cls\":\"SECRET\",\"catl\":[{\"type\":\"P\",\"name\":\"Releasable To\",\"vals\":[\"usa\"]}],\"dcr\":\"2024-10-21T20:47:36Z\"},\"context\":{\"@base\":\"urn:nato:stanag:5636:A:1:elements:json\"}}"),
},
}

Expand All @@ -33,12 +33,12 @@ func TestTDFWithAssertion(t *testing.T) {
hashOfAssertion, err := assertion.GetHash()
require.NoError(t, err)

assert.Equal(t, "4a447a13c5a32730d20bdf7feecb9ffe16649bc731914b574d80035a3927f860", string(hashOfAssertion))
assert.Equal(t, "cf73d5df901bc81fc697594c4af0e528b859674ffcd34df4ba385f13a3579650", string(hashOfAssertion))
}

func TestTDFWithAssertionJsonObject(t *testing.T) {
// Define the assertion config with a JSON object in the statement value
value := `{
value := []byte(`{
"ocl": {
"pol": "2ccf11cb-6c9a-4e49-9746-a7f0a295945d",
"cls": "SECRET",
Expand All @@ -54,7 +54,7 @@ func TestTDFWithAssertionJsonObject(t *testing.T) {
"context": {
"@base": "urn:nato:stanag:5636:A:1:elements:json"
}
}`
}`)
assertionConfig := AssertionConfig{
ID: "ab43266781e64b51a4c52ffc44d6152c",
Type: "handling",
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestTDFWithAssertionJsonObject(t *testing.T) {
hashOfAssertion, err := assertion.GetHash()
require.NoError(t, err)

expectedHash := "722dd40a90a0f7ec718fb156207a647e64daa43c0ae1f033033473a172c72aee"
expectedHash := "eef5daaa17ec25312f2f254d5471fbc1866fabbf52fbc07d4941cb1bc1f1f373"
assert.Equal(t, expectedHash, string(hashOfAssertion))
}

Expand Down Expand Up @@ -180,5 +180,5 @@ func TestDeserializingAssertionWithStringInStatementValue(t *testing.T) {
err := json.Unmarshal([]byte(assertionVal), &assertion)
require.NoError(t, err, "Error deserializing the assertion with a JSON object in the statement value")

assert.Equal(t, "this is a value", assertion.Statement.Value)
assert.Equal(t, json.RawMessage(`"this is a value"`), assertion.Statement.Value)
}
34 changes: 17 additions & 17 deletions sdk/tdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "base64binary",
Schema: "text",
Value: "ICAgIDxlZGoOkVkaD4=",
Value: []byte(`"ICAgIDxlZGoOkVkaD4="`),
},
},
{
Expand All @@ -728,7 +728,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "json",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}",
Value: []byte("{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}"),
},
},
},
Expand All @@ -746,7 +746,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "base64binary",
Schema: "text",
Value: "ICAgIDxlZGoOkVkaD4=",
Value: []byte(`"ICAgIDxlZGoOkVkaD4="`),
},
},
{
Expand All @@ -757,7 +757,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "json",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}",
Value: []byte("{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}"),
},
},
},
Expand All @@ -776,7 +776,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "base64binary",
Schema: "text",
Value: "ICAgIDxlZGoOkVkaD4=",
Value: []byte(`"ICAgIDxlZGoOkVkaD4="`),
},
SigningKey: defaultKey,
},
Expand All @@ -788,7 +788,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "json",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}",
Value: []byte("{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}"),
},
SigningKey: defaultKey,
},
Expand All @@ -809,7 +809,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "base64binary",
Schema: "text",
Value: "ICAgIDxlZGoOkVkaD4=",
Value: []byte(`"ICAgIDxlZGoOkVkaD4="`),
},
SigningKey: AssertionKey{
Alg: AssertionKeyAlgHS256,
Expand All @@ -824,7 +824,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "json",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}",
Value: []byte("{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}"),
},
SigningKey: AssertionKey{
Alg: AssertionKeyAlgRS256,
Expand Down Expand Up @@ -858,7 +858,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "base64binary",
Schema: "text",
Value: "ICAgIDxlZGoOkVkaD4=",
Value: []byte(`"ICAgIDxlZGoOkVkaD4="`),
},
SigningKey: AssertionKey{
Alg: AssertionKeyAlgHS256,
Expand All @@ -873,7 +873,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "json",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}",
Value: []byte("{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}"),
},
},
},
Expand All @@ -898,7 +898,7 @@ func (s *TDFSuite) Test_TDFWithAssertion() {
Statement: Statement{
Format: "json",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}",
Value: []byte("{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}"),
},
},
},
Expand Down Expand Up @@ -1123,7 +1123,7 @@ func (s *TDFSuite) Test_TDFWithAssertionNegativeTests() {
Statement: Statement{
Format: "base64binary",
Schema: "text",
Value: "ICAgIDxlZGoOkVkaD4=",
Value: []byte(`"ICAgIDxlZGoOkVkaD4="`),
},
SigningKey: defaultKey,
},
Expand All @@ -1135,7 +1135,7 @@ func (s *TDFSuite) Test_TDFWithAssertionNegativeTests() {
Statement: Statement{
Format: "json",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}",
Value: []byte("{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}"),
},
SigningKey: defaultKey,
},
Expand All @@ -1152,7 +1152,7 @@ func (s *TDFSuite) Test_TDFWithAssertionNegativeTests() {
Statement: Statement{
Format: "base64binary",
Schema: "text",
Value: "ICAgIDxlZGoOkVkaD4=",
Value: []byte(`"ICAgIDxlZGoOkVkaD4="`),
},
SigningKey: AssertionKey{
Alg: AssertionKeyAlgHS256,
Expand All @@ -1167,7 +1167,7 @@ func (s *TDFSuite) Test_TDFWithAssertionNegativeTests() {
Statement: Statement{
Format: "json",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}",
Value: []byte(`"{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}"`),
},
SigningKey: AssertionKey{
Alg: AssertionKeyAlgRS256,
Expand Down Expand Up @@ -1200,7 +1200,7 @@ func (s *TDFSuite) Test_TDFWithAssertionNegativeTests() {
Statement: Statement{
Format: "base64binary",
Schema: "text",
Value: "ICAgIDxlZGoOkVkaD4=",
Value: []byte(`"ICAgIDxlZGoOkVkaD4="`),
},
SigningKey: AssertionKey{
Alg: AssertionKeyAlgHS256,
Expand All @@ -1215,7 +1215,7 @@ func (s *TDFSuite) Test_TDFWithAssertionNegativeTests() {
Statement: Statement{
Format: "json",
Schema: "urn:nato:stanag:5636:A:1:elements:json",
Value: "{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}",
Value: []byte("{\"uuid\":\"f74efb60-4a9a-11ef-a6f1-8ee1a61c148a\",\"body\":{\"dataAttributes\":null,\"dissem\":null}}"),
},
},
},
Expand Down