Skip to content

Commit 2e686fb

Browse files
committed
fix jwt too large, close #386
Signed-off-by: Zzde <zhangxh1997@gmail.com>
1 parent cbffdc6 commit 2e686fb

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

pkg/auth/oauth_manager.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type OAuthManager struct {
1818
jwtSecret string
1919
}
2020

21+
// Keep headroom for cookie name and attributes within common 4096-byte browser limits.
22+
const maxAuthTokenCookieValueLength = 3800
23+
2124
func NewOAuthManager() *OAuthManager {
2225
return &OAuthManager{
2326
jwtSecret: common.JwtSecret,
@@ -89,6 +92,17 @@ func (om *OAuthManager) GenerateJWT(user *model.User, refreshToken string) (stri
8992
},
9093
}
9194

95+
tokenString, err := om.signJWT(claims)
96+
if err != nil {
97+
return "", err
98+
}
99+
if len(tokenString) > maxAuthTokenCookieValueLength {
100+
claims.RefreshToken = "" // Drop refresh token from JWT if it exceeds size limits
101+
}
102+
return om.signJWT(claims)
103+
}
104+
105+
func (om *OAuthManager) signJWT(claims Claims) (string, error) {
92106
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
93107
return token.SignedString([]byte(om.jwtSecret))
94108
}

0 commit comments

Comments
 (0)