-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
cursor.go
80 lines (65 loc) · 1.59 KB
/
cursor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package core
import (
"bytes"
"encoding/base64"
"github.com/dosco/graphjin/core/internal/crypto"
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/dosco/graphjin/internal/jsn"
)
type cursors struct {
data []byte
value string
}
func (gj *GraphJin) encryptCursor(qc *qcode.QCode, data []byte) (cursors, error) {
var keys [][]byte
cur := cursors{data: data}
for _, sel := range qc.Selects {
if sel.Paging.Cursor {
keys = append(keys, []byte((sel.FieldName + "_cursor")))
}
}
if len(keys) == 0 {
return cur, nil
}
from := jsn.Get(data, keys)
to := make([]jsn.Field, len(from))
for i, f := range from {
to[i].Key = f.Key
if f.Value[0] != '"' || f.Value[len(f.Value)-1] != '"' {
continue
}
if len(f.Value) > 2 {
val := f.Value[1 : len(f.Value)-1]
// save a copy of the first cursor value to use
// with subscriptions when fetching the next set
if cur.value == "" {
cur.value = string(val)
}
v, err := crypto.Encrypt(val, &gj.encKey)
if err != nil {
return cur, err
}
var b bytes.Buffer
b.Grow(base64.StdEncoding.EncodedLen(len(v)) + 2)
b.WriteByte('"')
b.WriteString(base64.StdEncoding.EncodeToString(v))
b.WriteByte('"')
to[i].Value = b.Bytes()
} else {
to[i].Value = []byte(`null`)
}
}
var b bytes.Buffer
if err := jsn.Replace(&b, data, from, to); err != nil {
return cur, err
}
cur.data = b.Bytes()
return cur, nil
}
func (gj *GraphJin) decrypt(data string) ([]byte, error) {
v, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, err
}
return crypto.Decrypt(v, &gj.encKey)
}