forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
51 lines (46 loc) · 1.04 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
package jwt
import (
"encoding/json"
jwtgo "github.com/dgrijalva/jwt-go"
)
// MapClaims converts a jwt.Claims to a MapClaims
func MapClaims(claims jwtgo.Claims) (jwtgo.MapClaims, error) {
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
}
// GetField extracts a field from the claims as a string
func GetField(claims jwtgo.MapClaims, fieldName string) string {
if fieldIf, ok := claims[fieldName]; ok {
if field, ok := fieldIf.(string); ok {
return field
}
}
return ""
}
// GetGroups extracts the groups from a claims
func GetGroups(claims jwtgo.MapClaims) []string {
groups := make([]string, 0)
groupsIf, ok := claims["groups"]
if !ok {
return groups
}
groupIfList, ok := groupsIf.([]interface{})
if !ok {
return groups
}
for _, groupIf := range groupIfList {
group, ok := groupIf.(string)
if ok {
groups = append(groups, group)
}
}
return groups
}