-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
284 lines (258 loc) · 7.95 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) 2020 Kien Nguyen-Tuan <kiennt2609@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jwt
import (
"crypto/rand"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"time"
jwtGo "github.com/golang-jwt/jwt/v4"
jwtGoRequest "github.com/golang-jwt/jwt/v4/request"
)
type Token struct {
privateKey interface{}
publicKey interface{}
signingMethod jwtGo.SigningMethod
store Store
options Options
}
type TokenInfo struct {
Id string
IssuedAt float64
Data map[string]interface{}
}
// Claims holds the claims encoded in a JWT
type Claims struct {
// RegisteredClaims are a structured version of the JWT Claims Set,
// restricted to Registered Claim Names, as referenced at
// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
//
// This type can be used on its own, but then additional private and
// public claims embedded in the JWT will not be parsed. The typical usecase
// therefore is to embedded this in a user-defined claim type.
*jwtGo.RegisteredClaims
Data map[string]interface{} `json:"data,omitempty"`
}
// NewToken constructs a new Token instance
func NewToken(o Options, s Store) (*Token, error) {
signingMethod := jwtGo.GetSigningMethod(o.SigningMethod)
var (
privateKey interface{}
publicKey interface{}
err error
)
switch signingMethod {
case jwtGo.SigningMethodHS256, jwtGo.SigningMethodHS384, jwtGo.SigningMethodHS512:
if o.HMACKey == nil {
return nil, ErrNoHMACKey
}
privateKey = o.HMACKey
publicKey = o.HMACKey
case jwtGo.SigningMethodRS256, jwtGo.SigningMethodRS384, jwtGo.SigningMethodRS512:
if o.PrivateKeyLocation == "" || o.PublicKeyLocation == "" {
return nil, ErrNoRSAKey
}
privateKey, publicKey, err = getRSAKeys(o.PrivateKeyLocation, o.PublicKeyLocation)
if err != nil {
return nil, err
}
case jwtGo.SigningMethodES256, jwtGo.SigningMethodES384, jwtGo.SigningMethodES512:
if o.PrivateKeyLocation == "" || o.PublicKeyLocation == "" {
return nil, ErrNoECKey
}
privateKey, publicKey, err = getECKeys(o.PrivateKeyLocation, o.PublicKeyLocation)
if err != nil {
return nil, err
}
default:
return nil, ErrInvalidSigningMethod
}
if o.UserProperty == "" {
o.UserProperty = "user"
}
return &Token{
privateKey: privateKey,
publicKey: publicKey,
signingMethod: signingMethod,
store: s,
options: o,
}, nil
}
// GetToken extracts a token string from the header.
func (t *Token) GetToken(req *http.Request) (string, error) {
if t.options.IsBearerToken {
return jwtGoRequest.AuthorizationHeaderExtractor.ExtractToken(req)
}
header := req.Header.Get(t.options.Header)
return header, nil
}
// GenerateToken generate a JWT.
// Please don't add sensitive data such as password to payload of JWT.
func (t *Token) GenerateToken(data map[string]interface{}) (string, error) {
id, err := generateRandString(32)
if err != nil {
return "", fmt.Errorf("JWT: unable to generate JWT id, %s", err)
}
claims := Claims{
RegisteredClaims: &jwtGo.RegisteredClaims{
ExpiresAt: jwtGo.NewNumericDate(time.Now().Add(t.options.TTL)),
IssuedAt: jwtGo.NewNumericDate(time.Now()),
ID: id,
},
Data: data,
}
unsigned := jwtGo.NewWithClaims(t.signingMethod, claims)
return unsigned.SignedString(t.privateKey)
}
func (t *Token) CheckToken(tokenString string) (map[string]interface{}, error) {
tokenInfo, err := t.validateJWT(tokenString)
if err != nil {
return nil, err
}
// When there is no storage, we should like to return information from token.
if t.store == nil {
return tokenInfo.Data, nil
}
return t.store.Check(tokenInfo.Id, tokenInfo.IssuedAt)
}
// ValidateToken validates whether a jwt string is valid.
// If so, it returns data included in the token and nil error.
func (t *Token) ValidateToken(tokenString string) (*TokenInfo, error) {
return t.validateJWT(tokenString)
}
// RevokeToken revokes a token which is no longer in use.
// This case often happens when a user logs out.
// or an authorization ends.
func (t *Token) RevokeToken(id string) error {
if t.store == nil {
return ErrNoStore
}
return t.store.Revoke(id)
}
// RefreshToken regenerate the token after check the given token
// string is valid.
func (t *Token) RefreshToken(tokenString string) (string, error) {
tokenInfo, err := t.validateJWT(tokenString)
if err != nil {
return "", err
}
return t.GenerateToken(tokenInfo.Data)
}
func (t *Token) validateJWT(tokenString string) (*TokenInfo, error) {
token, err := jwtGo.Parse(tokenString, func(token *jwtGo.Token) (interface{}, error) {
// Validate the algorithm is what you expect
if token.Method.Alg() != t.signingMethod.Alg() {
return nil, ErrUnexpectedSigningMethod
}
return t.publicKey, nil
})
if err != nil {
if e, ok := err.(*jwtGo.ValidationError); ok {
switch {
case e.Errors&jwtGo.ValidationErrorMalformed != 0:
// Token is malformed
return nil, ErrTokenMalformed
case e.Errors&jwtGo.ValidationErrorExpired != 0:
// Token is expired
return nil, ErrTokenExpired
case e.Errors&jwtGo.ValidationErrorNotValidYet != 0:
// Token is not active yet
return nil, ErrTokenNotActive
case e.Inner != nil:
// report e.Inner
return nil, e.Inner
}
}
return nil, err
}
if !token.Valid {
return nil, ErrInvalidToken
}
claims := token.Claims.(jwtGo.MapClaims)
if claims["jti"] == nil || claims["iat"] == nil {
return nil, ErrInvalidToken
}
jti, ok := claims["jti"].(string)
if !ok {
return nil, ErrGetTokenId
}
iat, ok := claims["iat"].(float64)
if !ok {
return nil, ErrGetIssuedTime
}
if claims["data"] == nil {
return nil, nil
}
data, ok := claims["data"].(map[string]interface{})
if !ok {
return nil, ErrGetData
}
return &TokenInfo{
Id: jti,
IssuedAt: iat,
Data: data,
}, nil
}
func getKeyContent(keyLocation string) ([]byte, error) {
keyContent, err := ioutil.ReadFile(keyLocation)
if err != nil {
return nil, fmt.Errorf("JWT: failed to load a key from %s, %s", keyLocation, err)
}
return keyContent, nil
}
func getRSAKeys(privateKeyLocation, publicKeyLocation string) (interface{}, interface{}, error) {
privateKeyContent, err := getKeyContent(privateKeyLocation)
if err != nil {
return nil, nil, err
}
publicKeyContent, err := getKeyContent(publicKeyLocation)
if err != nil {
return nil, nil, err
}
privateKey, err := jwtGo.ParseRSAPrivateKeyFromPEM(privateKeyContent)
if err != nil {
return nil, nil, fmt.Errorf("JWT: failed to generate a private RSA key, %s", err)
}
publicKey, err := jwtGo.ParseRSAPublicKeyFromPEM(publicKeyContent)
if err != nil {
return nil, nil, fmt.Errorf("JWT: failed to generate a public RSA key, %s", err)
}
return privateKey, publicKey, nil
}
func getECKeys(privateKeyLocation, publicKeyLocation string) (interface{}, interface{}, error) {
privateKeyContent, err := getKeyContent(privateKeyLocation)
if err != nil {
return nil, nil, err
}
publicKeyContent, err := getKeyContent(publicKeyLocation)
if err != nil {
return nil, nil, err
}
privateKey, err := jwtGo.ParseECPrivateKeyFromPEM(privateKeyContent)
if err != nil {
return nil, nil, fmt.Errorf("JWT: failed to generate a private EC key, %s", err)
}
publicKey, err := jwtGo.ParseECPublicKeyFromPEM(publicKeyContent)
if err != nil {
return nil, nil, fmt.Errorf("JWT: failed to generate a public EC key, %s", err)
}
return privateKey, publicKey, nil
}
func generateRandString(n int) (string, error) {
b := make([]byte, n)
_, err := rand.Read(b)
return base64.URLEncoding.EncodeToString(b), err
}