mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-05-06 05:38:50 +00:00
feat: binding ip with session
🛡️staying safe even your frontend was hacked
This commit is contained in:
@@ -50,10 +50,8 @@ func initParams() *jwt.GinJWTMiddleware {
|
||||
|
||||
func payloadFunc() func(data any) jwt.MapClaims {
|
||||
return func(data any) jwt.MapClaims {
|
||||
if v, ok := data.(string); ok {
|
||||
return jwt.MapClaims{
|
||||
model.CtxKeyAuthorizedUser: v,
|
||||
}
|
||||
if v, ok := data.(map[string]interface{}); ok {
|
||||
return v
|
||||
}
|
||||
return jwt.MapClaims{}
|
||||
}
|
||||
@@ -62,7 +60,15 @@ func payloadFunc() func(data any) jwt.MapClaims {
|
||||
func identityHandler() func(c *gin.Context) any {
|
||||
return func(c *gin.Context) any {
|
||||
claims := jwt.ExtractClaims(c)
|
||||
userId := claims[model.CtxKeyAuthorizedUser].(string)
|
||||
userId := claims["user_id"].(string)
|
||||
tokenIP := claims["ip"].(string)
|
||||
currentIP := c.GetString(model.CtxKeyRealIPStr)
|
||||
|
||||
if tokenIP != currentIP {
|
||||
// IP地址不匹配,token无效
|
||||
return nil
|
||||
}
|
||||
|
||||
var user model.User
|
||||
if err := singleton.DB.First(&user, userId).Error; err != nil {
|
||||
return nil
|
||||
@@ -109,7 +115,12 @@ func authenticator() func(c *gin.Context) (any, error) {
|
||||
|
||||
model.UnblockIP(singleton.DB, realip, model.BlockIDUnknownUser)
|
||||
model.UnblockIP(singleton.DB, realip, int64(user.ID))
|
||||
return utils.Itoa(user.ID), nil
|
||||
|
||||
// 返回用户ID和IP地址的组合,用于在payloadFunc中设置JWT claims
|
||||
return map[string]interface{}{
|
||||
"user_id": utils.Itoa(user.ID),
|
||||
"ip": realip,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,14 +185,16 @@ func fallbackAuthMiddleware(mw *jwt.GinJWTMiddleware) func(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
realIP := c.GetString(model.CtxKeyRealIPStr)
|
||||
|
||||
c.Set("JWT_PAYLOAD", claims)
|
||||
identity := mw.IdentityHandler(c)
|
||||
|
||||
if identity != nil {
|
||||
model.UnblockIP(singleton.DB, c.GetString(model.CtxKeyRealIPStr), model.BlockIDToken)
|
||||
model.UnblockIP(singleton.DB, realIP, model.BlockIDToken)
|
||||
c.Set(mw.IdentityKey, identity)
|
||||
} else {
|
||||
if err := model.BlockIP(singleton.DB, c.GetString(model.CtxKeyRealIPStr), model.WAFBlockReasonTypeBruteForceToken, model.BlockIDToken); err != nil {
|
||||
if err := model.BlockIP(singleton.DB, realIP, model.WAFBlockReasonTypeBruteForceToken, model.BlockIDToken); err != nil {
|
||||
waf.ShowBlockPage(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
jwt "github.com/appleboy/gin-jwt/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPayloadFunc(t *testing.T) {
|
||||
payloadFn := payloadFunc()
|
||||
|
||||
// 测试包含IP的格式
|
||||
t.Run("format with IP", func(t *testing.T) {
|
||||
data := map[string]interface{}{
|
||||
"user_id": "123",
|
||||
"ip": "192.168.1.1",
|
||||
}
|
||||
claims := payloadFn(data)
|
||||
assert.Equal(t, "123", claims["user_id"])
|
||||
assert.Equal(t, "192.168.1.1", claims["ip"])
|
||||
})
|
||||
|
||||
// 测试不包含IP的格式
|
||||
t.Run("format without IP", func(t *testing.T) {
|
||||
data := map[string]interface{}{
|
||||
"user_id": "123",
|
||||
}
|
||||
claims := payloadFn(data)
|
||||
assert.Equal(t, "123", claims["user_id"])
|
||||
assert.Nil(t, claims["ip"])
|
||||
})
|
||||
|
||||
// 测试无效数据格式
|
||||
t.Run("invalid data format", func(t *testing.T) {
|
||||
claims := payloadFn("123") // 字符串类型不再支持
|
||||
assert.Empty(t, claims)
|
||||
})
|
||||
|
||||
// 测试空的map
|
||||
t.Run("empty map", func(t *testing.T) {
|
||||
data := map[string]interface{}{}
|
||||
claims := payloadFn(data)
|
||||
assert.Empty(t, claims)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIPBinding(t *testing.T) {
|
||||
// 创建测试用的gin context
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
t.Run("IP mismatch should invalidate token", func(t *testing.T) {
|
||||
// 模拟JWT claims包含IP绑定
|
||||
claims := jwt.MapClaims{
|
||||
"user_id": "123",
|
||||
"ip": "192.168.1.1",
|
||||
"exp": float64(time.Now().Add(time.Hour).Unix()),
|
||||
}
|
||||
|
||||
// 这里需要实际的数据库和用户设置来完全测试
|
||||
// 但可以测试claims的基本结构
|
||||
assert.Equal(t, "123", claims["user_id"])
|
||||
assert.Equal(t, "192.168.1.1", claims["ip"])
|
||||
})
|
||||
|
||||
t.Run("no IP in token should deny access", func(t *testing.T) {
|
||||
// 没有IP绑定的token应该被拒绝
|
||||
claims := jwt.MapClaims{
|
||||
"user_id": "123",
|
||||
"exp": float64(time.Now().Add(time.Hour).Unix()),
|
||||
}
|
||||
|
||||
// 验证token结构
|
||||
assert.Equal(t, "123", claims["user_id"])
|
||||
assert.Nil(t, claims["ip"])
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user