This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
utils.go
135 lines (108 loc) · 3.29 KB
/
utils.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
package main
import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/codegangsta/cli"
"github.com/fatih/color"
"os"
"strings"
"text/template"
)
type curlData struct {
Headers map[string]string
Body string
URL string
Method string
}
func (cdata *curlData) render(c *cli.Context) error {
credentials := getClientCredentials(c)
tpl := fmt.Sprintf("❯❯❯ curl -v -X{{$.Method}} -u \"%s:%s\" {{range $key, $value := $.Headers}}-H \"{{$key}}: {{$value}}\" {{end}}{{$.Body}} {{$.URL}}", credentials[0], credentials[1])
var curl bytes.Buffer
t := template.New("curl")
t.Parse(tpl)
err := t.Execute(&curl, cdata)
if err != nil {
color.Red("Error: %s", err)
return err
}
boldYellow := color.New(color.FgYellow).Add(color.Bold).Add(color.Underline)
boldYellow.Printf("\n★★★ cURL command to replay request ★★★\n\n")
color.Yellow("%s", curl.String())
return nil
}
func renderResults(obj interface{}) error {
boldMagenta := color.New(color.FgMagenta).Add(color.Bold).Add(color.Underline)
boldMagenta.Printf("★★★ Results ★★★\n\n")
pretty, err := prettyJSON(obj)
if err != nil {
color.Red("%s: %s\n", pretty, err)
} else {
color.Magenta("%s\n", pretty)
}
return err
}
func getUserIdentifier(c *cli.Context) string {
userid := c.GlobalString("user-id")
if userid == "" {
userid = createUserIdentifier()
}
return userid
}
func createUserIdentifier() string {
dictionary := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=-_."
bytes := make([]byte, 64)
rand.Read(bytes)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}
func disableColors(c *cli.Context) {
if c.GlobalBool("no-color") {
color.NoColor = true
}
}
func prettyJSON(obj interface{}) ([]byte, error) {
result, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return []byte("Failed to prettify JSON object"), err
}
return result, nil
}
func xorBytes(b1, b2 []byte) []byte {
if len(b1) != len(b2) {
panic("length mismatch")
}
rv := make([]byte, len(b1))
for i := range b1 {
rv[i] = b1[i] ^ b2[i]
}
return rv
}
func getClientCredentials(c *cli.Context) []string {
credentials := []string{c.GlobalString("client-id"), c.GlobalString("client-secret")}
if credentials[0] == "" || credentials[1] == "" {
color.Yellow("No client credentials given. Fallback to builtin default...")
color.Yellow("Keep in mind that your document might be visible to other users.")
color.Yellow("Your unique user-id is the only secret to protect your data.\n\n")
superSecretSecret := []byte("V;4nJvuANmoywKNYk.yewNhqwmAQctc3BvByxeozQVpiK")
// Decode HEX default credentials
credentialsBytes, err := hex.DecodeString(defaultClientCredentials)
if err != nil {
color.Red("Error: client-id and client-secret missing and fallback decoding (step 1) failed: %s\n\n", err)
cli.ShowCommandHelp(c, c.Command.FullName())
os.Exit(1)
}
decodedCredentials := strings.Split(string(xorBytes(credentialsBytes, superSecretSecret)), ":")
if len(decodedCredentials) < 2 {
color.Red("Error: client-id and client-secret missing and fallback decoding (step 2) failed: %s\n\n", err)
cli.ShowCommandHelp(c, c.Command.FullName())
os.Exit(1)
}
credentials = decodedCredentials
}
return credentials
}