...
|
...
|
@@ -17,13 +17,14 @@ var ( |
|
|
//MyToken ...
|
|
|
type MyToken struct {
|
|
|
jwt.StandardClaims
|
|
|
UID int64 `json:"uid"`
|
|
|
CompanyID int64 `json:"company_id"`
|
|
|
UserCompanyId int64 `json:"user_company_id"`
|
|
|
UID int64 `json:"uid"`
|
|
|
CompanyID int64 `json:"company_id"`
|
|
|
UserCompanyId int64 `json:"user_company_id"`
|
|
|
Account string `json:"account"`
|
|
|
}
|
|
|
|
|
|
//CreateJWTToken ...
|
|
|
func CreateJWTToken(uid int64, companyid int64, userCompanyId int64, expires int64) (string, error) {
|
|
|
func CreateJWTToken(uid int64, companyid int64, userCompanyId int64, account string, expires int64) (string, error) {
|
|
|
nowTime := time.Now().Unix()
|
|
|
claims := MyToken{
|
|
|
StandardClaims: jwt.StandardClaims{
|
...
|
...
|
@@ -35,6 +36,7 @@ func CreateJWTToken(uid int64, companyid int64, userCompanyId int64, expires int |
|
|
UID: uid,
|
|
|
CompanyID: companyid,
|
|
|
UserCompanyId: userCompanyId,
|
|
|
Account: account,
|
|
|
}
|
|
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
...
|
...
|
@@ -60,8 +62,8 @@ func ValidJWTToken(tokenString string) (*MyToken, error) { |
|
|
log.Error("获取userCompany数据失败,id=%d", claims.UserCompanyId)
|
|
|
return nil, fmt.Errorf("token Valid fail")
|
|
|
}
|
|
|
if userCompanyData.UserId != claims.UID {
|
|
|
log.Error("登录信息uid与用户数据不匹配, userCompanyData.UserId=%d, claims.UID=%d", userCompanyData.UserId, claims.UID)
|
|
|
if userCompanyData.Phone != claims.Account {
|
|
|
log.Error("登录信息Account与用户数据不匹配, userCompanyData.Phone=%d, claims.Account=%d", userCompanyData.Phone, claims.Account)
|
|
|
return nil, fmt.Errorf("token Valid fail")
|
|
|
}
|
|
|
return claims, nil
|
...
|
...
|
@@ -79,7 +81,7 @@ func IsJwtErrorExpired(err error) bool { |
|
|
return false
|
|
|
}
|
|
|
|
|
|
func GenerateAuthToken(uid int64, companyid int64, usercompanyid int64) (protocol.LoginAuthToken, error) {
|
|
|
func GenerateAuthToken(uid int64, companyid int64, usercompanyid int64, account string) (protocol.LoginAuthToken, error) {
|
|
|
var (
|
|
|
authToken protocol.LoginAuthToken
|
|
|
accesstoken string //主token,请求用
|
...
|
...
|
@@ -87,7 +89,7 @@ func GenerateAuthToken(uid int64, companyid int64, usercompanyid int64) (protoco |
|
|
err error
|
|
|
nowtime = time.Now()
|
|
|
)
|
|
|
accesstoken, err = CreateJWTToken(uid, companyid, usercompanyid, nowtime.Unix()+expiresIn+1)
|
|
|
accesstoken, err = CreateJWTToken(uid, companyid, usercompanyid, account, nowtime.Unix()+expiresIn+1)
|
|
|
if err != nil {
|
|
|
return authToken, err
|
|
|
}
|
...
|
...
|
|