/
shared.go
114 lines (101 loc) · 2.23 KB
/
shared.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
package tokens
import "fmt"
// Claims represents token claims.
type Claims map[string]interface{}
func (c Claims) getFloat64Claim(claim string) (float64, bool) {
if value, ok := c[claim]; ok {
switch tvalue := value.(type) {
case float32:
return float64(tvalue), true
case uint:
return float64(tvalue), true
case uint32:
return float64(tvalue), true
case uint64:
return float64(tvalue), true
case int:
return float64(tvalue), true
case int32:
return float64(tvalue), true
case int64:
return float64(tvalue), true
case float64:
return tvalue, true
default:
return 0, false
}
}
return 0, false
}
func (c Claims) getInt64Claim(claim string) (int64, bool) {
if value, ok := c[claim]; ok {
switch tvalue := value.(type) {
case float32:
return int64(tvalue), true
case float64:
return int64(tvalue), true
case uint:
return int64(tvalue), true
case uint32:
return int64(tvalue), true
case uint64:
return int64(tvalue), true
case int:
return int64(tvalue), true
case int32:
return int64(tvalue), true
case int64:
return tvalue, true
default:
return 0, false
}
}
return 0, false
}
func (c Claims) getBoolClaim(claim string) (bool, bool) {
if value, ok := c[claim]; ok {
switch tvalue := value.(type) {
case bool:
return tvalue, true
default:
return false, false
}
}
return false, false
}
func (c Claims) HasClaim(claim string) bool {
_, ok := c[claim]
return ok
}
func (c Claims) GetClaim(claim string) (interface{}, bool) {
value, ok := c[claim]
return value, ok
}
func (c Claims) GetClaimMustString(claim string) (string, bool) {
if value, ok := c[claim]; ok {
switch tvalue := value.(type) {
case string:
return tvalue, ok
default:
return fmt.Sprintf("%v", tvalue), ok
}
}
return "", false
}
// TokenType is a token type.
type TokenType string
const (
// BearerTokenType is a bearer token type.
BearerTokenType TokenType = "Bearer"
// RefreshTokenType is a refresh token type.
RefreshTokenType TokenType = "Refresh"
)
func getTokenParts(parts []string) (string, string, string) {
return parts[0], parts[1], parts[2]
}
type defaultToken struct {
claims Claims
}
func (at *defaultToken) RawClaims() Claims {
return at.claims
}