- auth2添加固定TOKEN

This commit is contained in:
gazebo
2019-08-21 09:17:28 +08:00
parent bf3e825f11
commit c5f859c92e

View File

@@ -88,6 +88,20 @@ var (
AuthTypeEmail: regexp.MustCompile(`^[A-Za-z0-9_\-\.]+\@[A-Za-z0-9_\-]+(\.[A-Za-z]+){1,5}$`), AuthTypeEmail: regexp.MustCompile(`^[A-Za-z0-9_\-\.]+\@[A-Za-z0-9_\-]+(\.[A-Za-z]+){1,5}$`),
AuthTypeMobile: regexp.MustCompile(`^1[345789]\d{9}$`), AuthTypeMobile: regexp.MustCompile(`^1[345789]\d{9}$`),
} }
// 永久全局的TOKEN内部使用
fixedTokenMap = map[string]*AuthInfo{
"85e60d6a-f1f8-4837-9c7d-d7072b0ba1c6": &AuthInfo{
UserBasic: UserBasic{
UserID2: "shifengfix",
Mobile: "18048531223",
Name: "石峰固定",
},
AuthBindInfo: &AuthBindEx{},
LoginTime: utils.ZeroTimeValue,
ExpiresAt: 0,
},
}
) )
var ( var (
@@ -106,6 +120,16 @@ var (
func init() { func init() {
authers = make(map[string]IAuther) authers = make(map[string]IAuther)
InitFixedToken()
}
func InitFixedToken() {
for k, v := range fixedTokenMap {
v.UserID = k
v.Token = k
v.TokenType = TokenTypeNormal
}
} }
func Init(userProvider2 IUserProvider) { func Init(userProvider2 IUserProvider) {
@@ -116,6 +140,10 @@ func RegisterAuther(authType string, handler IAuther) {
authers[authType] = handler authers[authType] = handler
} }
func getFixedTokenName(token string) *AuthInfo {
return fixedTokenMap[token]
}
func createAuthInfo(user IUser, authBindInfo *AuthBindEx) (authInfo *AuthInfo) { func createAuthInfo(user IUser, authBindInfo *AuthBindEx) (authInfo *AuthInfo) {
token, tokenType := createToken(user) token, tokenType := createToken(user)
expireDuration := DefTokenDuration expireDuration := DefTokenDuration
@@ -303,6 +331,9 @@ func RemoveUserInfo(token string) {
} }
func GetTokenInfo(token string) (authInfo *AuthInfo, err error) { func GetTokenInfo(token string) (authInfo *AuthInfo, err error) {
if authInfo = getFixedTokenName(token); authInfo != nil {
return authInfo, nil
}
if err = api.Cacher.GetAs(token, &authInfo); err == nil { if err = api.Cacher.GetAs(token, &authInfo); err == nil {
return authInfo, nil return authInfo, nil
} }
@@ -348,6 +379,9 @@ func createToken(user IUser) (token string, tokenType int) {
} }
func GetTokenType(token string) (tokenType int) { func GetTokenType(token string) (tokenType int) {
if getFixedTokenName(token) != nil {
return TokenTypeNormal
}
tokenType = TokenTypeNone tokenType = TokenTypeNone
if token != "" { if token != "" {
tokenPartList := strings.Split(token, TokenTypeSep) tokenPartList := strings.Split(token, TokenTypeSep)
@@ -361,6 +395,9 @@ func GetTokenType(token string) (tokenType int) {
} }
func IsV2Token(token string) bool { func IsV2Token(token string) bool {
if getFixedTokenName(token) != nil {
return true
}
tokenPartList := strings.Split(token, TokenTypeSep) tokenPartList := strings.Split(token, TokenTypeSep)
return len(tokenPartList) > 1 && tokenPartList[1] == TokenVer return len(tokenPartList) > 1 && tokenPartList[1] == TokenVer
} }