-
Notifications
You must be signed in to change notification settings - Fork 145
/
token.go
63 lines (51 loc) · 1.25 KB
/
token.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
package loader
import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
)
type tokenConf struct {
serverURL string
grpcBroadcastAddress string
tenantId string
}
func getConfFromJWT(token string) (*tokenConf, error) {
claims, err := extractClaimsFromJWT(token)
if err != nil {
return nil, err
}
serverURL, ok := claims["server_url"].(string)
if !ok {
return nil, fmt.Errorf("server_url claim not found")
}
grpcBroadcastAddress, ok := claims["grpc_broadcast_address"].(string)
if !ok {
return nil, fmt.Errorf("grpc_broadcast_address claim not found")
}
tenantId, ok := claims["sub"].(string)
if !ok {
return nil, fmt.Errorf("sub claim not found")
}
return &tokenConf{
serverURL: serverURL,
grpcBroadcastAddress: grpcBroadcastAddress,
tenantId: tenantId,
}, nil
}
func extractClaimsFromJWT(token string) (map[string]interface{}, error) {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return nil, fmt.Errorf("invalid token format")
}
claimsData, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, err
}
var claims map[string]interface{}
err = json.Unmarshal(claimsData, &claims)
if err != nil {
return nil, err
}
return claims, nil
}