-
Notifications
You must be signed in to change notification settings - Fork 2
/
jwt.go
282 lines (243 loc) · 7.62 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
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
package jwt
import (
"log"
"net/http"
"strings"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
const (
// ErrKey is used to set error into gin.Context.
ErrKey = "GIN_JWT_ERROR"
// PayloadKey is used to set payload of jwt token into gin.Context.
PayloadKey = "GIN_JWT_PAYLOAD"
// TokenKey is used to set a jwt token into gin.Context.
TokenKey = "GIN_JWT_TOKEN"
// UserKey is used to set a user into gin.Context.
UserKey = "GIN_JWT_USER"
authorizationHeaderKey = "Authorization"
authorizationHeaderPrefix = "Bearer "
)
// MapClaims is alias of github.com/dgrijalva/jwt-go.MapClaims.
type MapClaims = jwt.MapClaims
var (
// ErrorAuthorizationHeaderIsEmpty is a error in the case of Authorization header is empty.
ErrorAuthorizationHeaderIsEmpty = errors.New("Authorization header is empty")
// ErrorAuthorizationHeaderIsInvalid is a error in the case of Authorization header isn't valid.
ErrorAuthorizationHeaderIsInvalid = errors.New("Authorization header is invalid")
// ErrorAuthorizationTokenExpired is an error in the case of Authorization token is expired.
ErrorAuthorizationTokenExpired = errors.New("Authorization token is expired")
// ErrorAuthenticationFailed is an error at authentication is failed.
ErrorAuthenticationFailed = errors.New("Authentication failled")
// ErrorPermissionDenied is a error at permission is denied.
ErrorPermissionDenied = errors.New("permission is denied")
// ErrorUserNotFound is an error in the case of a user not found.
ErrorUserNotFound = errors.New("user not found")
)
// ErrorHandler handles gin-jwt's errors from gin.Context.
func ErrorHandler(c *gin.Context) {
c.Next()
err := Error(c)
if err == nil {
return
}
switch err {
// status 401
case ErrorAuthorizationHeaderIsEmpty,
ErrorAuthorizationHeaderIsInvalid,
ErrorAuthorizationTokenExpired,
ErrorAuthenticationFailed:
log.Println(err)
// status 403
case ErrorPermissionDenied:
log.Println(err)
// status 500
default:
log.Println(err)
}
}
// Auth provides useful authorization/authentication functions.
type Auth struct {
// ExpiryInterval is an interval of expiration that is used to calculate the `exp` claim of JWT.
ExpiryInterval time.Duration
// SigningMethod is a method of the signing of JWT token. default is `HS256`.
SigningMethod string
// SecretKey is a secret key for signing JWT.
SecretKey []byte
// Authenticator authenticates a request and return jwt.MapClaims
// that contains a user information of the request.
Authenticator func(*gin.Context) (MapClaims, error)
// UserFetcher takes a jwt.MapClaims and return a user object.
UserFetcher func(*gin.Context, MapClaims) (interface{}, error)
// this is for testing.
nowFunc func() time.Time
}
// New returns initialized Auth.
// If Authenticator or UserFetcher are nil, return the error.
func New(a Auth) (Auth, error) {
if a.Authenticator == nil {
return Auth{}, errors.New("missing Auth.Authenticator")
}
if a.UserFetcher == nil {
return Auth{}, errors.New("missing Auth.UserFetcher")
}
if a.SigningMethod == "" {
a.SigningMethod = "HS256"
}
if a.ExpiryInterval == 0 {
a.ExpiryInterval = time.Hour
}
a.nowFunc = time.Now
return a, nil
}
// VerifyPerm is used to pass a MapClaim to a authorization function.
// After authorization, use UserFetcher to get a user object by identity,
// and set it into the gin.Context.
func (a Auth) VerifyPerm(permitted func(MapClaims) bool) gin.HandlerFunc {
return func(c *gin.Context) {
token, err := a.parseToken(c)
if handleParseTokenError(c, err) {
c.Abort()
return
}
claims := token.Claims.(MapClaims)
// set payload to context for debug
c.Set(PayloadKey, claims)
if !permitted(claims) {
c.Set(ErrKey, ErrorPermissionDenied)
c.Status(http.StatusForbidden)
c.Abort()
return
}
u, err := a.UserFetcher(c, claims)
if err != nil {
c.Set(ErrKey, err)
c.Status(http.StatusInternalServerError)
c.Abort()
return
}
if u == nil {
c.Set(ErrKey, ErrorUserNotFound)
c.Status(http.StatusInternalServerError)
c.Abort()
return
}
c.Set(UserKey, u)
c.Next()
}
}
// Authenticate can be used by clients to get a jwt token.
// Authenticator of Auth is used in this method to authenticate a user request.
// Authorization token that is a form of `Authorization: Bearer <TOKEN>` is supplied
// in a response header.
func (a Auth) Authenticate(c *gin.Context) {
claims, err := a.Authenticator(c)
if err != nil {
c.Set(ErrKey, err)
c.Status(http.StatusUnauthorized)
c.Abort()
return
}
a.respondAuthorizationHeader(c, claims)
}
// RefreshToken can be used to refresh a token. The token still needs to be valid on refresh.
// Authorization token that is a form of `Authorization: Bearer <TOKEN>` is supplied
// in a response header.
func (a Auth) RefreshToken(c *gin.Context) {
token, err := a.parseToken(c)
if handleParseTokenError(c, err) {
c.Abort()
return
}
a.respondAuthorizationHeader(c, token.Claims.(MapClaims))
}
// respondAuthorizationHeader create a authorization token and set it in the response header.
func (a Auth) respondAuthorizationHeader(c *gin.Context, claims MapClaims) {
newToken := jwt.New(jwt.GetSigningMethod(a.SigningMethod))
newToken.Claims = a.refreshExp(claims)
tokenString, err := newToken.SignedString(a.SecretKey)
if err != nil {
c.Set(ErrKey, err)
c.Status(http.StatusInternalServerError)
return
}
c.Header(authorizationHeaderKey, authorizationHeaderPrefix+tokenString)
c.Status(http.StatusOK)
}
func (a Auth) parseToken(c *gin.Context) (*jwt.Token, error) {
tokenStr, err := a.authToken(c.Request.Header)
if err != nil {
return nil, err
}
// set token string to context for debug.
c.Set(TokenKey, tokenStr)
return a._parseToken(tokenStr)
}
// _parseToken is separated from the `parseToken` function because it's used for testing.
func (a Auth) _parseToken(token string) (*jwt.Token, error) {
return jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
return a.SecretKey, nil
})
}
// Extract a token string from a request header.
func (a Auth) authToken(header http.Header) (string, error) {
authHeader := header.Get(authorizationHeaderKey)
if authHeader == "" {
return "", errors.WithStack(ErrorAuthorizationHeaderIsEmpty)
}
if !strings.HasPrefix(authHeader, authorizationHeaderPrefix) {
return "", errors.WithStack(ErrorAuthorizationHeaderIsInvalid)
}
return strings.TrimPrefix(authHeader, authorizationHeaderPrefix), nil
}
// handleParseTokenError is used for error handling of parseToken.
func handleParseTokenError(c *gin.Context, err error) bool {
switch errors.Cause(err) {
case ErrorAuthorizationHeaderIsEmpty, ErrorAuthorizationHeaderIsInvalid:
c.Status(http.StatusForbidden)
c.Set(ErrKey, err)
return true
}
validationErr, ok := err.(*jwt.ValidationError)
if ok && validationErr.Errors == jwt.ValidationErrorExpired {
c.Status(http.StatusUnauthorized)
c.Set(ErrKey, ErrorAuthorizationTokenExpired)
return true
}
if err != nil {
c.Status(http.StatusInternalServerError)
c.Set(ErrKey, err)
return true
}
return false
}
// refreshExp refreshes `exp` and `iat`.
func (a Auth) refreshExp(claims MapClaims) MapClaims {
newClaims := MapClaims{}
for k, v := range claims {
newClaims[k] = v
}
now := a.nowFunc()
expire := now.Add(a.ExpiryInterval)
newClaims["exp"] = expire.Unix()
newClaims["iat"] = now.Unix()
return newClaims
}
// Error extracts a error from gin.Context.
func Error(c *gin.Context) interface{} {
v, ok := c.Get(ErrKey)
if !ok {
return nil
}
return v
}
// User extracts the user object from gin.Context that VerifyPerm set.
func User(c *gin.Context) interface{} {
v, ok := c.Get(UserKey)
if !ok {
return nil
}
return v
}