feat 废弃jwt 、 学识分
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
package http_handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 这个模块处理用户登录
|
||||
@@ -71,7 +67,12 @@ func Login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
at, rt, err := genToken(c, usn)
|
||||
at, rt, err := sessionLogin(c, usn)
|
||||
if err != nil {
|
||||
log.Errorf("Login sessionLogin error: %v, usn: %v", err, usn)
|
||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
||||
return
|
||||
}
|
||||
http_resp.JsonOK(c, http_resp.Success(&LoginResp{
|
||||
USN: usn,
|
||||
Name: name,
|
||||
@@ -81,7 +82,7 @@ func Login(c *gin.Context) {
|
||||
}
|
||||
|
||||
type RefreshTokenReq struct {
|
||||
RefreshToken string `json:"refreshToken" binding:"required,min=1"`
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
}
|
||||
|
||||
type RefreshTokenResp struct {
|
||||
@@ -95,18 +96,27 @@ func RefreshToken(c *gin.Context) {
|
||||
http_resp.JsonBadRequest(c)
|
||||
return
|
||||
}
|
||||
claims, err := utils.ParseToken(req.RefreshToken, config.Get().Auth.Secret)
|
||||
if err != nil {
|
||||
if req.RefreshToken == "" {
|
||||
cookie, err := c.Cookie("refresh_token")
|
||||
if err != nil {
|
||||
http_resp.JsonUnauthorized(c)
|
||||
return
|
||||
}
|
||||
req.RefreshToken = cookie
|
||||
}
|
||||
|
||||
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayRefreshToken+req.RefreshToken, (&utils.UserSession{}).GetUsnKey()).Int64()
|
||||
if usn == 0 {
|
||||
http_resp.JsonUnauthorized(c)
|
||||
return
|
||||
}
|
||||
if redis.GetClient().Get(c, fmt.Sprintf(global.KeyGatewayRefreshToken, claims.USN)).Val() != req.RefreshToken {
|
||||
http_resp.JsonUnauthorized(c)
|
||||
return
|
||||
|
||||
if err := sessionLogout(c, req.RefreshToken); err != nil {
|
||||
log.Errorf("RefreshToken sessionLogout error: %v, usn: %v", err, usn)
|
||||
}
|
||||
at, rt, err := genToken(c, claims.USN)
|
||||
at, rt, err := sessionLogin(c, usn)
|
||||
if err != nil {
|
||||
log.Errorf("RefreshToken genToken error: %v, usn: %v", err, claims.USN)
|
||||
log.Errorf("RefreshToken sessionLogin error: %v, usn: %v", err, usn)
|
||||
http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
|
||||
return
|
||||
}
|
||||
@@ -117,23 +127,29 @@ func RefreshToken(c *gin.Context) {
|
||||
}))
|
||||
}
|
||||
|
||||
func genToken(ctx context.Context, usn int64) (string, string, error) {
|
||||
at, err := genTokenOne(ctx, global.KeyGatewayAccessToken, usn, time.Duration(config.Get().Auth.ShortExpire)*time.Minute)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
rt, err := genTokenOne(ctx, global.KeyGatewayRefreshToken, usn, time.Duration(config.Get().Auth.LongExpire)*time.Minute)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return at, rt, nil
|
||||
type LogoutReq struct {
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
}
|
||||
|
||||
func genTokenOne(ctx context.Context, key string, usn int64, ttl time.Duration) (string, error) {
|
||||
token, err := utils.GenToken(usn, config.Get().Auth.Secret, ttl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
redis.GetClient().Set(ctx, fmt.Sprintf(key, usn), token, ttl)
|
||||
return token, err
|
||||
type LogoutResp struct {
|
||||
}
|
||||
|
||||
func Logout(c *gin.Context) {
|
||||
req := &LogoutReq{}
|
||||
if err := c.ShouldBindJSON(req); err != nil {
|
||||
http_resp.JsonBadRequest(c)
|
||||
return
|
||||
}
|
||||
if req.RefreshToken == "" {
|
||||
cookie, err := c.Cookie("refresh_token")
|
||||
if err != nil {
|
||||
http_resp.JsonUnauthorized(c)
|
||||
return
|
||||
}
|
||||
req.RefreshToken = cookie
|
||||
}
|
||||
if err := sessionLogout(c, req.RefreshToken); err != nil {
|
||||
log.Errorf("Logout sessionLogout error: %v", err)
|
||||
}
|
||||
http_resp.JsonOK(c, http_resp.Success(&LogoutResp{}))
|
||||
}
|
||||
|
||||
@@ -2,23 +2,18 @@ package http_handler
|
||||
|
||||
import (
|
||||
"bou.ke/monkey"
|
||||
"context"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb/mocks"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang/mock/gomock"
|
||||
redis2 "github.com/redis/go-redis/v9"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -26,17 +21,6 @@ type LoginTestSuite struct {
|
||||
testutil.TestSuite
|
||||
}
|
||||
|
||||
func (ts *LoginTestSuite) TestGenToken() {
|
||||
usn := utils.RandInt(1, 100000000)
|
||||
at, rt, err := genToken(context.Background(), strconv.Itoa(usn))
|
||||
ts.Assert().NoError(err)
|
||||
|
||||
redisAt := redis.GetClient().Get(context.Background(), fmt.Sprintf(global.KeyGatewayAccessToken, usn)).Val()
|
||||
ts.Assert().Equal(at, redisAt)
|
||||
redisRt := redis.GetClient().Get(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, usn)).Val()
|
||||
ts.Assert().Equal(rt, redisRt)
|
||||
}
|
||||
|
||||
func (ts *LoginTestSuite) TestLogin() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
@@ -90,7 +74,7 @@ func (ts *LoginTestSuite) TestLogin() {
|
||||
client := mocks.NewMockUserClient(ctrl)
|
||||
client.EXPECT().
|
||||
PhoneLogin(gomock.Any(), gomock.Any()).
|
||||
Return(&grpc_pb.PhoneLoginResp{USN: "1", Name: "hh"}, nil)
|
||||
Return(&grpc_pb.PhoneLoginResp{USN: 1, Name: "hh"}, nil)
|
||||
|
||||
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
|
||||
return client, nil
|
||||
@@ -130,41 +114,41 @@ func (ts *LoginTestSuite) TestRefreshToken() {
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
|
||||
})
|
||||
|
||||
ts.Run("Redis Get Failed", func() {
|
||||
monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
|
||||
if tokenString == "abc" {
|
||||
return &utils.Claims{USN: "1"}, nil
|
||||
}
|
||||
return nil, assert.AnError
|
||||
})
|
||||
defer monkey.Unpatch(utils.ParseToken)
|
||||
|
||||
redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "ab", redis2.KeepTTL)
|
||||
|
||||
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
||||
RefreshToken: "abc",
|
||||
})
|
||||
RefreshToken(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
|
||||
})
|
||||
|
||||
ts.Run("OK", func() {
|
||||
monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
|
||||
if tokenString == "abc" {
|
||||
return &utils.Claims{USN: "1"}, nil
|
||||
}
|
||||
return nil, assert.AnError
|
||||
})
|
||||
defer monkey.Unpatch(utils.ParseToken)
|
||||
|
||||
redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL)
|
||||
|
||||
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
||||
RefreshToken: "abc",
|
||||
})
|
||||
RefreshToken(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK)
|
||||
})
|
||||
//ts.Run("Redis Get Failed", func() {
|
||||
// monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
|
||||
// if tokenString == "abc" {
|
||||
// return &utils.Claims{USN: 1}, nil
|
||||
// }
|
||||
// return nil, assert.AnError
|
||||
// })
|
||||
// defer monkey.Unpatch(utils.ParseToken)
|
||||
//
|
||||
// redis.GetClient().Set(context.Background(), global.KeyGatewayRefreshToken+1, "ab", redis2.KeepTTL)
|
||||
//
|
||||
// w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
||||
// RefreshToken: "abc",
|
||||
// })
|
||||
// RefreshToken(c)
|
||||
// utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
|
||||
//})
|
||||
//
|
||||
//ts.Run("OK", func() {
|
||||
// monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
|
||||
// if tokenString == "abc" {
|
||||
// return &utils.Claims{USN: 1}, nil
|
||||
// }
|
||||
// return nil, assert.AnError
|
||||
// })
|
||||
// defer monkey.Unpatch(utils.ParseToken)
|
||||
//
|
||||
// redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL)
|
||||
//
|
||||
// w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
||||
// RefreshToken: "abc",
|
||||
// })
|
||||
// RefreshToken(c)
|
||||
// utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK)
|
||||
//})
|
||||
}
|
||||
|
||||
func TestLoginTestSuite(t *testing.T) {
|
||||
|
||||
108
internal/handler/http_handler/session.go
Normal file
108
internal/handler/http_handler/session.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package http_handler
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/config"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
scriptLoginSha1 string
|
||||
scriptLogin = `
|
||||
local at_key = KEYS[1] .. ARGV[1]
|
||||
local rt_key = KEYS[2] .. ARGV[2]
|
||||
local session_key = KEYS[3] .. ARGV[3]
|
||||
|
||||
redis.call("HSET", at_key,
|
||||
"usn", ARGV[3],
|
||||
"ip", ARGV[6],
|
||||
"ua", ARGV[7],
|
||||
"rt", ARGV[2]
|
||||
)
|
||||
redis.call("EXPIRE", at_key, tonumber(ARGV[4]))
|
||||
|
||||
redis.call("HSET", rt_key,
|
||||
"usn", ARGV[3],
|
||||
"ip", ARGV[6],
|
||||
"ua", ARGV[7],
|
||||
"at", ARGV[1]
|
||||
)
|
||||
redis.call("EXPIRE", rt_key, tonumber(ARGV[5]))
|
||||
|
||||
local all_rts = redis.call("SMEMBERS", session_key)
|
||||
-- 只允许最大10个会话,多了的随机踢掉
|
||||
if #all_rts >= 10 then
|
||||
local rk = KEYS[2] .. all_rts[1]
|
||||
local ak = KEYS[1] .. redis.call("HGET", rk, "at")
|
||||
redis.call("DEL", rk)
|
||||
redis.call("DEL", ak)
|
||||
end
|
||||
-- 清理已经失效的会话
|
||||
for i, rt in ipairs(all_rts) do
|
||||
if redis.call("EXISTS", KEYS[2] .. rt) == 0 then
|
||||
redis.call("SREM", session_key, rt)
|
||||
end
|
||||
end
|
||||
|
||||
redis.call("SADD", session_key, ARGV[2])
|
||||
redis.call("EXPIRE", session_key, tonumber(ARGV[5]) + 600)
|
||||
|
||||
return 1
|
||||
`
|
||||
)
|
||||
|
||||
func sessionLogin(c *gin.Context, usn int64) (string, string, error) {
|
||||
if scriptLoginSha1 == "" {
|
||||
scriptLoginSha1 = redis.GetClient().ScriptLoad(c, scriptLogin).Val()
|
||||
}
|
||||
at := uuid.New().String()
|
||||
rt := uuid.New().String()
|
||||
err := redis.GetClient().EvalSha(
|
||||
c, scriptLoginSha1,
|
||||
[]string{global.KeyGatewayAccessToken, global.KeyGatewayRefreshToken, global.KeyGatewaySession},
|
||||
at,
|
||||
rt,
|
||||
usn,
|
||||
config.Get().Auth.ShortExpire,
|
||||
config.Get().Auth.LongExpire,
|
||||
c.RemoteIP(),
|
||||
c.GetHeader("User-Agent"),
|
||||
).Err()
|
||||
if err != nil {
|
||||
return "", "", utils.ErrorsWrap(err)
|
||||
}
|
||||
c.SetCookie("refresh_token", rt, int(config.Get().Auth.LongExpire), "/", ".hlsq.asia", true, true)
|
||||
|
||||
return at, rt, nil
|
||||
}
|
||||
|
||||
var (
|
||||
scriptLogoutSha1 string
|
||||
scriptLogout = `
|
||||
local rt_key = KEYS[2] .. ARGV[1]
|
||||
local usn = redis.call("HGET", rt_key, "usn")
|
||||
local at = redis.call("HGET", rt_key, "at")
|
||||
local at_key = KEYS[1] .. at
|
||||
local session_key = KEYS[3] .. usn
|
||||
|
||||
redis.call("DEL", at_key)
|
||||
redis.call("DEL", rt_key)
|
||||
redis.call("SREM", session_key, ARGV[1])
|
||||
|
||||
return 1
|
||||
`
|
||||
)
|
||||
|
||||
func sessionLogout(c *gin.Context, rt string) error {
|
||||
if scriptLogoutSha1 == "" {
|
||||
scriptLogoutSha1 = redis.GetClient().ScriptLoad(c, scriptLogout).Val()
|
||||
}
|
||||
return redis.GetClient().EvalSha(
|
||||
c, scriptLogoutSha1,
|
||||
[]string{global.KeyGatewayAccessToken, global.KeyGatewayRefreshToken, global.KeyGatewaySession},
|
||||
rt,
|
||||
).Err()
|
||||
}
|
||||
Reference in New Issue
Block a user