/
form.go
141 lines (122 loc) · 3.08 KB
/
form.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package client
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"time"
oldCloud "github.com/ory/client-go/114"
"github.com/pkg/errors"
"github.com/tidwall/sjson"
"github.com/ory/x/cmdx"
)
func getLabel(attrs *oldCloud.UiNodeInputAttributes, node *oldCloud.UiNode) string {
if attrs.Name == "identifier" {
return fmt.Sprintf("%s: ", "Email")
} else if node.Meta.Label != nil {
return fmt.Sprintf("%s: ", node.Meta.Label.Text)
} else if attrs.Label != nil {
return fmt.Sprintf("%s: ", attrs.Label.Text)
}
return fmt.Sprintf("%s: ", attrs.Name)
}
type passwordReader = func() ([]byte, error)
func renderForm(stdin *bufio.Reader, pwReader passwordReader, stderr io.Writer, ui oldCloud.UiContainer, method string, out interface{}) (err error) {
for _, message := range ui.Messages {
_, _ = fmt.Fprintf(stderr, "%s\n", message.Text)
}
for _, node := range ui.Nodes {
for _, message := range node.Messages {
_, _ = fmt.Fprintf(stderr, "%s\n", message.Text)
}
}
values := json.RawMessage(`{}`)
for k := range ui.Nodes {
node := ui.Nodes[k]
if node.Group != method && node.Group != "default" {
continue
}
switch node.Type {
case "input":
attrs := node.Attributes.UiNodeInputAttributes
switch attrs.Type {
case "button":
continue
case "submit":
continue
}
if attrs.Name == "traits.consent.tos" {
for {
ok, err := cmdx.AskScannerForConfirmation(getLabel(attrs, &node), stdin, stderr)
if err != nil {
return err
}
if ok {
break
}
}
values, err = sjson.SetBytes(values, attrs.Name, time.Now().UTC().Format(time.RFC3339))
if err != nil {
return err
}
continue
}
if strings.Contains(attrs.Name, "traits.details") {
continue
}
switch attrs.Type {
case "hidden":
continue
case "checkbox":
result, err := cmdx.AskScannerForConfirmation(getLabel(attrs, &node), stdin, stderr)
if err != nil {
return err
}
values, err = sjson.SetBytes(values, attrs.Name, result)
if err != nil {
return err
}
case "password":
var password string
for password == "" {
_, _ = fmt.Fprint(stderr, getLabel(attrs, &node))
v, err := pwReader()
if err != nil {
return err
}
password = strings.ReplaceAll(string(v), "\n", "")
fmt.Println("")
}
values, err = sjson.SetBytes(values, attrs.Name, password)
if err != nil {
return err
}
default:
var value string
for value == "" {
_, _ = fmt.Fprint(stderr, getLabel(attrs, &node))
v, err := stdin.ReadString('\n')
if err != nil {
return errors.Wrap(err, "failed to read from stdin")
}
value = strings.ReplaceAll(v, "\n", "")
}
values, err = sjson.SetBytes(values, attrs.Name, value)
if err != nil {
return err
}
}
default:
// Do nothing
}
}
values, err = sjson.SetBytes(values, "method", method)
if err != nil {
return err
}
return errors.WithStack(json.NewDecoder(bytes.NewBuffer(values)).Decode(out))
}