-
Notifications
You must be signed in to change notification settings - Fork 45
/
jwt.go
157 lines (144 loc) · 4.49 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
package middleware
import (
"errors"
"strings"
"time"
"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/utils"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"github.com/sirupsen/logrus"
)
// JWTMAuth check if authed and set uid to context
func JWTAuth(c *gin.Context) {
defer func() {
logrus.WithContext(c).Info("finish jwt middleware")
}()
var tokenStr string
if tokenStr = c.Copy().Query(common.TokenKey); len(tokenStr) != 0 {
if t, err := utils.ParseToken(conf.JWTSecret(), tokenStr); err == nil {
for k, v := range t {
c.Set(k, v)
}
logrus.WithContext(c).Infof("query auth success")
if err = resignAndPatchCtxJWT(c, t, tokenStr); err != nil {
logrus.WithContext(c).WithError(err).Errorf("resign jwt error")
common.ErrUnAuthorized(c, "resign jwt error")
c.Abort()
return
}
c.Next()
SetToken(c, utils.ToStr(t[common.UserIDKey]))
return
}
logrus.WithContext(c).Infof("query auth failed")
}
cookieToken, err := c.Cookie(conf.Get().App.CookieName)
if err == nil {
if t, err := utils.ParseToken(conf.JWTSecret(), cookieToken); err == nil {
for k, v := range t {
c.Set(k, v)
}
logrus.WithContext(c).Infof("cookie auth success")
if err = resignAndPatchCtxJWT(c, t, cookieToken); err != nil {
logrus.WithContext(c).WithError(err).Errorf("resign jwt error")
common.ErrUnAuthorized(c, "resign jwt error")
c.Abort()
return
}
c.Next()
return
} else {
logrus.WithContext(c).WithError(err).Errorf("jwt middleware parse token error")
common.ErrUnAuthorized(c, "invalid authorization")
c.Abort()
return
}
}
tokenStr = c.Request.Header.Get(common.AuthorizationKey)
tokenStr = strings.TrimPrefix(tokenStr, "Bearer ")
if tokenStr == "" || tokenStr == "null" {
logrus.WithContext(c).WithError(errors.New("authorization is empty")).Infof("authorization is empty")
common.ErrUnAuthorized(c, "invalid authorization")
c.Abort()
return
}
if t, err := utils.ParseToken(conf.JWTSecret(), tokenStr); err == nil {
for k, v := range t {
c.Set(k, v)
}
logrus.WithContext(c).Infof("header auth success")
if err = resignAndPatchCtxJWT(c, t, tokenStr); err != nil {
logrus.WithContext(c).WithError(err).Errorf("resign jwt error")
common.ErrUnAuthorized(c, "resign jwt error")
c.Abort()
return
}
c.Next()
return
} else {
logrus.WithContext(c).WithError(err).Errorf("jwt middleware parse token error")
}
}
func resignAndPatchCtxJWT(c *gin.Context, t jwt.MapClaims, tokenStr string) error {
tokenExpire, _ := t.GetExpirationTime()
now := time.Now().Add(time.Duration(conf.Get().App.CookieAge/2) * time.Second)
if now.Before(tokenExpire.Time) {
logrus.WithContext(c).Infof("jwt not going to expire, continue to use old one")
c.Set(common.TokenKey, tokenStr)
return nil
}
token, err := utils.GetJwtTokenFromMap(conf.JWTSecret(),
time.Now().Unix(),
int64(conf.Get().App.CookieAge),
map[string]string{common.UserIDKey: utils.ToStr(t[common.UserIDKey])})
if err != nil {
c.Set(common.TokenKey, tokenStr)
logrus.WithContext(c).WithError(err).Errorf("resign jwt error")
return err
}
logrus.WithContext(c).Infof("jwt going to expire, resigning token")
c.Header(common.SetAuthorizationKey, token)
c.SetCookie(conf.Get().App.CookieName,
token,
conf.Get().App.CookieAge,
conf.Get().App.CookiePath,
conf.Get().App.CookieDomain,
conf.Get().App.CookieSecure,
conf.Get().App.CookieHTTPOnly)
c.Set(common.TokenKey, token)
return nil
}
func SetToken(c *gin.Context, uid string) (string, error) {
logrus.WithContext(c).Infof("set token for uid:[%s]", uid)
token, err := utils.GetJwtTokenFromMap(conf.JWTSecret(),
time.Now().Unix(),
int64(conf.Get().App.CookieAge),
map[string]string{common.UserIDKey: uid})
if err != nil {
return "", err
}
c.SetCookie(conf.Get().App.CookieName,
token,
conf.Get().App.CookieAge,
conf.Get().App.CookiePath,
conf.Get().App.CookieDomain,
conf.Get().App.CookieSecure,
conf.Get().App.CookieHTTPOnly)
c.Set(common.TokenKey, token)
c.Header(common.SetAuthorizationKey, token)
return token, nil
}
// PushTokenStr 推送token到客户端
func PushTokenStr(c *gin.Context, tokenStr string) {
logrus.WithContext(c).Infof("push new token to client")
c.SetCookie(conf.Get().App.CookieName,
tokenStr,
conf.Get().App.CookieAge,
conf.Get().App.CookiePath,
conf.Get().App.CookieDomain,
conf.Get().App.CookieSecure,
conf.Get().App.CookieHTTPOnly)
c.Header(common.SetAuthorizationKey, tokenStr)
}