-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
jwt.go
141 lines (125 loc) · 3.1 KB
/
jwt.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
package jwt
import (
"encoding/json"
"fmt"
"strings"
"time"
jwtgo "github.com/golang-jwt/jwt/v4"
)
// MapClaims converts a jwt.Claims to a MapClaims
func MapClaims(claims jwtgo.Claims) (jwtgo.MapClaims, error) {
if mapClaims, ok := claims.(*jwtgo.MapClaims); ok {
return *mapClaims, nil
}
claimsBytes, err := json.Marshal(claims)
if err != nil {
return nil, err
}
var mapClaims jwtgo.MapClaims
err = json.Unmarshal(claimsBytes, &mapClaims)
if err != nil {
return nil, err
}
return mapClaims, nil
}
// StringField extracts a field from the claims as a string
func StringField(claims jwtgo.MapClaims, fieldName string) string {
if fieldIf, ok := claims[fieldName]; ok {
if field, ok := fieldIf.(string); ok {
return field
}
}
return ""
}
// Float64Field extracts a field from the claims as a float64
func Float64Field(claims jwtgo.MapClaims, fieldName string) float64 {
if fieldIf, ok := claims[fieldName]; ok {
if field, ok := fieldIf.(float64); ok {
return field
}
}
return 0
}
// GetScopeValues extracts the values of specified scopes from the claims
func GetScopeValues(claims jwtgo.MapClaims, scopes []string) []string {
groups := make([]string, 0)
for i := range scopes {
scopeIf, ok := claims[scopes[i]]
if !ok {
continue
}
switch val := scopeIf.(type) {
case []interface{}:
for _, groupIf := range val {
group, ok := groupIf.(string)
if ok {
groups = append(groups, group)
}
}
case []string:
groups = append(groups, val...)
case string:
groups = append(groups, val)
}
}
return groups
}
func numField(m jwtgo.MapClaims, key string) (int64, error) {
field, ok := m[key]
if !ok {
return 0, fmt.Errorf("token does not have %s claim", key)
}
switch val := field.(type) {
case float64:
return int64(val), nil
case json.Number:
return val.Int64()
case int64:
return val, nil
default:
return 0, fmt.Errorf("%s '%v' is not a number", key, val)
}
}
// IssuedAt returns the issued at as an int64
func IssuedAt(m jwtgo.MapClaims) (int64, error) {
return numField(m, "iat")
}
// IssuedAtTime returns the issued at as a time.Time
func IssuedAtTime(m jwtgo.MapClaims) (time.Time, error) {
iat, err := IssuedAt(m)
return time.Unix(iat, 0), err
}
// ExpirationTime returns the expiration as a time.Time
func ExpirationTime(m jwtgo.MapClaims) (time.Time, error) {
exp, err := numField(m, "exp")
return time.Unix(exp, 0), err
}
func Claims(in interface{}) jwtgo.Claims {
claims, ok := in.(jwtgo.Claims)
if ok {
return claims
}
return nil
}
// IsMember returns whether or not the user's claims is a member of any of the groups
func IsMember(claims jwtgo.Claims, groups []string, scopes []string) bool {
mapClaims, err := MapClaims(claims)
if err != nil {
return false
}
// O(n^2) loop
for _, userGroup := range GetGroups(mapClaims, scopes) {
for _, group := range groups {
if userGroup == group {
return true
}
}
}
return false
}
func GetGroups(mapClaims jwtgo.MapClaims, scopes []string) []string {
return GetScopeValues(mapClaims, scopes)
}
func IsValid(token string) bool {
return len(strings.SplitN(token, ".", 3)) == 3
}