-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
claims_pub.go
119 lines (101 loc) · 2.35 KB
/
claims_pub.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
package sjwt
import (
"fmt"
"strconv"
)
// Set adds/sets a name/value to claims
func (c Claims) Set(name string, value interface{}) { c[name] = value }
// Del deletes a name/value from claims
func (c Claims) Del(name string) { delete(c, name) }
// Has will let you know whether or not a claim exists
func (c Claims) Has(name string) bool { _, ok := c[name]; return ok }
// Get gets claim value
func (c Claims) Get(name string) (interface{}, error) {
if !c.Has(name) {
return nil, ErrNotFound
}
return c[name], nil
}
// GetBool will get the boolean value on the Claims
func (c Claims) GetBool(name string) (bool, error) {
if !c.Has(name) {
return false, ErrNotFound
}
// Type check
switch val := c[name].(type) {
case string:
v, _ := strconv.ParseBool(val)
return v, nil
case bool:
return val, nil
}
return false, ErrClaimValueInvalid
}
// GetStr will get the string value on the Claims
func (c Claims) GetStr(name string) (string, error) {
if !c.Has(name) {
return "", ErrNotFound
}
switch val := c[name].(type) {
case float32:
return strconv.FormatFloat(float64(val), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(val, 'f', -1, 64), nil
}
return fmt.Sprintf("%v", c[name]), nil
}
// GetInt will get the int value on the Claims
func (c Claims) GetInt(name string) (int, error) {
if !c.Has(name) {
return 0, ErrNotFound
}
switch val := c[name].(type) {
case string:
v, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return 0, ErrClaimValueInvalid
}
return int(v), nil
case float32:
return int(val), nil
case float64:
return int(val), nil
case uint:
return int(val), nil
case uint8:
return int(val), nil
case uint16:
return int(val), nil
case uint32:
return int(val), nil
case uint64:
return int(val), nil
case int:
return int(val), nil
case int8:
return int(val), nil
case int16:
return int(val), nil
case int32:
return int(val), nil
case int64:
return int(val), nil
}
return 0, ErrClaimValueInvalid
}
// GetFloat will get the float value on the Claims
func (c Claims) GetFloat(name string) (float64, error) {
if !c.Has(name) {
return 0, ErrNotFound
}
switch val := c[name].(type) {
case float32:
return float64(val), nil
case float64:
return float64(val), nil
case string:
v, _ := strconv.ParseFloat(val, 64)
return v, nil
}
return 0, ErrClaimValueInvalid
}