|
@@ -17,13 +17,14 @@ var ( |
|
@@ -17,13 +17,14 @@ var ( |
17
|
//MyToken ...
|
17
|
//MyToken ...
|
18
|
type MyToken struct {
|
18
|
type MyToken struct {
|
19
|
jwt.StandardClaims
|
19
|
jwt.StandardClaims
|
20
|
- UID int64 `json:"uid"`
|
|
|
21
|
- CompanyID int64 `json:"company_id"`
|
|
|
22
|
- UserCompanyId int64 `json:"user_company_id"`
|
20
|
+ UID int64 `json:"uid"`
|
|
|
21
|
+ CompanyID int64 `json:"company_id"`
|
|
|
22
|
+ UserCompanyId int64 `json:"user_company_id"`
|
|
|
23
|
+ Account string `json:"account"`
|
23
|
}
|
24
|
}
|
24
|
|
25
|
|
25
|
//CreateJWTToken ...
|
26
|
//CreateJWTToken ...
|
26
|
-func CreateJWTToken(uid int64, companyid int64, userCompanyId int64, expires int64) (string, error) {
|
27
|
+func CreateJWTToken(uid int64, companyid int64, userCompanyId int64, account string, expires int64) (string, error) {
|
27
|
nowTime := time.Now().Unix()
|
28
|
nowTime := time.Now().Unix()
|
28
|
claims := MyToken{
|
29
|
claims := MyToken{
|
29
|
StandardClaims: jwt.StandardClaims{
|
30
|
StandardClaims: jwt.StandardClaims{
|
|
@@ -35,6 +36,7 @@ func CreateJWTToken(uid int64, companyid int64, userCompanyId int64, expires int |
|
@@ -35,6 +36,7 @@ func CreateJWTToken(uid int64, companyid int64, userCompanyId int64, expires int |
35
|
UID: uid,
|
36
|
UID: uid,
|
36
|
CompanyID: companyid,
|
37
|
CompanyID: companyid,
|
37
|
UserCompanyId: userCompanyId,
|
38
|
UserCompanyId: userCompanyId,
|
|
|
39
|
+ Account: account,
|
38
|
}
|
40
|
}
|
39
|
|
41
|
|
40
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
42
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
@@ -60,8 +62,8 @@ func ValidJWTToken(tokenString string) (*MyToken, error) { |
|
@@ -60,8 +62,8 @@ func ValidJWTToken(tokenString string) (*MyToken, error) { |
60
|
log.Error("获取userCompany数据失败,id=%d", claims.UserCompanyId)
|
62
|
log.Error("获取userCompany数据失败,id=%d", claims.UserCompanyId)
|
61
|
return nil, fmt.Errorf("token Valid fail")
|
63
|
return nil, fmt.Errorf("token Valid fail")
|
62
|
}
|
64
|
}
|
63
|
- if userCompanyData.UserId != claims.UID {
|
|
|
64
|
- log.Error("登录信息uid与用户数据不匹配, userCompanyData.UserId=%d, claims.UID=%d", userCompanyData.UserId, claims.UID)
|
65
|
+ if userCompanyData.Phone != claims.Account {
|
|
|
66
|
+ log.Error("登录信息Account与用户数据不匹配, userCompanyData.Phone=%d, claims.Account=%d", userCompanyData.Phone, claims.Account)
|
65
|
return nil, fmt.Errorf("token Valid fail")
|
67
|
return nil, fmt.Errorf("token Valid fail")
|
66
|
}
|
68
|
}
|
67
|
return claims, nil
|
69
|
return claims, nil
|
|
@@ -79,7 +81,7 @@ func IsJwtErrorExpired(err error) bool { |
|
@@ -79,7 +81,7 @@ func IsJwtErrorExpired(err error) bool { |
79
|
return false
|
81
|
return false
|
80
|
}
|
82
|
}
|
81
|
|
83
|
|
82
|
-func GenerateAuthToken(uid int64, companyid int64, usercompanyid int64) (protocol.LoginAuthToken, error) {
|
84
|
+func GenerateAuthToken(uid int64, companyid int64, usercompanyid int64, account string) (protocol.LoginAuthToken, error) {
|
83
|
var (
|
85
|
var (
|
84
|
authToken protocol.LoginAuthToken
|
86
|
authToken protocol.LoginAuthToken
|
85
|
accesstoken string //主token,请求用
|
87
|
accesstoken string //主token,请求用
|
|
@@ -87,7 +89,7 @@ func GenerateAuthToken(uid int64, companyid int64, usercompanyid int64) (protoco |
|
@@ -87,7 +89,7 @@ func GenerateAuthToken(uid int64, companyid int64, usercompanyid int64) (protoco |
87
|
err error
|
89
|
err error
|
88
|
nowtime = time.Now()
|
90
|
nowtime = time.Now()
|
89
|
)
|
91
|
)
|
90
|
- accesstoken, err = CreateJWTToken(uid, companyid, usercompanyid, nowtime.Unix()+expiresIn+1)
|
92
|
+ accesstoken, err = CreateJWTToken(uid, companyid, usercompanyid, account, nowtime.Unix()+expiresIn+1)
|
91
|
if err != nil {
|
93
|
if err != nil {
|
92
|
return authToken, err
|
94
|
return authToken, err
|
93
|
}
|
95
|
}
|