...
|
...
|
@@ -3,12 +3,14 @@ package auth |
|
|
import (
|
|
|
"crypto/sha1"
|
|
|
"encoding/hex"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"oppmg/common/config"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/services/ucenter"
|
|
|
"oppmg/storage/redisdata"
|
|
|
"strings"
|
|
|
"time"
|
...
|
...
|
@@ -155,3 +157,102 @@ func ResetLoginTokenRedis(loginToken protocol.LoginAuthToken) error { |
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
func ChangeLoginToken(userid, companyid int64) (protocol.LoginAuthToken, error) {
|
|
|
var (
|
|
|
logintoken protocol.LoginAuthToken
|
|
|
err error
|
|
|
usercompany *models.UserCompany
|
|
|
companydata *models.Company
|
|
|
)
|
|
|
|
|
|
usercompany, err = models.GetUserCompanyBy(userid, companyid)
|
|
|
if err != nil {
|
|
|
log.Error("GetUserCompanyBy(userid, companyid) err:%s", err)
|
|
|
return logintoken, protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if ok := usercompany.IsEnable(); !ok {
|
|
|
log.Debug("公司禁用此用户")
|
|
|
return logintoken, protocol.NewErrWithMessage("10027")
|
|
|
}
|
|
|
if ok := usercompany.IsDelete(); !ok {
|
|
|
log.Debug("公司删除此用户")
|
|
|
return logintoken, protocol.NewErrWithMessage("10027")
|
|
|
}
|
|
|
companydata, err = models.GetCompanyById(usercompany.CompanyId)
|
|
|
if err != nil {
|
|
|
log.Error("GetCompanyById(%d) err:%s", usercompany.CompanyId, err)
|
|
|
return logintoken, protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if ok := companydata.IsEnable(); !ok {
|
|
|
log.Debug("无效公司")
|
|
|
return logintoken, protocol.NewErrWithMessage("10027")
|
|
|
}
|
|
|
logintoken, err = GenerateAuthToken(userid, companydata.Id)
|
|
|
if err != nil {
|
|
|
log.Error("GenerateAuthToken err:%s", err)
|
|
|
return logintoken, protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
return logintoken, nil
|
|
|
}
|
|
|
|
|
|
func RefreshLoginToken(refreshtoken string) (protocol.LoginAuthToken, error) {
|
|
|
var (
|
|
|
logintoken protocol.LoginAuthToken
|
|
|
mtoken *MyToken
|
|
|
err error
|
|
|
storetoken protocol.LoginAuthToken
|
|
|
)
|
|
|
mtoken, err = ValidJWTToken(refreshtoken)
|
|
|
if err != nil {
|
|
|
log.Debug("token失效 err:%s", err)
|
|
|
return logintoken, protocol.NewErrWithMessage("10024")
|
|
|
}
|
|
|
storetoken, err = redisdata.GetLoginToken(mtoken.UID)
|
|
|
if err != nil {
|
|
|
log.Error("redis err:%s", err)
|
|
|
return logintoken, protocol.NewErrWithMessage("10024")
|
|
|
}
|
|
|
if storetoken.RefreshToken != refreshtoken {
|
|
|
return logintoken, protocol.NewErrWithMessage("10024")
|
|
|
}
|
|
|
logintoken, _ = GenerateAuthToken(mtoken.UID, mtoken.CompanyID)
|
|
|
ResetLoginTokenRedis(logintoken)
|
|
|
return logintoken, nil
|
|
|
}
|
|
|
|
|
|
func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, error) {
|
|
|
var (
|
|
|
err error
|
|
|
logintoken protocol.LoginAuthToken
|
|
|
uclientReturn ucenter.ResponseLogin
|
|
|
)
|
|
|
|
|
|
_, err := models.GetUserByPhone(account)
|
|
|
if err != nil {
|
|
|
log.Debug("GetUserByPhone(%s) err:%s", account, err)
|
|
|
return logintoken, protocol.NewErrWithMessage("10021")
|
|
|
}
|
|
|
|
|
|
param := ucenter.RequesLogin{
|
|
|
Phone: account,
|
|
|
Password: password,
|
|
|
}
|
|
|
uclient := ucenter.NewUCenterClient()
|
|
|
btBody, err := uclient.Call(param)
|
|
|
if err != nil {
|
|
|
log.Error("统一用户中心请求失败 err:%s", err)
|
|
|
return logintoken, protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
err = json.Unmarshal(btBody, &uclientReturn)
|
|
|
if err != nil {
|
|
|
log.Error("解析统一用户中心响应失败 err:%s", err)
|
|
|
return logintoken, protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
if !(uclientReturn.Code == ucenter.ResponseCode0 &&
|
|
|
uclientReturn.Msg == ucenter.ResponseMsgOk) {
|
|
|
return logintoken, protocol.NewErrWithMessage("10021")
|
|
|
}
|
|
|
|
|
|
return logintoken, err
|
|
|
} |
...
|
...
|
|