Skip to content
This repository has been archived by the owner on Apr 27, 2023. It is now read-only.

Commit

Permalink
Credential values json serialize/deserialize
Browse files Browse the repository at this point in the history
* added cred vals serialization methods to header
* added cred vals serialization methods to Golang code
* updated value builder test
* fixed comment

Signed-off-by: konstantin.goncharov <[email protected]>
Signed-off-by: Ry Jones <[email protected]>
  • Loading branch information
konstantin.goncharov authored and ryjones committed Aug 9, 2022
1 parent c12e50a commit 5407e2a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build
.idea/
coverage.txt
*.out
.vscode/
20 changes: 20 additions & 0 deletions pkg/libursa/ursa/ursa_cl.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,26 @@ struct ExternError ursa_cl_credential_values_builder_finalize(const void *creden
*/
struct ExternError ursa_cl_credential_values_builder_new(const void **credential_values_builder_p);

/**
* Creates and returns credential values json.
*
* Note: Credential values instance deallocation must be performed by calling ursa_cl_credential_values_free.
*
* # Arguments
* * `credential_values_json` - Reference that contains credential values json.
* * `credential_values_p` - Reference that will contain credential values instance pointer.
*/
struct ExternError ursa_cl_credential_values_from_json(const char *credential_values_json, const void **credential_values_p);

/**
* Returns json representation of credential values.
*
* # Arguments
* * `credential_values` - Reference that contains credential values instance pointer.
* * `credential_values_json_p` - Reference that will contain credential values json.
*/
struct ExternError ursa_cl_credential_values_to_json(const void *credential_values, const char **credential_values_json_p);

/**
* Deallocates credential values instance.
*
Expand Down
27 changes: 27 additions & 0 deletions pkg/libursa/ursa/value_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ func NewValueBuilder() (*CredentialValuesBuilder, error) {
return &CredentialValuesBuilder{builder}, nil
}

// CredentialValuesFromJSON creates and returns credential values from JSON
func CredentialValuesFromJSON(jsn []byte) (*CredentialValues, error) {
var ptr unsafe.Pointer
cjson := C.CString(string(jsn))
defer C.free(unsafe.Pointer(cjson))

result := C.ursa_cl_credential_values_from_json(cjson, &ptr)
if result.code != 0 {
return nil, ursaError(C.GoString(result.message))
}

return &CredentialValues{ptr}, nil
}

// AddDecHidden adds new hidden attribute dec_value to credential values map
func (r *CredentialValuesBuilder) AddDecHidden(attr, decValue string) error {
cattr := C.CString(attr)
Expand Down Expand Up @@ -100,6 +114,19 @@ func (r *CredentialValues) Free() error {
return nil
}

// ToJSON returns JSON representation of credential values
func (r *CredentialValues) ToJSON() ([]byte, error) {
var d *C.char
defer C.free(unsafe.Pointer(d))

result := C.ursa_cl_credential_values_to_json(r.ptr, &d)
if result.code != 0 {
return nil, ursaError(C.GoString(result.message))
}

return []byte(C.GoString(d)), nil
}

// EncodeValue encodes any value into decimal representation
func EncodeValue(val interface{}) (string, string) {
var raw, enc string
Expand Down
16 changes: 16 additions & 0 deletions pkg/libursa/ursa/value_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ func TestNewValueBuilder(t *testing.T) {
builder, err := NewValueBuilder()
assert.Empty(t, err)
assert.NotEmpty(t, builder)

err = builder.AddDecHidden("master_secret", "122345")
assert.NoError(t, err)

vals, err := builder.Finalize()
assert.NoError(t, err)

str, err := vals.ToJSON()
assert.NoError(t, err)

err = vals.Free()
assert.NoError(t, err)

newVals, err := CredentialValuesFromJSON(str)
assert.NoError(t, err)
assert.NotNil(t, newVals)
})
}

Expand Down

0 comments on commit 5407e2a

Please sign in to comment.