- v2 token format changed

- DisableUser
- clear all tokens when disable user
- handle ding ding user leave event
This commit is contained in:
gazebo
2019-03-09 20:59:18 +08:00
parent 4b362baea9
commit 2e65154206
6 changed files with 117 additions and 21 deletions

View File

@@ -10,6 +10,7 @@ import (
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/dchest/captcha"
@@ -24,6 +25,7 @@ const (
)
const (
TokenHeader = "TOKEN"
TokenVer = "V2"
TokenTypeSep = "."
TokenUserEmpty = "NULL"
@@ -37,8 +39,11 @@ const (
)
const (
DefTokenDuration = 7 * 24 * time.Hour // 7天
DefTokenDuration = 7 * 24 * time.Hour // 正式TOKEN7天有效期
TmpTokenDuration = 30 * time.Minute // 临时TOKEN30分钟有效期
MinCaptchaLen = 4
MaxCaptchaWidth = 400
MaxCaptchaHeight = 400
)
type IUser interface {
@@ -113,21 +118,23 @@ func RegisterAuther(authType string, handler IAuther) {
func createAuthInfo(user IUser, authBindInfo *AuthBindEx) (authInfo *AuthInfo) {
token, tokenType := createToken(user)
expireDuration := DefTokenDuration
authInfo = &AuthInfo{
AuthBindInfo: authBindInfo,
LoginTime: time.Now(),
ExpiresIn: time.Now().Add(DefTokenDuration).Unix(),
Token: token,
TokenType: tokenType,
LoginTime: time.Now(),
ExpiresIn: time.Now().Add(DefTokenDuration).Unix(),
Token: token,
TokenType: tokenType,
}
if user != nil {
authInfo.UpdateByIUser(user)
globals.SugarLogger.Debugf("createAuthInfo id:%s, id2:%s, mobile:%s, authInfo:%s", authInfo.GetID(), authInfo.GetID2(), authInfo.GetMobile(), utils.Format4Output(authInfo, true))
} else {
expireDuration = TmpTokenDuration
authInfo.ExpiresIn = time.Now().Add(expireDuration).Unix()
globals.SugarLogger.Debugf("createAuthInfo authInfo:%s", utils.Format4Output(authInfo, true))
}
SetUserInfo(token, authInfo, DefTokenDuration)
SetUserInfo(token, authInfo, expireDuration)
return authInfo
}
@@ -135,6 +142,12 @@ func CreateCaptcha(width, height, captchaLen int) (captchaInfo *CaptchaInfo, err
if captchaLen < MinCaptchaLen {
captchaLen = MinCaptchaLen
}
if width > MaxCaptchaWidth {
width = MaxCaptchaWidth
}
if height > MaxCaptchaHeight {
height = MaxCaptchaHeight
}
captchaInfo = &CaptchaInfo{
ID: captcha.NewLen(captchaLen),
}
@@ -283,6 +296,7 @@ func Logout(authInfo *AuthInfo) (err error) {
}
// token缓存相关
/////////////
func RemoveUserInfo(token string) {
api.Cacher.Del(token)
@@ -300,17 +314,36 @@ func SetUserInfo(token string, authInfo *AuthInfo, duration time.Duration) {
api.Cacher.Set(token, authInfo, DefTokenDuration)
}
func ClearUserToken(userID string) {
if keys, err := api.Cacher.Keys(strings.Join([]string{
TokenHeader,
TokenVer,
userID,
"*",
}, TokenTypeSep)); err == nil {
for _, key := range keys {
api.Cacher.Del(key)
}
}
}
/////////////
func createToken(user IUser) (token string, tokenType int) {
userID := TokenUserEmpty
userName := TokenUserEmpty
tokenType = TokenTypeOnlyAuth
if user != nil {
userID = "[" + user.GetID2() + "]"
userID = user.GetID()
userName = "[" + user.GetID2() + "]"
tokenType = TokenTypeNormal
}
return strings.Join([]string{
TokenHeader,
TokenVer,
time.Now().Format("20060102-150405"),
userID,
time.Now().Format("20060102-150405"),
userName,
utils.GetUUID(),
}, TokenTypeSep), tokenType
}
@@ -319,7 +352,7 @@ func GetTokenType(token string) (tokenType int) {
tokenType = TokenTypeNone
if token != "" {
tokenPartList := strings.Split(token, TokenTypeSep)
if (len(tokenPartList) == 1) || (len(tokenPartList) == 4 && tokenPartList[2] != TokenUserEmpty) {
if (len(tokenPartList) == 1) || (len(tokenPartList) == 6 && tokenPartList[2] != TokenUserEmpty) {
tokenType = TokenTypeNormal
} else {
tokenType = TokenTypeOnlyAuth
@@ -344,3 +377,14 @@ func GuessAuthTypeFromAuthID(authID string) (authType string) {
}
return AuthTypeNone
}
func DisableUser(userID, operatorUserName string) (err error) {
if _, err = dao.UpdateEntityLogically(dao.GetDB(), &model.AuthBind{}, map[string]interface{}{
"Status": model.AuthBindStatusDisabled,
}, operatorUserName, map[string]interface{}{
"UserID": userID,
}); err == nil {
ClearUserToken(userID)
}
return err
}