-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
304 lines (269 loc) · 7.31 KB
/
helpers.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
package aeio
import (
"errors"
"log"
"math"
"net/http"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"cloud.google.com/go/datastore"
)
// AncestorKindKey returns the key of the nearest specified kind ancestor for any given key.
// Only if there is no ancestors, it checks if itself is the kind and return itself.
// If there is no element of the kind, returns nil.
func AncestorKindKey(k *datastore.Key, kind string) *datastore.Key {
for {
if k.Parent != nil {
k = k.Parent
if k.Kind == kind {
return k
}
continue
}
if k.Kind == kind {
return k
} else {
return nil
}
}
}
// RootKey returns the deepest ancestor of the passed key.
// Very useful for checking Users path for example.
func RootKey(k *datastore.Key) (key *datastore.Key) {
for {
if k.Parent == nil {
return k
}
k = k.Parent
}
}
// NoZeroTime checks if time is zero, and if so, returns the actual time.
func NoZeroTime(t time.Time) time.Time {
if t.IsZero() {
return time.Now().UTC()
}
return t
}
// // String TODO: revision of all helpers.go functions, maybe should return errors also
// func String(v interface{}) (s string) {
// switch v := v.(type) {
// case float32:
// s = strconv.FormatFloat(float64(v), 'f', -1, 64)
// case float64:
// s = strconv.FormatFloat(v, 'f', -1, 64)
// case int:
// s = strconv.FormatInt(int64(v), 10)
// case int8:
// s = strconv.FormatInt(int64(v), 10)
// case int16:
// s = strconv.FormatInt(int64(v), 10)
// case int32:
// s = strconv.FormatInt(int64(v), 10)
// case int64:
// s = strconv.FormatInt(v, 10)
// case uint:
// s = strconv.FormatUint(uint64(v), 10)
// case uint8:
// s = strconv.FormatUint(uint64(v), 10)
// case uint16:
// s = strconv.FormatUint(uint64(v), 10)
// case uint32:
// s = strconv.FormatUint(uint64(v), 10)
// case uint64:
// s = strconv.FormatUint(v, 10)
// default:
// s = ""
// }
// return
// }
// func StringLatin(v interface{}) (s string) {
// switch v := v.(type) {
// case float32:
// s = strconv.FormatFloat(float64(v), 'f', 2, 64)
// case float64:
// s = strconv.FormatFloat(v, 'f', 2, 64)
// default:
// s = ""
// }
// return strings.Replace(s, ".", ",", 1)
// }
// func ParseFloat(v string) (f float64) {
// rex := regexp.MustCompile("[^0-9.]+")
// clean := rex.ReplaceAllString(v, "")
// f, err := strconv.ParseFloat(clean, 64)
// if err != nil {
// f = 0
// }
// return
// }
func parseInt(v string) (i int) {
rex := regexp.MustCompile("[^0-9.]+")
clean := rex.ReplaceAllString(v, "")
f, err := strconv.ParseFloat(clean, 64)
if err != nil {
f = 0
}
return int(f + math.Copysign(0, f))
}
// func Round(num float64) int {
// return int(num + math.Copysign(0.5, num))
// }
// func Fixed(num float64, precision int) float64 {
// output := math.Pow(10, float64(precision))
// return float64(Round(num*output)) / output
// }
// func Cents(num float64) int {
// n := Fixed(num, 2)
// return Round(n * 100)
// }
// func GenerateRandomBytes(n int) ([]byte, error) {
// b := make([]byte, n)
// _, err := rand.Get(b)
// if err != nil {
// return nil, err
// }
// return b, nil
// }
// func GenerateRandomString(n int) (string, error) {
// b, err := GenerateRandomBytes(n)
// return base64.URLEncoding.EncodeToString(b), err
// }
// const letterBytes = "0123456789"
// const letterIdxBits = 6 // 6 bits to represent a letter index
// const letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
// const letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
// func GenerateRandomNumber(n int) string {
// if n <= 0 {
// return ""
// }
//
// var src = mathRand.NewSource(time.Now().UnixNano())
//
// b := make([]byte, n)
// // A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
// for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
// if remain == 0 {
// cache, remain = src.Int63(), letterIdxMax
// }
// if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
// b[i] = letterBytes[idx]
// i--
// }
// cache >>= letterIdxBits
// remain--
// }
//
// return string(b)
// }
// func HashString(password string, salt string) string {
// b := pbkdf2.Key([]byte(password), []byte(salt), 4096, 512, sha512.New)
// return base64.URLEncoding.EncodeToString(b)
// }
// func RemoveUnsafe(s string) string {
// reg, _ := regexp.Compile("[^A-Za-z]+")
// return reg.ReplaceAllString(s, "")
// }
// NewResourceFromRequest initializes a base resource with information from the request.
func NewResourceFromRequest(writer *http.ResponseWriter, request *http.Request) (*Resource, error) {
r := &Resource{}
r.Access = newAccess(writer, request)
r.Key = Key(r.Access.Request.URL.Path)
if r.Key == nil {
return r, errorInvalidPath.withStack(10)
}
return r, nil
}
// InitResource uses a Key to initialize a specific resource beyond the root resource. The resource returned is ready to be actioned.
// Examples are returning sub resources or even something outside the scope of root.
// You can pass an incomplete Key to complete when putting the object on the datastore.
func InitResource(access *Access, key *datastore.Key) (r *Resource) {
return &Resource{Key: key, Access: access}
}
// NewResource is used to create empty children resources. Parent path may be nil (root).
// It returns a resource with an incompleteKey.
// It initializes an object of type kind.
func NewResource(access *Access, parentKey *datastore.Key, kind string) (*Resource, error) {
r := &Resource{Access: access}
if parentKey == nil {
parentKey = datastore.IncompleteKey("", nil)
}
err := ValidatePaternity(parentKey.Kind, kind)
if err != nil {
return nil, err
}
if parentKey.Kind == "" {
r.Key = Key("/" + kind)
} else {
r.Key = Key(Path(parentKey) + "/" + kind)
}
if r.Key == nil {
return nil, errorInvalidPath.withStack(10)
}
r.Data, err = NewObject(kind)
if err != nil {
return nil, err
}
return r, nil
}
// Key transforms a path in a datastore key using an access.
func Key(path string) (k *datastore.Key) {
var kd string
var id int64
var p []string
if path == "" {
log.Print("path to key from empty path")
return nil
}
if validPath.MatchString(path) != true {
_, file, line, _ := runtime.Caller(2)
log.Println("invalid_regex_path", path, file, line)
return nil
}
p = strings.SplitN(path, "/", 2)
p = strings.Split(p[1], "/")
for i := 0; i < len(p); i = i + 2 {
kd = p[i]
if i < len(p)-1 {
id, _ = strconv.ParseInt(p[i+1], 10, 64)
k = datastore.IDKey(kd, id, k)
} else {
k = datastore.IncompleteKey(kd, k)
}
}
return k
}
// Path transforms a datastore Key into a Path
func Path(k *datastore.Key) (p string) {
if k.Incomplete() == false {
p = "/" + strconv.FormatInt(k.ID, 10)
}
p = "/" + k.Kind + p
for {
k = k.Parent
if k == nil {
break
}
p = "/" + k.Kind + "/" + strconv.FormatInt(k.ID, 10) + p
}
return p
}
func CheckAdminToken(request *http.Request) error {
jwtToken := request.Header.Get("Authorization")
token, err := FireAppAuthClient.VerifyIDToken(request.Context(), jwtToken)
if err != nil {
log.Println("error verifying token")
return err
}
_, ok := token.Claims["userId"]
if !ok {
return errors.New("token_not_linked")
}
userAdminClaim, ok := token.Claims["role"]
if !ok || userAdminClaim != "admin" {
return errors.New("token_not_admin")
}
return nil
}