forked from spruceid/siwe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
60 lines (50 loc) · 1.17 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
package siwe
import (
"fmt"
"strings"
"time"
"github.com/dchest/uniuri"
"github.com/relvacode/iso8601"
)
func parseTimestamp(fields map[string]interface{}, key string) (*string, error) {
var value string
if val, ok := fields[key]; ok {
switch val.(type) {
case time.Time:
value = val.(time.Time).UTC().Format(time.RFC3339)
case string:
_, err := iso8601.ParseString(val.(string))
if err != nil {
return nil, &InvalidMessage{fmt.Sprintf("Invalid format for field `%s`", key)}
}
value = val.(string)
default:
return nil, &InvalidMessage{fmt.Sprintf("`%s` must be either an ISO8601 formatted string or time.Time", key)}
}
}
if value == "" {
return nil, nil
}
return &value, nil
}
func GenerateNonce() string {
return uniuri.NewLen(16)
}
func isNotEmpty(str *string) bool {
return str != nil && len(strings.TrimSpace(*str)) > 0
}
func isEmpty(str *string) bool {
return str == nil || len(strings.TrimSpace(*str)) == 0
}
func isStringAndNotEmpty(m map[string]interface{}, k string) (*string, bool) {
if v, ok := m[k]; ok {
switch v.(type) {
case string:
s := v.(string)
if s != "" {
return &s, true
}
}
}
return nil, false
}