by Robin at 20240807

This commit is contained in:
Leufolium 2024-08-07 22:22:32 +08:00
parent 68be863a92
commit d33f3f212a
9 changed files with 55 additions and 55 deletions

View File

@ -91,7 +91,8 @@ type ApiLoginResp struct {
type ApiLogoutReq struct { type ApiLogoutReq struct {
base.BaseRequest base.BaseRequest
Mid *int64 `json:"mid"` // mid TokenUuid int64
Mid *int64 `json:"mid"` // mid
} }
type ApiLogoutData struct { type ApiLogoutData struct {

View File

@ -105,7 +105,8 @@ type OpLoginResp struct {
type OpLogoutReq struct { type OpLogoutReq struct {
base.BaseRequest base.BaseRequest
Mid *int64 `json:"mid"` // mid TokenUuid int64
Mid *int64 `json:"mid"` // mid
} }
type OpLogoutData struct { type OpLogoutData struct {

View File

@ -94,6 +94,7 @@ func ApiLoginByVeriCode(ctx *gin.Context) {
func ApiLogout(ctx *gin.Context) { func ApiLogout(ctx *gin.Context) {
req := ctx.MustGet("client_req").(*loginproto.ApiLogoutReq) req := ctx.MustGet("client_req").(*loginproto.ApiLogoutReq)
req.TokenUuid = ctx.MustGet("token_uuid").(int64)
logoutData, ec := service.DefaultService.ApiLogout(ctx, req) logoutData, ec := service.DefaultService.ApiLogout(ctx, req)
if ec != errcode.ErrCodeLoginSrvOk { if ec != errcode.ErrCodeLoginSrvOk {
logger.Error("ApiLogout fail, req: %v, ec: %v", util.ToJson(req), ec) logger.Error("ApiLogout fail, req: %v, ec: %v", util.ToJson(req), ec)

View File

@ -95,6 +95,7 @@ func OpLoginByVeriCode(ctx *gin.Context) {
func OpLogout(ctx *gin.Context) { func OpLogout(ctx *gin.Context) {
req := ctx.MustGet("client_req").(*loginproto.OpLogoutReq) req := ctx.MustGet("client_req").(*loginproto.OpLogoutReq)
req.TokenUuid = ctx.MustGet("token_uuid").(int64)
logoutData, ec := service.DefaultService.OpLogout(ctx, req) logoutData, ec := service.DefaultService.OpLogout(ctx, req)
if ec != errcode.ErrCodeLoginSrvOk { if ec != errcode.ErrCodeLoginSrvOk {
logger.Error("OpLogout fail, req: %v, ec: %v", util.ToJson(req), ec) logger.Error("OpLogout fail, req: %v, ec: %v", util.ToJson(req), ec)

View File

@ -100,10 +100,9 @@ func (s *Service) ApiLoginByPswd(ctx *gin.Context, req *loginproto.ApiLoginByPsw
// 2.让已登录的用户强制下线 // 2.让已登录的用户强制下线
if account.GetRole() == consts.User { if account.GetRole() == consts.User {
if _, ec = s.ApiLogout(ctx, &loginproto.ApiLogoutReq{ if err := s.utilLogoutAll(ctx, account.GetMid()); err != nil {
Mid: account.Mid, logger.Error("utilLogoutAll failed, err: %v", err)
}); ec != errcode.ErrCodeLoginSrvOk { ec = errcode.ErrCodeLoginSrvOk
logger.Error("ApiLogout failed!")
return return
} }
} }
@ -175,10 +174,9 @@ func (s *Service) ApiLoginByVeriCode(ctx *gin.Context, req *loginproto.ApiLoginB
// 4.让已登录的用户强制下线 // 4.让已登录的用户强制下线
if account.GetRole() == consts.User { if account.GetRole() == consts.User {
if _, ec = s.ApiLogout(ctx, &loginproto.ApiLogoutReq{ if err := s.utilLogoutAll(ctx, account.GetMid()); err != nil {
Mid: account.Mid, logger.Error("utilLogoutAll failed, err: %v", err)
}); ec != errcode.ErrCodeLoginSrvOk { ec = errcode.ErrCodeLoginSrvOk
logger.Error("ApiLogout failed!")
return return
} }
} }
@ -218,8 +216,8 @@ func (s *Service) ApiLogout(ctx *gin.Context, req *loginproto.ApiLogoutReq) (log
ec = errcode.ErrCodeLoginSrvOk ec = errcode.ErrCodeLoginSrvOk
if err := _DefaultToken.OpDeleteByMid(ctx, util.DerefInt64(req.Mid)); err != nil && err != qmgo.ErrNoSuchDocuments { if err := _DefaultToken.OpDelete(ctx, req.TokenUuid); err != nil && err != qmgo.ErrNoSuchDocuments {
logger.Error("ApiDeleteByMid failed, err: %v", err) logger.Error("OpDelete failed, err: %v", err)
logoutData = &loginproto.OpLogoutData{ logoutData = &loginproto.OpLogoutData{
OpResult: false, OpResult: false,
} }
@ -227,18 +225,6 @@ func (s *Service) ApiLogout(ctx *gin.Context, req *loginproto.ApiLogoutReq) (log
return return
} }
// 更新登录状态
if err := _DefaultLogin.OpUpdateByMid(ctx, &loginproto.OpUpdateByMidReq{
Login: &dbstruct.Login{
IsLogined: goproto.Int64(0),
},
Mid: req.Mid,
}); err != nil {
ec = errcode.ErrCodeLoginSrvFail
logger.Error("ApiUpdateByMid failed, err: %v", err)
return
}
logoutData = &loginproto.OpLogoutData{ logoutData = &loginproto.OpLogoutData{
OpResult: true, OpResult: true,
} }

View File

@ -103,29 +103,29 @@ func (p *Token) OpGenerate(ctx *gin.Context, req *tokenproto.OpCreateReq) (token
return return
} }
func (p *Token) OpVerify(ctx *gin.Context, tokenString string) error { func (p *Token) OpVerify(ctx *gin.Context, tokenString string) (int64, error) {
//是否携带令牌 //是否携带令牌
if tokenString == "" { if tokenString == "" {
logger.Error("Missing auth token") logger.Error("Missing auth token")
return fmt.Errorf("missing auth token") return -1, fmt.Errorf("missing auth token")
} }
//检查令牌加密方法及签名 //检查令牌加密方法及签名
token, err := p.OpVerifyCrypto(ctx, tokenString) token, err := p.OpVerifyCrypto(ctx, tokenString)
if err != nil { if err != nil {
logger.Error("OpVerifyCrypto failed") logger.Error("OpVerifyCrypto failed")
return err return -1, err
} }
//检查数据库中是否还存在该token是否还有效 //检查数据库中是否还存在该token是否还有效
err = p.OpVerifyValid(ctx, token) id, err := p.OpVerifyValid(ctx, token)
if err != nil { if err != nil {
logger.Error("OpVerifyValid failed") logger.Error("OpVerifyValid failed")
return err return -1, err
} }
return nil return id, nil
} }
// 检查令牌加密方法及签名 // 检查令牌加密方法及签名
@ -144,29 +144,29 @@ func (p *Token) OpVerifyCrypto(ctx *gin.Context, tokenString string) (token *jwt
} }
// 检查该token是否还有效 // 检查该token是否还有效
func (p *Token) OpVerifyValid(ctx *gin.Context, token *jwt.Token) error { func (p *Token) OpVerifyValid(ctx *gin.Context, token *jwt.Token) (int64, error) {
claims, ok := token.Claims.(jwt.MapClaims) claims, ok := token.Claims.(jwt.MapClaims)
if !ok { if !ok {
return fmt.Errorf("Token type assertion failed") return -1, fmt.Errorf("Token type assertion failed")
} }
if !token.Valid { if !token.Valid {
return fmt.Errorf("Token is invalid") return -1, fmt.Errorf("Token is invalid")
} }
tokenUuid, err := strconv.ParseInt(fmt.Sprintf("%.f", claims["token_uuid"]), 10, 64) tokenUuid, err := strconv.ParseInt(fmt.Sprintf("%.f", claims["token_uuid"]), 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("failed to acquire token_uuid from token") return -1, fmt.Errorf("failed to acquire token_uuid from token")
} }
list, err := p.OpList(ctx, &tokenproto.OpListReq{ list, err := p.OpList(ctx, &tokenproto.OpListReq{
Id: tokenUuid, Id: tokenUuid,
}) })
if err != nil { if err != nil {
return fmt.Errorf("OpList failed") return -1, fmt.Errorf("OpList failed")
} }
if len(list) == 0 { if len(list) == 0 {
return fmt.Errorf("登录失效,请重新登录!") return -1, fmt.Errorf("登录失效,请重新登录!")
} }
return nil return tokenUuid, nil
} }

View File

@ -713,8 +713,8 @@ func (s *Service) OpLogout(ctx *gin.Context, req *loginproto.OpLogoutReq) (logou
ec = errcode.ErrCodeLoginSrvOk ec = errcode.ErrCodeLoginSrvOk
if err := _DefaultToken.OpDeleteByMid(ctx, util.DerefInt64(req.Mid)); err != nil && err != qmgo.ErrNoSuchDocuments { if err := _DefaultToken.OpDelete(ctx, req.TokenUuid); err != nil && err != qmgo.ErrNoSuchDocuments {
logger.Error("OpDeleteByMid failed, err: %v", err) logger.Error("ApiDeleteByMid failed, err: %v", err)
logoutData = &loginproto.OpLogoutData{ logoutData = &loginproto.OpLogoutData{
OpResult: false, OpResult: false,
} }
@ -722,18 +722,6 @@ func (s *Service) OpLogout(ctx *gin.Context, req *loginproto.OpLogoutReq) (logou
return return
} }
// 更新登录状态
if err := _DefaultLogin.OpUpdateByMid(ctx, &loginproto.OpUpdateByMidReq{
Login: &dbstruct.Login{
IsLogined: goproto.Int64(0),
},
Mid: req.Mid,
}); err != nil {
ec = errcode.ErrCodeLoginSrvFail
logger.Error("OpUpdateByMid failed, err: %v", err)
return
}
logoutData = &loginproto.OpLogoutData{ logoutData = &loginproto.OpLogoutData{
OpResult: true, OpResult: true,
} }
@ -1034,8 +1022,8 @@ func (s *Service) OpGetAccountCount(ctx *gin.Context, req *accountproto.OpCountR
} }
// Token // Token
func (s *Service) OpVerifyToken(ctx *gin.Context, token string) (err error) { func (s *Service) OpVerifyToken(ctx *gin.Context, token string) (id int64, err error) {
if err = _DefaultToken.OpVerify(ctx, token); err != nil { if id, err = _DefaultToken.OpVerify(ctx, token); err != nil {
logger.Error("OpVerifyToken fail, err :%v", err) logger.Error("OpVerifyToken fail, err :%v", err)
return return
} }

View File

@ -2061,3 +2061,24 @@ func (s *Service) utilSignHvyogoMessage(msg interfaces.HvyogoSignable) ([]byte,
return resultBytes, nil return resultBytes, nil
} }
func (s *Service) utilLogoutAll(ctx *gin.Context, mid int64) (err error) {
if err = _DefaultToken.OpDeleteByMid(ctx, mid); err != nil && err != qmgo.ErrNoSuchDocuments {
logger.Error("ApiDeleteByMid failed, err: %v", err)
return
}
// 更新登录状态
if err = _DefaultLogin.OpUpdateByMid(ctx, &loginproto.OpUpdateByMidReq{
Login: &dbstruct.Login{
IsLogined: goproto.Int64(0),
},
Mid: goproto.Int64(mid),
}); err != nil {
logger.Error("ApiUpdateByMid failed, err: %v", err)
return
}
return
}

View File

@ -12,7 +12,7 @@ var (
DefaultJwtAuthenticator gin.HandlerFunc DefaultJwtAuthenticator gin.HandlerFunc
) )
func InitJwtAuthenticator(verifyFunc func(*gin.Context, string) error) { func InitJwtAuthenticator(verifyFunc func(*gin.Context, string) (int64, error)) {
DefaultJwtAuthenticator = func(ctx *gin.Context) { DefaultJwtAuthenticator = func(ctx *gin.Context) {
//1.获取token //1.获取token
@ -25,11 +25,12 @@ func InitJwtAuthenticator(verifyFunc func(*gin.Context, string) error) {
logger.Info("token : %v", tokenString) logger.Info("token : %v", tokenString)
//2.校验 //2.校验
err := verifyFunc(ctx, tokenString) id, err := verifyFunc(ctx, tokenString)
if !base.CheckBadRequest(ctx, err) { if !base.CheckBadRequest(ctx, err) {
return return
} }
ctx.Set("token_uuid", id)
ctx.Next() ctx.Next()
} }
} }