/
form.go
261 lines (219 loc) · 6.19 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
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
package form
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/asaskevich/govalidator"
"github.com/cheikhshift/gos/core"
"gopkg.in/mgo.v2/bson"
"io"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"time"
)
// Handle file upload. Use func Path to get a
// string path relative to your filesystem.
type File string
// Display textarea
type Paragraph string
// Display Date input. Return string of Date.
// Format YYYY-M-d
type Date string
// Display dropdown.
// Tag properties :
// title : title of field.
// placeholder : Prompt left of field.
// select : comma delimited choices of field.
type Select string
// Display dropdown list with multiple selection support.
// Tag properties :
// title : title of field.
// placeholder : Prompt left of field.
// select : comma delimited choices of field.
type SelectMult Select
// Display radio input.
// Tag properties :
// title : title of field.
// select : comma delimited choices of field.
type Radio Select
// Display Email input.
type Email string
// Display Passworld input
type Password []byte
//select opts,placeholder,title
const (
MB = 1 << 20
)
var (
// string : Default input classes of
// generared HTML input class attribute.
InputClass string
// string : Default submit button attribute class of
// generated HTML forms.
ButtonClass string
// string : Secret form key
FormKey = "a very very very very secret key"
// int : Maximum upload size in Mb
MaxSize = 10 //Mb
)
func GetSel(tagstr string) string {
strs := strings.Split(tagstr, "\",")
for _, v := range strs {
if strings.Contains(v, "select") {
splm := strings.Split(v, ":")
return strings.Replace(splm[1], "\"", "", -1)
}
}
return ""
}
func GetPl(tagstr string) string {
strs := strings.Split(tagstr, "\",")
for _, v := range strs {
if strings.Contains(v, "placeholder") {
splm := strings.Split(v, ":")
return strings.Replace(splm[1], "\"", "", -1)
}
}
return ""
}
func Hash(input string) []byte {
return sha512.New512_256().Sum([]byte(input))
}
func Path(pathtoformat File) string {
return fmt.Sprintf("./uploads/%s", pathtoformat)
}
// Parse form from request and set data to specified
// pointer.
func Form(r *http.Request, i interface{}) error {
//convert form and validate
var err error
v := reflect.ValueOf(i).Elem()
//t := reflect.TypeOf(item)
if strings.Contains(r.Header.Get("content-type"), "/json") {
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(i)
if err != nil {
return err
}
_, err = govalidator.ValidateStruct(i)
if err != nil {
return err
}
} else {
bso := bson.M{"__elem": "form"}
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
fieldtype := strings.ToLower(field.Type.String())
if strings.Contains(fieldtype, "file") {
file, handler, _ := r.FormFile(field.Name)
if handler != nil {
fid := fmt.Sprintf("%s-%s", core.NewLen(15), handler.Filename)
f, err := os.OpenFile(fmt.Sprintf("./uploads/%s", fid), os.O_WRONLY|os.O_CREATE, 0700)
if err != nil {
return err
}
io.Copy(f, file)
f.Close()
bso[field.Name] = fid
file.Close()
}
} else if strings.Contains(fieldtype, "bool") {
bso[field.Name] = strings.Contains(r.FormValue(field.Name), "on")
} else if strings.Contains(fieldtype, "password") {
bso[field.Name] = Hash(r.FormValue(field.Name))
} else if strings.Contains(fieldtype, "int") {
i, _ := strconv.Atoi(r.FormValue(field.Name))
bso[field.Name] = i
} else if strings.Contains(fieldtype, "float") {
f, _ := strconv.ParseFloat(r.FormValue(field.Name), 64)
bso[field.Name] = f
} else {
bso[field.Name] = r.FormValue(field.Name)
}
}
data, _ := json.Marshal(&bso)
json.Unmarshal([]byte(data), i)
_, err = govalidator.ValidateStruct(i)
if err != nil {
return err
}
}
return nil
//ValidateStruct
}
func SetKey(key string) {
FormKey = key
}
func ToBson(data string) bson.M {
res := bson.M{}
json.Unmarshal([]byte(data), &res)
return res
}
// Returns bool : Verifies if request is valid
func ValidateRequest(r *http.Request, usertoken string) bool {
var token string
if strings.Contains(r.Header.Get("content-type"), "/json") {
decoder := json.NewDecoder(r.Body)
var i bson.M
err := decoder.Decode(i)
if err != nil {
return false
}
token = Decrypt([]byte(FormKey), i["formtoken"].(string))
} else {
token = Decrypt([]byte(FormKey), r.FormValue("formtoken"))
}
return strings.Contains(token, usertoken) && strings.Contains(token, r.URL.Path)
}
// Returns form token.
// Each token is specific to the URL specified.
func GenerateToken(url string, usertoken string) string {
return Encrypt([]byte(FormKey), fmt.Sprintf("%s@%s@%s", url, time.Now().String(), usertoken))
}
func Encrypt(key []byte, text string) string {
// key := []byte(keyText)
plaintext := []byte(text)
block, err := aes.NewCipher(key)
if err != nil {
return err.Error()
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return err.Error()
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
// convert to base64
return base64.URLEncoding.EncodeToString(ciphertext)
}
//func GetFile
//func DeleteFile add support to html file filters : file:"mimeType"
// decrypt from base64 to decrypted string
func Decrypt(key []byte, cryptoText string) string {
ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)
block, err := aes.NewCipher(key)
if err != nil {
return err.Error()
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
if len(ciphertext) < aes.BlockSize {
return "ciphertext too short"
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
// XORKeyStream can work in-place if the two arguments are the same.
stream.XORKeyStream(ciphertext, ciphertext)
return fmt.Sprintf("%s", ciphertext)
}