/
node.go
428 lines (363 loc) · 8.66 KB
/
node.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package node
import (
"bytes"
"context"
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/text"
"github.com/ory/x/stringslice"
)
// swagger:enum UiNodeType
type UiNodeType string
const (
Text UiNodeType = "text"
Input UiNodeType = "input"
Image UiNodeType = "img"
Anchor UiNodeType = "a"
Script UiNodeType = "script"
)
func (t UiNodeType) String() string {
return string(t)
}
// swagger:enum UiNodeGroup
type UiNodeGroup string
const (
DefaultGroup UiNodeGroup = "default"
PasswordGroup UiNodeGroup = "password"
OpenIDConnectGroup UiNodeGroup = "oidc"
ProfileGroup UiNodeGroup = "profile"
LinkGroup UiNodeGroup = "link"
TOTPGroup UiNodeGroup = "totp"
LookupGroup UiNodeGroup = "lookup_secret"
WebAuthnGroup UiNodeGroup = "webauthn"
)
func (g UiNodeGroup) String() string {
return string(g)
}
// swagger:model uiNodes
type Nodes []*Node
// Node represents a flow's nodes
//
// Nodes are represented as HTML elements or their native UI equivalents. For example,
// a node can be an `<img>` tag, or an `<input element>` but also `some plain text`.
//
// swagger:model uiNode
type Node struct {
// The node's type
//
// required: true
Type UiNodeType `json:"type" faker:"-"`
// Group specifies which group (e.g. password authenticator) this node belongs to.
//
// required: true
Group UiNodeGroup `json:"group"`
// The node's attributes.
//
// required: true
// swagger:type uiNodeAttributes
Attributes Attributes `json:"attributes" faker:"ui_node_attributes"`
// The node's messages
//
// Contains error, validation, or other messages relevant to this node.
//
// required: true
Messages text.Messages `json:"messages"`
// Meta contains a node meta information
//
// This might include a label and other information that can optionally
// be used to render UIs.
//
// required: true
Meta *Meta `json:"meta"`
}
// A Node's Meta Information
//
// This might include a label and other information that can optionally
// be used to render UIs.
//
// swagger:model uiNodeMeta
type Meta struct {
// Label represents the node's label.
//
// Keep in mind that these values are autogenerated and can not be changed.
// If you wish to use other titles or labels implement that directly in
// your UI.
Label *text.Message `json:"label,omitempty"`
}
// Used for en/decoding the Attributes field.
type jsonRawNode struct {
Type UiNodeType `json:"type"`
Group UiNodeGroup `json:"group"`
Attributes Attributes `json:"attributes"`
Messages text.Messages `json:"messages"`
Meta *Meta `json:"meta"`
}
func (n *Node) ID() string {
return n.Attributes.ID()
}
func (n *Node) Reset() {
n.Messages = nil
n.Attributes.Reset()
}
func (n *Node) WithMetaLabel(label *text.Message) *Node {
if n.Meta == nil {
n.Meta = new(Meta)
}
n.Meta.Label = label
return n
}
func (n *Node) GetValue() interface{} {
return n.Attributes.GetValue()
}
func (n Nodes) Find(id string) *Node {
for _, nn := range n {
if nn.ID() == id {
return nn
}
}
return nil
}
func (n Nodes) Reset(exclude ...string) {
for k, nn := range n {
nn.Messages = nil
if !stringslice.Has(exclude, nn.ID()) {
nn.Reset()
}
n[k] = nn
}
}
func (n Nodes) ResetNodes(reset ...string) {
for k, nn := range n {
if stringslice.Has(reset, nn.ID()) {
nn.Reset()
}
n[k] = nn
}
}
func (n Nodes) ResetNodesWithPrefix(prefix string) {
for k := range n {
if strings.HasPrefix(n[k].ID(), prefix) {
n[k].Reset()
}
}
}
func getStringSliceIndexOf(needle []string, haystack string) int {
for k := range needle {
if needle[k] == haystack {
return k
}
}
return -1
}
type sortOptions struct {
orderByGroups []string
schemaRef string
keysInOrder []string
keysInOrderAppend []string
keysInOrderPost func([]string) []string
}
type SortOption func(*sortOptions)
func SortByGroups(orderByGroups []UiNodeGroup) func(*sortOptions) {
return func(options *sortOptions) {
options.orderByGroups = make([]string, len(orderByGroups))
for k := range orderByGroups {
options.orderByGroups[k] = string(orderByGroups[k])
}
}
}
func SortBySchema(schemaRef string) func(*sortOptions) {
return func(options *sortOptions) {
options.schemaRef = schemaRef
}
}
func SortUseOrder(keysInOrder []string) func(*sortOptions) {
return func(options *sortOptions) {
options.keysInOrder = keysInOrder
}
}
func SortUseOrderAppend(keysInOrder []string) func(*sortOptions) {
return func(options *sortOptions) {
options.keysInOrderAppend = keysInOrder
}
}
func SortUpdateOrder(f func([]string) []string) func(*sortOptions) {
return func(options *sortOptions) {
options.keysInOrderPost = f
}
}
func (n Nodes) SortBySchema(ctx context.Context, opts ...SortOption) error {
var o sortOptions
for _, f := range opts {
f(&o)
}
if o.schemaRef != "" {
schemaKeys, err := schema.GetKeysInOrder(ctx, o.schemaRef)
if err != nil {
return err
}
o.keysInOrder = append(o.keysInOrder, schemaKeys...)
}
if o.keysInOrderPost != nil {
o.keysInOrder = o.keysInOrderPost(o.keysInOrder)
}
o.keysInOrder = append(o.keysInOrder, o.keysInOrderAppend...)
getKeyPosition := func(node *Node) int {
lastPrefix := len(o.keysInOrder)
// Method should always be the last element in the list
if node.Attributes.ID() == "method" {
return len(n) + len(o.keysInOrder) + 1
}
for i, n := range o.keysInOrder {
if strings.HasPrefix(node.ID(), n) {
return i
}
}
return lastPrefix
}
if len(o.orderByGroups) > 0 {
// Sort by groups so that default is in front, then oidc, password, ...
sort.Slice(n, func(i, j int) bool {
a := string(n[i].Group)
b := string(n[j].Group)
return getStringSliceIndexOf(o.orderByGroups, a) < getStringSliceIndexOf(o.orderByGroups, b)
})
}
sort.SliceStable(n, func(i, j int) bool {
a := n[i]
b := n[j]
if a.Group == b.Group {
pa, pb := getKeyPosition(a), getKeyPosition(b)
if pa < pb {
return true
} else if pa > pb {
return false
}
return fmt.Sprintf("%v", a.GetValue()) < fmt.Sprintf("%v", b.GetValue())
}
return false
})
return nil
}
// Remove removes one or more nodes by their IDs.
func (n *Nodes) Remove(ids ...string) {
if n == nil {
return
}
var r Nodes
for k, v := range *n {
var found bool
for _, needle := range ids {
if (*n)[k].ID() == needle {
found = true
break
}
}
if !found {
r = append(r, v)
}
}
*n = r
}
// Upsert updates or appends a node.
func (n *Nodes) Upsert(node *Node) {
if n == nil {
*n = append(*n, node)
return
}
for i := range *n {
if (*n)[i].ID() == node.ID() {
(*n)[i] = node
return
}
}
*n = append(*n, node)
}
// SetValueAttribute sets a node's attribute's value or returns false if no node is found.
func (n *Nodes) SetValueAttribute(id string, value interface{}) bool {
for i := range *n {
if (*n)[i].ID() == id {
(*n)[i].Attributes.SetValue(value)
return true
}
}
return false
}
// Append appends a node.
func (n *Nodes) Append(node *Node) {
*n = append(*n, node)
}
func (n *Node) UnmarshalJSON(data []byte) error {
var attr Attributes
switch t := gjson.GetBytes(data, "type").String(); UiNodeType(t) {
case Text:
attr = &TextAttributes{
NodeType: string(Text),
}
case Input:
attr = &InputAttributes{
NodeType: string(Input),
}
case Anchor:
attr = &AnchorAttributes{
NodeType: string(Anchor),
}
case Image:
attr = &ImageAttributes{
NodeType: string(Image),
}
case Script:
attr = &ScriptAttributes{
NodeType: string(Script),
}
default:
return fmt.Errorf("unexpected node type: %s", t)
}
var d jsonRawNode
d.Attributes = attr
if err := json.NewDecoder(bytes.NewReader(data)).Decode(&d); err != nil {
return err
}
*n = Node(d)
if n.Meta == nil {
n.Meta = new(Meta)
}
return nil
}
func (n *Node) MarshalJSON() ([]byte, error) {
var t UiNodeType
if n.Attributes != nil {
switch attr := n.Attributes.(type) {
case *TextAttributes:
t = Text
attr.NodeType = string(Text)
case *InputAttributes:
t = Input
attr.NodeType = string(Input)
case *AnchorAttributes:
t = Anchor
attr.NodeType = string(Anchor)
case *ImageAttributes:
t = Image
attr.NodeType = string(Image)
case *ScriptAttributes:
t = Script
attr.NodeType = string(Script)
default:
return nil, errors.WithStack(fmt.Errorf("unknown node type: %T", n.Attributes))
}
}
if n.Type == "" {
n.Type = t
} else if n.Type != t {
return nil, errors.WithStack(fmt.Errorf("node type and node attributes mismatch: %T != %s", n.Attributes, n.Type))
}
if n.Meta == nil {
n.Meta = new(Meta)
}
return json.Marshal((*jsonRawNode)(n))
}