-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathginsec.go
344 lines (273 loc) · 8.06 KB
/
ginsec.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package ginsec
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"net/http"
"strings"
"time"
)
// MapClaims Default user claims
type MapClaims map[string]interface{}
// GinJWTMiddleware The Gin-JWT-Go middleware
type GinJWTMiddleware struct {
// Realm name of the realm. Required.
Realm string
// Key secret key used to sign the jwt token. Required.
Key []byte
// Timeout expiration of the jwt token. Default is 1 hour.
Timeout time.Duration
// RefreshTimeout expiration of the refresh token. Default is 2 hours.
RefreshTimeout time.Duration
// IdentityKey set the identity key. Required.
IdentityKey string
// IdentityHandler set the identity handler function. Required.
IdentityHandler func(*gin.Context) interface{}
// Callback function that should perform the authorization of the authenticated user. Called
// only after an authentication success. Must return true on success, false on failure.
// Optional, default to success.
Authorizator func(data interface{}, c *gin.Context) bool
// Unauthorized allow users to define a response. Optional.
Unauthorized func(*gin.Context, int, string)
// Callback function that will be called during login.
// Using this function it is possible to add additional payload data to the webtoken.
// The data is then made available during requests via c.Get("JWT_PAYLOAD").
// Note that the payload is not encrypted.
// The attributes mentioned on jwt.io can't be used as keys for the map.
// Optional, by default no additional data will be set.
PayloadFunc func(data interface{}) MapClaims
// CookieName
CookieName string
// TokenLookup is a string in the form of "<source>:<name>" that is used
// to extract token from the request.
// Optional. Default value "header:Authorization".
// Possible values:
// - "header:<name>"
// - "cookie:<name>"
TokenLookup string
}
// Some constants used across GinSec.
const (
SigningAlgorithm = "HS256"
TokenHeadName = "Bearer"
)
// Errors
var (
ErrKeyEmpty = errors.New("key cannot be empty")
ErrIdentityHandlerEmpty = errors.New("idenity handler cannot be empty")
ErrIdentityKeyEmpty = errors.New("identity key cannot be empty")
ErrEmptyAuthHeader = errors.New("authentication header cannot be empty")
ErrEmptyCookieToken = errors.New("cookie cannot be empty")
ErrInvalidAuthHeader = errors.New("invalid authentication header")
ErrMissingExpField = errors.New("missing exp field")
ErrWrongFormatOfExp = errors.New("wrong exp field format")
ErrExpiredToken = errors.New("token expired")
ErrClaimsIncorrect = errors.New("you're claims are incorrect")
)
func New(mw *GinJWTMiddleware) (*GinJWTMiddleware, error) {
if err := mw.MiddlewareInit(); err != nil {
return nil, err
}
return mw, nil
}
func (mw *GinJWTMiddleware) MiddlewareInit() error {
if mw.Key == nil {
return ErrKeyEmpty
}
if mw.TokenLookup == "" {
mw.TokenLookup = "header:Authorization"
}
if mw.Realm == "" {
mw.Realm = "GinSec"
}
if mw.Timeout == 0 {
mw.Timeout = time.Hour
}
if mw.RefreshTimeout == 0 {
mw.RefreshTimeout = time.Hour * 2
}
if mw.IdentityKey == "" {
return ErrIdentityKeyEmpty
}
if mw.Authorizator == nil {
mw.Authorizator = func(data interface{}, c *gin.Context) bool {
return true
}
}
if mw.Unauthorized == nil {
mw.Unauthorized = func(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"code": code,
"message": message,
})
}
}
if mw.CookieName == "" {
mw.CookieName = "jwt"
}
if mw.IdentityHandler == nil {
return ErrIdentityHandlerEmpty
}
return nil
}
func (mw *GinJWTMiddleware) MiddlewareFunc() gin.HandlerFunc {
return func(c *gin.Context) {
mw.middlewareImpl(c)
}
}
func (mw *GinJWTMiddleware) middlewareImpl(c *gin.Context) {
claims, err := mw.GetClaimsFromJWT(c)
if err != nil {
mw.unauthorized(c, http.StatusUnauthorized, err.Error())
return
}
if claims["exp"] == nil {
mw.unauthorized(c, http.StatusBadRequest, ErrMissingExpField.Error())
return
}
if _, ok := claims["exp"].(float64); !ok {
mw.unauthorized(c, http.StatusBadRequest, ErrWrongFormatOfExp.Error())
return
}
if int64(claims["exp"].(float64)) < time.Now().Unix() {
mw.unauthorized(c, http.StatusUnauthorized, ErrExpiredToken.Error())
return
}
c.Set("JWT_PAYLOAD", claims)
identity := mw.IdentityHandler(c)
if identity != nil {
c.Set(mw.IdentityKey, identity)
}
if !mw.Authorizator(identity, c) {
mw.unauthorized(c, http.StatusUnauthorized, ErrClaimsIncorrect.Error())
return
}
c.Next()
}
func (mw *GinJWTMiddleware) GetClaimsFromJWT(c *gin.Context) (MapClaims, error) {
token, err := mw.ParseToken(c)
if err != nil {
return nil, err
}
claims := MapClaims{}
for key, value := range token.Claims.(jwt.MapClaims) {
claims[key] = value
}
return claims, nil
}
func (mw *GinJWTMiddleware) ParseToken(c *gin.Context) (*jwt.Token, error) {
var token string
var err error
methods := strings.Split(mw.TokenLookup, ",")
for _, method := range methods {
if len(token) > 0 {
break
}
parts := strings.Split(strings.TrimSpace(method), ":")
k := strings.TrimSpace(parts[0])
v := strings.TrimSpace(parts[1])
switch k {
case "header":
token, err = mw.jwtFromHeader(c, v)
case "cookie":
token, err = mw.jwtFromCookie(c, v)
}
}
if err != nil {
return nil, err
}
return jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
// save token string if valid
c.Set("JWT_TOKEN", token)
return mw.Key, nil
})
}
func (mw *GinJWTMiddleware) jwtFromHeader(c *gin.Context, key string) (string, error) {
authHeader := c.Request.Header.Get(key)
if authHeader == "" {
return "", ErrEmptyAuthHeader
}
parts := strings.SplitN(authHeader, " ", 2)
if !(len(parts) == 2 && parts[0] == TokenHeadName) {
return "", ErrInvalidAuthHeader
}
return parts[1], nil
}
func (mw *GinJWTMiddleware) jwtFromCookie(c *gin.Context, key string) (string, error) {
cookie, _ := c.Cookie(key)
if cookie == "" {
return "", ErrEmptyCookieToken
}
return cookie, nil
}
func (mw *GinJWTMiddleware) TokenGenerator(data interface{}) (string, time.Time, error) {
token := jwt.New(jwt.GetSigningMethod(SigningAlgorithm))
claims := token.Claims.(jwt.MapClaims)
if mw.PayloadFunc != nil {
for key, value := range mw.PayloadFunc(data) {
claims[key] = value
}
}
expire := time.Now().UTC().Add(mw.Timeout)
claims["exp"] = expire.Unix()
claims["orig_iat"] = time.Now().Unix()
tokenString, err := mw.signedString(token)
if err != nil {
return "", time.Time{}, err
}
return tokenString, expire, nil
}
func (mw *GinJWTMiddleware) RefreshTokenGenerator(data interface{}) (string, time.Time, error) {
token := jwt.New(jwt.GetSigningMethod(SigningAlgorithm))
claims := token.Claims.(jwt.MapClaims)
if mw.PayloadFunc != nil {
for key, value := range mw.PayloadFunc(data) {
claims[key] = value
}
}
expire := time.Now().UTC().Add(mw.RefreshTimeout)
claims["exp"] = expire.Unix()
claims["orig_iat"] = time.Now().Unix()
tokenString, err := mw.signedString(token)
if err != nil {
return "", time.Time{}, err
}
return tokenString, expire, nil
}
func (mw *GinJWTMiddleware) signedString(token *jwt.Token) (string, error) {
tokenString, err := token.SignedString(mw.Key)
return tokenString, err
}
func (mw *GinJWTMiddleware) unauthorized(c *gin.Context, code int, message string) {
c.Header("WWW-Authenticate", "JWT realm="+mw.Realm)
mw.Unauthorized(c, code, message)
}
func ExtractClaimsFromToken(token *jwt.Token) MapClaims {
if token == nil {
return make(MapClaims)
}
claims := MapClaims{}
for key, value := range token.Claims.(jwt.MapClaims) {
claims[key] = value
}
return claims
}
func GetToken(c *gin.Context) string {
token, exists := c.Get("JWT_TOKEN")
if !exists {
return ""
}
return token.(string)
}
func ExtractClaims(c *gin.Context) MapClaims {
claims, exists := c.Get("JWT_PAYLOAD")
if !exists {
return make(MapClaims)
}
return claims.(MapClaims)
}
func (mw *GinJWTMiddleware) ParseTokenString(token string) (*jwt.Token, error) {
return jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
return mw.Key, nil
})
}