forked from centrifugal/centrifuge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_verifier_jwt.go
137 lines (126 loc) · 4 KB
/
token_verifier_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
package centrifuge
import (
"crypto/rsa"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"sync"
"github.com/dgrijalva/jwt-go/v4"
)
type tokenVerifierJWT struct {
mu sync.RWMutex
TokenHMACSecretKey []byte
TokenRSAPublicKey *rsa.PublicKey
}
func newTokenVerifierJWT(tokenHMACSecretKey string, tokenRSAPublicKey *rsa.PublicKey) tokenVerifier {
return &tokenVerifierJWT{
TokenHMACSecretKey: []byte(tokenHMACSecretKey),
TokenRSAPublicKey: tokenRSAPublicKey,
}
}
var (
errTokenExpired = errors.New("token expired")
errMalformedToken = errors.New("malformed token")
)
type connectTokenClaims struct {
Info json.RawMessage `json:"info"`
Base64Info string `json:"b64info"`
Channels []string `json:"channels"`
jwt.StandardClaims
}
type subscribeTokenClaims struct {
Client string `json:"client"`
Channel string `json:"channel"`
Info json.RawMessage `json:"info"`
Base64Info string `json:"b64info"`
ExpireTokenOnly bool `json:"eto"`
jwt.StandardClaims
}
func (verifier *tokenVerifierJWT) VerifyConnectToken(token string) (connectToken, error) {
parsedToken, err := jwt.ParseWithClaims(token, &connectTokenClaims{}, verifier.jwtKeyFunc())
if err != nil {
if _, ok := err.(*jwt.TokenExpiredError); ok {
// The only problem with token is its expiration - no other
// errors set in Errors bitfield.
return connectToken{}, errTokenExpired
}
return connectToken{}, err
}
if claims, ok := parsedToken.Claims.(*connectTokenClaims); ok && parsedToken.Valid {
token := connectToken{
UserID: claims.StandardClaims.Subject,
Info: claims.Info,
Channels: claims.Channels,
}
if claims.StandardClaims.ExpiresAt != nil {
token.ExpireAt = claims.StandardClaims.ExpiresAt.Unix()
}
if claims.Base64Info != "" {
byteInfo, err := base64.StdEncoding.DecodeString(claims.Base64Info)
if err != nil {
return connectToken{}, err
}
token.Info = byteInfo
}
return token, nil
}
return connectToken{}, errMalformedToken
}
func (verifier *tokenVerifierJWT) VerifySubscribeToken(token string) (subscribeToken, error) {
parsedToken, err := jwt.ParseWithClaims(token, &subscribeTokenClaims{}, verifier.jwtKeyFunc())
if err != nil {
if _, ok := err.(*jwt.TokenExpiredError); ok {
// The only problem with token is its expiration - no other
// errors set in Errors bitfield.
return subscribeToken{}, errTokenExpired
}
return subscribeToken{}, err
}
if claims, ok := parsedToken.Claims.(*subscribeTokenClaims); ok && parsedToken.Valid {
token := subscribeToken{
Client: claims.Client,
Info: claims.Info,
Channel: claims.Channel,
ExpireTokenOnly: claims.ExpireTokenOnly,
}
if claims.StandardClaims.ExpiresAt != nil {
token.ExpireAt = claims.StandardClaims.ExpiresAt.Unix()
}
if claims.Base64Info != "" {
byteInfo, err := base64.StdEncoding.DecodeString(claims.Base64Info)
if err != nil {
return subscribeToken{}, err
}
token.Info = byteInfo
}
return token, nil
}
return subscribeToken{}, errMalformedToken
}
func (verifier *tokenVerifierJWT) Reload(config Config) {
verifier.mu.Lock()
defer verifier.mu.Unlock()
verifier.TokenRSAPublicKey = config.TokenRSAPublicKey
verifier.TokenHMACSecretKey = []byte(config.TokenHMACSecretKey)
}
func (verifier *tokenVerifierJWT) jwtKeyFunc() func(token *jwt.Token) (interface{}, error) {
return func(token *jwt.Token) (interface{}, error) {
verifier.mu.RLock()
defer verifier.mu.RUnlock()
switch token.Method.(type) {
case *jwt.SigningMethodHMAC:
if len(verifier.TokenHMACSecretKey) == 0 {
return nil, fmt.Errorf("token HMAC secret key not set")
}
return verifier.TokenHMACSecretKey, nil
case *jwt.SigningMethodRSA:
if verifier.TokenRSAPublicKey == nil {
return nil, fmt.Errorf("token RSA public key not set")
}
return verifier.TokenRSAPublicKey, nil
default:
return nil, fmt.Errorf("unsupported signing method: %v", token.Header["alg"])
}
}
}