Skip to content

Commit

Permalink
update: service value components to use hex encoding instead base64
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrynenko committed Dec 27, 2024
1 parent ee9368a commit 55f1b53
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/spec/components/parameters/DataValueParam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ in: query
name: filter[value]
required: false
description: |
Param to filter data by Base64 encoded value
example: aGVsbG8gd29ybGQ=
Param to filter data by Hex encoded value without 0x prefix
example: 68656c6c6f20776f726c64
schema:
type: string
4 changes: 2 additions & 2 deletions docs/spec/components/schemas/AddValue.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ allOf:
properties:
value:
type: string
description: Base64 encoded data value
example: aGVsbG8gd29ybGQ=
description: Hex encoded data value without 0x prefix
example: 68656c6c6f20776f726c64
4 changes: 2 additions & 2 deletions docs/spec/components/schemas/Value.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ allOf:
example: 550e8400-e29b-41d4-a716-446655440000
value:
type: string
description: Base64 encoded data value
example: aGVsbG8gd29ybGQ=
description: Hex encoded data value without 0x prefix
example: 68656c6c6f20776f726c64
6 changes: 2 additions & 4 deletions docs/spec/paths/integrations@[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ delete:
summary: Delete value
description: |
Delete value from storage. If no filter query specified nothing is delete, because one of filters is required.
When filtering by value, service queries by exact match and if any value found deletes it.
Also take into account that value is Base64 encoded, so may contain some escape symbol that should be encoded.
When filtering by value, service queries by exact match and if any value found deletes it.
operationId: GetData
parameters:
- $ref: '#/components/parameters/DataKeyParam'
Expand Down Expand Up @@ -71,8 +70,7 @@ get:
description: |
Get value from storage. If no filter query specified any row is returned.
When filtering by value, service queries not by exact match, but searching for the nearest by
hammming distance element. Also take into account that value is Base64 encoded, so may contain
some escape symbol that should be encoded.
hammming distance element.
operationId: GetData
parameters:
- $ref: '#/components/parameters/DataKeyParam'
Expand Down
2 changes: 1 addition & 1 deletion internal/data/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type KVQ interface {

FilterByKey(key string) KVQ
FilterByValue(value []byte) KVQ
FilterByBase64ValueLength(value string) KVQ
FilterByHexValueLength(value string) KVQ

OrderBy(expr sq.Sqlizer, order OrderType) KVQ
}
Expand Down
8 changes: 4 additions & 4 deletions internal/data/pg/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (q kvQ) FilterByValue(value []byte) data.KVQ {
return q.withFilters(sq.Eq{"value": value})
}

func (q kvQ) FilterByBase64ValueLength(value string) data.KVQ {
return q.withFilters(sq.Expr("LENGTH(value) = LENGTH(DECODE(?, 'base64'))", value))
func (q kvQ) FilterByHexValueLength(value string) data.KVQ {
return q.withFilters(sq.Expr("LENGTH(value) = LENGTH(DECODE(?, 'hex'))", value))
}

func (q kvQ) OrderBy(expr sq.Sqlizer, order data.OrderType) data.KVQ {
Expand All @@ -77,6 +77,6 @@ func (q kvQ) withFilters(stmt interface{}) data.KVQ {
return q
}

func HammingDistanceBase64(value string) sq.Sqlizer {
return sq.Expr("hamming_distance(value, DECODE(?, 'base64'))", value)
func HammingDistanceHex(value string) sq.Sqlizer {
return sq.Expr("hamming_distance(value, DECODE(?, 'hex'))", value)
}
8 changes: 4 additions & 4 deletions internal/service/handlers/add_data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package handlers

import (
"encoding/base64"
"encoding/hex"
"net/http"

"github.com/google/uuid"
Expand All @@ -20,9 +20,9 @@ func AddData(w http.ResponseWriter, r *http.Request) {
return
}

value, err := base64.StdEncoding.DecodeString(req.Data.Attributes.Value)
value, err := hex.DecodeString(req.Data.Attributes.Value)
if err != nil {
Log(r).WithError(err).WithField("value", req.Data.Attributes.Value).Error("failed to decode base64 string")
Log(r).WithError(err).WithField("value", req.Data.Attributes.Value).Error("failed to decode hex string")
ape.RenderErr(w, problems.InternalError())
return
}
Expand Down Expand Up @@ -50,7 +50,7 @@ func newKVResponse(kv data.KV) resources.ValueResponse {
Type: resources.VALUE,
},
Attributes: resources.ValueAttributes{
Value: base64.StdEncoding.EncodeToString(kv.Value),
Value: hex.EncodeToString(kv.Value),
Key: kv.Key,
},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/service/handlers/delete_data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package handlers

import (
"encoding/base64"
"encoding/hex"
"net/http"

"github.com/rarimo/zk-biometrics-svc/internal/service/requests"
Expand All @@ -24,9 +24,9 @@ func DeleteData(w http.ResponseWriter, r *http.Request) {
}

if req.Value != nil {
value, err := base64.StdEncoding.DecodeString(*req.Value)
value, err := hex.DecodeString(*req.Value)
if err != nil {
Log(r).WithError(err).WithField("value", *req.Value).Error("failed to decode Base64 string")
Log(r).WithError(err).WithField("value", *req.Value).Error("failed to decode hex string")
ape.RenderErr(w, problems.InternalError())
return
}
Expand Down
4 changes: 2 additions & 2 deletions internal/service/handlers/get_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func GetData(w http.ResponseWriter, r *http.Request) {

if req.Value != nil {
kvQuery = kvQuery.
FilterByBase64ValueLength(*req.Value).
OrderBy(pg.HammingDistanceBase64(*req.Value), data.OrderDesc)
FilterByHexValueLength(*req.Value).
OrderBy(pg.HammingDistanceHex(*req.Value), data.OrderDesc)
}

kv, err := kvQuery.Get()
Expand Down
2 changes: 1 addition & 1 deletion internal/service/requests/add_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func AddData(r *http.Request) (req resources.AddValueRequest, err error) {

return req, val.Errors{
"data/type": val.Validate(req.Data.Type, val.Required, val.In(resources.VALUE)),
"data/attributes/value": val.Validate(req.Data.Attributes.Value, val.Required, is.Base64),
"data/attributes/value": val.Validate(req.Data.Attributes.Value, val.Required, is.Hexadecimal),
}.Filter()
}
2 changes: 1 addition & 1 deletion internal/service/requests/delete_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewDeleteDataRequest(r *http.Request) (req GetDataRequest, err error) {
val.When(val.IsEmpty(req.Value), validation.Required.Error(FilterQueryErrMsg)),
),
"value": val.Validate(req.Value,
val.When(!val.IsEmpty(req.Value), is.Base64),
val.When(!val.IsEmpty(req.Value), is.Hexadecimal),
val.When(val.IsEmpty(req.Key), validation.Required.Error(FilterQueryErrMsg)),
),
}.Filter()
Expand Down
2 changes: 1 addition & 1 deletion internal/service/requests/get_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewGetDataRequest(r *http.Request) (req GetDataRequest, err error) {

err = val.Errors{
"key": val.Validate(req.Key, val.When(!val.IsEmpty(req.Key), is.UUID)),
"value": val.Validate(req.Value, val.When(!val.IsEmpty(req.Value), is.Base64)),
"value": val.Validate(req.Value, val.When(!val.IsEmpty(req.Value), is.Hexadecimal)),
}.Filter()
return
}
2 changes: 1 addition & 1 deletion resources/model_add_value_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
package resources

type AddValueAttributes struct {
// Base64 encoded data value
// Hex encoded data value without 0x prefix
Value string `json:"value"`
}
2 changes: 1 addition & 1 deletion resources/model_value_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ package resources
type ValueAttributes struct {
// Unique identifier for stored data
Key string `json:"key"`
// Base64 encoded data value
// Hex encoded data value without 0x prefix
Value string `json:"value"`
}

0 comments on commit 55f1b53

Please sign in to comment.