作者 tangxuhui

增加验证。变更手机号将后,判定token失效

@@ -2,6 +2,8 @@ package auth @@ -2,6 +2,8 @@ package auth
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "oppmg/common/log"
  6 + "oppmg/models"
5 "oppmg/protocol" 7 "oppmg/protocol"
6 "time" 8 "time"
7 9
@@ -50,12 +52,22 @@ func ValidJWTToken(tokenString string) (*MyToken, error) { @@ -50,12 +52,22 @@ func ValidJWTToken(tokenString string) (*MyToken, error) {
50 if err != nil { 52 if err != nil {
51 return nil, err 53 return nil, err
52 } 54 }
53 - if claims, ok := token.Claims.(*MyToken); ok && token.Valid {  
54 - // 验证成功,返回信息  
55 - return claims, nil 55 + claims, ok := token.Claims.(*MyToken)
  56 + if !(ok && token.Valid) {
  57 + // 验证失败,返回信息
  58 + return nil, fmt.Errorf("token Valid fail")
56 } 59 }
57 - // 验证失败  
58 - return nil, fmt.Errorf("token Valid fail") 60 + userCompanyData, err := models.GetUserCompanyById(claims.UserCompanyId)
  61 + if err != nil {
  62 + log.Error("获取userCompany数据失败,id=%d", claims.UserCompanyId)
  63 + return nil, fmt.Errorf("token Valid fail")
  64 + }
  65 + if userCompanyData.UserId != claims.UID {
  66 + log.Error("登录信息uid与用户数据不匹配, userCompanyData.UserId=%d, claims.UID=%d", userCompanyData.UserId, claims.UID)
  67 + return nil, fmt.Errorf("token Valid fail")
  68 + }
  69 + // 验证成功
  70 + return claims, nil
59 } 71 }
60 72
61 func IsJwtErrorExpired(err error) bool { 73 func IsJwtErrorExpired(err error) bool {