- user2
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package auth2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -10,10 +12,11 @@ import (
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"github.com/dchest/captcha"
|
||||
)
|
||||
|
||||
const (
|
||||
UserIDEmpty = ""
|
||||
UserIDNone = ""
|
||||
UserIDID = "userid"
|
||||
UserIDID2 = "userid2"
|
||||
UserIDMobile = "mobile"
|
||||
@@ -28,13 +31,14 @@ const (
|
||||
|
||||
const (
|
||||
AuthTypeNone = ""
|
||||
AuthTypePassword = "password"
|
||||
AuthTypePassword = "localpass"
|
||||
AuthTypeEmail = "email"
|
||||
AuthTypeMobile = "mobile"
|
||||
)
|
||||
|
||||
const (
|
||||
DefTokenDuration = 7 * 24 * time.Hour // 7天
|
||||
MinCaptchaLen = 4
|
||||
)
|
||||
|
||||
type IUser interface {
|
||||
@@ -49,6 +53,11 @@ type IUserProvider interface {
|
||||
GetUser(authID, authIDType string) (user IUser)
|
||||
}
|
||||
|
||||
type CaptchaInfo struct {
|
||||
ID string `json:"id"`
|
||||
ImgBase64 string `json:"imgBase64"`
|
||||
}
|
||||
|
||||
type IAuther interface {
|
||||
SendVerifyCode(authID string) (err error)
|
||||
// 负责验证secret,并找到相应的用户返回(password,email,mobile类型的不负责用户查找)如果找不到用户UserID为空
|
||||
@@ -59,8 +68,14 @@ type IAuther interface {
|
||||
}
|
||||
|
||||
var (
|
||||
authers map[string]IAuther
|
||||
userProvider IUserProvider
|
||||
authers map[string]IAuther
|
||||
userProvider IUserProvider
|
||||
TestMobileMap = map[string]int{
|
||||
"91112345678": 1,
|
||||
}
|
||||
TestCaptchaMap = map[string]string{
|
||||
"hGU7pB": "dd1AvY",
|
||||
}
|
||||
authTypeGuesserMap = map[string]*regexp.Regexp{
|
||||
AuthTypeEmail: regexp.MustCompile(`^[A-Za-z0-9_\-\.]+\@[A-Za-z0-9_\-]+(\.[A-Za-z]+){1,5}$`),
|
||||
AuthTypeMobile: regexp.MustCompile(`^1[34578]\d{9}$`),
|
||||
@@ -68,12 +83,13 @@ var (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInternalErrror = errors.New("内部错误")
|
||||
ErrTokenIsInvalid = errors.New("Token非法")
|
||||
ErrUserAlreadyExist = errors.New("用户已经存在")
|
||||
ErrUserNotExist = errors.New("用户不存在")
|
||||
ErrIllegalAuthType = errors.New("非法的登录类型")
|
||||
ErrIllegalAuthTypeAlreadyExist = errors.New("要登录类型已经存在")
|
||||
ErrInternalErrror = errors.New("内部错误")
|
||||
ErrTokenIsInvalid = errors.New("Token非法")
|
||||
ErrUserAlreadyExist = errors.New("用户已经存在")
|
||||
ErrUserNotExist = errors.New("用户不存在")
|
||||
ErrIllegalAuthType = errors.New("非法的登录类型")
|
||||
ErrAuthTypeAlreadyExist = errors.New("要登录类型已经存在")
|
||||
ErrCaptchaIsNotOk = errors.New("图形校验码不正确")
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -88,7 +104,7 @@ func RegisterAuther(authType string, handler IAuther) {
|
||||
authers[authType] = handler
|
||||
}
|
||||
|
||||
func CreateauthInfo(user IUser, authBindInfo *model.AuthBind, userData interface{}) (authInfo *AuthInfo) {
|
||||
func CreateAuthInfo(user IUser, authBindInfo *model.AuthBind, userData interface{}) (authInfo *AuthInfo) {
|
||||
token, tokenType := createToken(user)
|
||||
authInfo = &AuthInfo{
|
||||
IUser: user,
|
||||
@@ -101,14 +117,39 @@ func CreateauthInfo(user IUser, authBindInfo *model.AuthBind, userData interface
|
||||
UserData: userData,
|
||||
}
|
||||
if user != nil {
|
||||
globals.SugarLogger.Debugf("CreateauthInfo id:%s, id2:%s, authBindInfo:%s, authInfo:%s", authInfo.GetID(), authInfo.GetID2(), authInfo.GetMobile(), utils.Format4Output(authBindInfo, true), utils.Format4Output(authInfo, true))
|
||||
globals.SugarLogger.Debugf("CreateAuthInfo id:%s, id2:%s, authBindInfo:%s, authInfo:%s", authInfo.GetID(), authInfo.GetID2(), authInfo.GetMobile(), utils.Format4Output(authBindInfo, true), utils.Format4Output(authInfo, true))
|
||||
} else {
|
||||
globals.SugarLogger.Debugf("CreateauthInfo authBindInfo:%s, authInfo:%s", utils.Format4Output(authBindInfo, true), utils.Format4Output(authInfo, true))
|
||||
globals.SugarLogger.Debugf("CreateAuthInfo authBindInfo:%s, authInfo:%s", utils.Format4Output(authBindInfo, true), utils.Format4Output(authInfo, true))
|
||||
}
|
||||
SetUserInfo(token, authInfo, DefTokenDuration)
|
||||
return authInfo
|
||||
}
|
||||
|
||||
func CreateCaptcha(width, height, captchaLen int) (captchaInfo *CaptchaInfo, err error) {
|
||||
if captchaLen < MinCaptchaLen {
|
||||
captchaLen = MinCaptchaLen
|
||||
}
|
||||
captchaInfo = &CaptchaInfo{
|
||||
ID: captcha.NewLen(captchaLen),
|
||||
}
|
||||
bufWriter := bytes.NewBuffer(nil)
|
||||
captcha.WriteImage(bufWriter, captchaInfo.ID, width, height)
|
||||
captchaInfo.ImgBase64 = "data:image/png;base64," + base64.StdEncoding.EncodeToString(bufWriter.Bytes())
|
||||
return captchaInfo, err
|
||||
}
|
||||
|
||||
func SendVerifyCode(captchaID, captchaValue, authID string) (err error) {
|
||||
if TestCaptchaMap[captchaID] == captchaValue || captcha.VerifyString(captchaID, captchaValue) {
|
||||
authType := GuessAuthTypeFromAuthID(authID)
|
||||
if handler := authers[authType]; handler == nil {
|
||||
return ErrIllegalAuthType
|
||||
} else {
|
||||
return handler.SendVerifyCode(authID)
|
||||
}
|
||||
}
|
||||
return ErrCaptchaIsNotOk
|
||||
}
|
||||
|
||||
// 账号密码时:authIDType可能是:UserIDID,UserIDID2,UserIDMobile,UserIDEmail,authSecret是密码的sha1
|
||||
// 邮箱时(如果允许):authIDType是:UserIDEmail,authSecret是验证码的sha1
|
||||
// 手机时(如果允许):authIDType是:UserIDMobile,authSecret是验证码的sha1
|
||||
@@ -140,7 +181,7 @@ func Login(authType, authID, authIDType, authSecret string) (authInfo *AuthInfo,
|
||||
user = userProvider.GetUser(authBind.UserID, UserIDID)
|
||||
}
|
||||
}
|
||||
authInfo = CreateauthInfo(user, authBind, nil)
|
||||
authInfo = CreateAuthInfo(user, authBind, nil)
|
||||
}
|
||||
} else {
|
||||
err = ErrIllegalAuthType
|
||||
@@ -160,7 +201,7 @@ func BindUser(inauthInfo *AuthInfo, user IUser) (outauthInfo *AuthInfo, err erro
|
||||
inauthInfo.AuthBindInfo.UserID = user.GetID()
|
||||
if err = handler.AddAuthBind(inauthInfo.AuthBindInfo, user.GetName()); err == nil {
|
||||
RemoveUserInfo(inauthInfo.Token)
|
||||
outauthInfo = CreateauthInfo(user, inauthInfo.AuthBindInfo, inauthInfo.UserData)
|
||||
outauthInfo = CreateAuthInfo(user, inauthInfo.AuthBindInfo, inauthInfo.UserData)
|
||||
}
|
||||
} else {
|
||||
err = ErrIllegalAuthType
|
||||
@@ -174,7 +215,7 @@ func AddAuthBind(authInfo *AuthInfo, newAuthInfo *AuthInfo) (err error) {
|
||||
return ErrInternalErrror
|
||||
}
|
||||
if newAuthInfo.IUser != nil {
|
||||
return ErrIllegalAuthTypeAlreadyExist
|
||||
return ErrAuthTypeAlreadyExist
|
||||
}
|
||||
RemoveUserInfo(newAuthInfo.Token)
|
||||
newAuthInfo.AuthBindInfo.UserID = authInfo.GetID()
|
||||
@@ -223,22 +264,13 @@ func Logout(authInfo *AuthInfo) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func SendVerifyCode(authID string) (err error) {
|
||||
authType := GuessAuthTypeFromAuthID(authID)
|
||||
if handler := authers[authType]; handler == nil {
|
||||
return ErrIllegalAuthType
|
||||
} else {
|
||||
return handler.SendVerifyCode(authID)
|
||||
}
|
||||
}
|
||||
|
||||
// token缓存相关
|
||||
|
||||
func RemoveUserInfo(token string) {
|
||||
api.Cacher.Del(token)
|
||||
}
|
||||
|
||||
func GetUserInfo(token string) (authInfo *AuthInfo, err error) {
|
||||
func GetTokenInfo(token string) (authInfo *AuthInfo, err error) {
|
||||
authInfo = new(AuthInfo)
|
||||
if err = api.Cacher.GetAs(token, authInfo); err == nil {
|
||||
return authInfo, nil
|
||||
@@ -284,6 +316,9 @@ func IsV2Token(token string) bool {
|
||||
}
|
||||
|
||||
func GuessAuthTypeFromAuthID(authID string) (authType string) {
|
||||
if TestMobileMap[authID] == 1 {
|
||||
return AuthTypeMobile
|
||||
}
|
||||
for k, v := range authTypeGuesserMap {
|
||||
if v.FindStringSubmatch(authID) != nil {
|
||||
return k
|
||||
|
||||
Reference in New Issue
Block a user