2020-04-10 08:27:43 +02:00
|
|
|
package core
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
"github.com/dosco/super-graph/core/internal/crypto"
|
|
|
|
"github.com/dosco/super-graph/core/internal/qcode"
|
2020-04-18 23:42:17 +02:00
|
|
|
"github.com/dosco/super-graph/jsn"
|
2020-02-10 07:45:37 +01:00
|
|
|
)
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
func (sg *SuperGraph) encryptCursor(qc *qcode.QCode, data []byte) ([]byte, error) {
|
2020-02-10 07:45:37 +01:00
|
|
|
var keys [][]byte
|
|
|
|
|
|
|
|
for _, s := range qc.Selects {
|
|
|
|
if s.Paging.Type != qcode.PtOffset {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
buf.WriteString(s.FieldName)
|
|
|
|
buf.WriteString("_cursor")
|
|
|
|
keys = append(keys, buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
from := jsn.Get(data, keys)
|
|
|
|
to := make([]jsn.Field, len(from))
|
|
|
|
|
|
|
|
for i, f := range from {
|
|
|
|
to[i].Key = f.Key
|
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
if f.Value[0] != '"' || f.Value[len(f.Value)-1] != '"' {
|
2020-02-10 07:45:37 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2020-02-22 21:58:37 +01:00
|
|
|
|
|
|
|
if len(f.Value) > 2 {
|
2020-04-10 08:27:43 +02:00
|
|
|
v, err := crypto.Encrypt(f.Value[1:len(f.Value)-1], &sg.encKey)
|
2020-02-22 21:58:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteByte('"')
|
|
|
|
buf.WriteString(base64.StdEncoding.EncodeToString(v))
|
|
|
|
buf.WriteByte('"')
|
|
|
|
} else {
|
|
|
|
buf.WriteString(`null`)
|
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
to[i].Value = buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
if err := jsn.Replace(&buf, data, from, to); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
func (sg *SuperGraph) decrypt(data string) ([]byte, error) {
|
2020-02-10 07:45:37 +01:00
|
|
|
v, err := base64.StdEncoding.DecodeString(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-10 08:27:43 +02:00
|
|
|
return crypto.Decrypt(v, &sg.encKey)
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|