/
jsonpointer.go
197 lines (169 loc) · 4.94 KB
/
jsonpointer.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package authenticationflow
import (
"errors"
"fmt"
"strconv"
"github.com/iawaknahc/jsonschema/pkg/jsonpointer"
"github.com/authgear/authgear-server/pkg/lib/config"
)
var ErrInvalidJSONPointer = errors.New("invalid json pointer")
var ErrNoEntries = errors.New("no entries")
const (
JsonPointerTokenOneOf string = "one_of"
JsonPointerTokenSteps string = "steps"
)
type TraverseEntry struct {
FlowObject config.AuthenticationFlowObject
JSONPointer jsonpointer.T
Name string
FieldName string
Index int
}
func FlowObjectGetName(o config.AuthenticationFlowObject) string {
if root, ok := o.(config.AuthenticationFlowObjectFlowRoot); ok {
return root.GetName()
}
if step, ok := o.(config.AuthenticationFlowObjectFlowStep); ok {
return step.GetName()
}
return ""
}
func FlowObjectGetSteps(o config.AuthenticationFlowObject) ([]config.AuthenticationFlowObject, bool) {
if root, ok := o.(config.AuthenticationFlowObjectFlowRoot); ok {
return root.GetSteps(), true
}
if branch, ok := o.(config.AuthenticationFlowObjectFlowBranch); ok {
return branch.GetSteps(), true
}
return nil, false
}
func FlowObjectGetOneOf(o config.AuthenticationFlowObject) ([]config.AuthenticationFlowObject, bool) {
if step, ok := o.(config.AuthenticationFlowObjectFlowStep); ok {
return step.GetOneOf(), true
}
return nil, false
}
func Traverse(o config.AuthenticationFlowObject, pointer jsonpointer.T) ([]TraverseEntry, error) {
entries := []TraverseEntry{
{
FlowObject: o,
Name: FlowObjectGetName(o),
},
}
var traversedPointer jsonpointer.T
for {
switch {
case len(pointer) == 0:
return entries, nil
case len(pointer)%2 == 1:
return nil, ErrInvalidJSONPointer
default:
fieldName := pointer[0]
indexStr := pointer[1]
index, err := strconv.Atoi(indexStr)
if err != nil {
return nil, fmt.Errorf("%v is not an index", indexStr)
}
traversedPointer = traversedPointer.AddReferenceToken(fieldName)
traversedPointer = traversedPointer.AddReferenceToken(indexStr)
pointer = pointer[2:]
var objects []config.AuthenticationFlowObject
var ok bool
switch fieldName {
case "steps":
objects, ok = FlowObjectGetSteps(o)
case "one_of":
objects, ok = FlowObjectGetOneOf(o)
default:
break
}
if !ok {
return nil, ErrInvalidJSONPointer
}
if index >= len(objects) {
return nil, fmt.Errorf("index out of bound: %v >= %v", index, len(objects))
}
o = objects[index]
entries = append(entries, TraverseEntry{
FlowObject: o,
JSONPointer: traversedPointer,
Name: FlowObjectGetName(o),
FieldName: fieldName,
Index: index,
})
}
}
}
func GetCurrentObject(entries []TraverseEntry) (config.AuthenticationFlowObject, error) {
if len(entries) <= 0 {
return nil, ErrNoEntries
}
return entries[len(entries)-1].FlowObject, nil
}
func JSONPointerForStep(p jsonpointer.T, index int) jsonpointer.T {
return p.AddReferenceToken(JsonPointerTokenSteps).AddReferenceToken(strconv.Itoa(index))
}
func JSONPointerForOneOf(p jsonpointer.T, index int) jsonpointer.T {
return p.AddReferenceToken(JsonPointerTokenOneOf).AddReferenceToken(strconv.Itoa(index))
}
func JSONPointerToParent(p jsonpointer.T) jsonpointer.T {
if len(p) < 2 {
panic(ErrInvalidJSONPointer)
}
return p[:len(p)-2]
}
func JSONPointerSubtract(p1 jsonpointer.T, p2 jsonpointer.T) jsonpointer.T {
result := jsonpointer.T{}
isDifferent := false
for idx, el := range p1 {
// Compare the element at same position, until found a different
if (len(p2)-1) < idx || el != p2[idx] {
isDifferent = true
}
if isDifferent {
result = append(result, el)
}
}
return result
}
func FlowObject(flowRootObject config.AuthenticationFlowObject, pointer jsonpointer.T) (config.AuthenticationFlowObject, error) {
entries, err := Traverse(flowRootObject, pointer)
if err != nil {
return nil, err
}
current, err := GetCurrentObject(entries)
if err != nil {
return nil, err
}
return current, nil
}
func GetFlowAction(flowRootObject config.AuthenticationFlowObject, pointer jsonpointer.T) *FlowAction {
flowObject, err := FlowObject(flowRootObject, pointer)
if err != nil {
panic(err)
}
switch o := flowObject.(type) {
case config.AuthenticationFlowObjectFlowRoot:
return nil
case config.AuthenticationFlowObjectFlowStep:
return &FlowAction{
Type: FlowActionTypeFromStepType(o.GetType()),
}
case config.AuthenticationFlowObjectFlowBranch:
branchInfo := o.GetBranchInfo()
step := &FlowAction{
Identification: branchInfo.Identification,
Authentication: branchInfo.Authentication,
}
stepPointer := JSONPointerToParent(pointer)
stepFlowObjectBeforeCast, err := FlowObject(flowRootObject, stepPointer)
if err != nil {
panic(err)
}
stepFlowObject := stepFlowObjectBeforeCast.(config.AuthenticationFlowObjectFlowStep)
step.Type = FlowActionTypeFromStepType(stepFlowObject.GetType())
return step
default:
panic(fmt.Errorf("unexpected flow object: %T", flowObject))
}
}