- dingding auth

This commit is contained in:
gazebo
2019-03-07 21:15:19 +08:00
parent 123ffd4353
commit ce109a60b3
16 changed files with 238 additions and 85 deletions

View File

@@ -53,6 +53,7 @@ type IUserProvider interface {
GetUser(authID, authIDType string) (user IUser)
UpdateUserMobile(userID string, mobile string) (err error)
UpdateUserEmail(userID string, email string) (err error)
CreateUser(userID2, mobile, email, name string) (user IUser, err error)
}
type CaptchaInfo struct {
@@ -63,8 +64,8 @@ type CaptchaInfo struct {
type IAuther interface {
SendVerifyCode(authID string) (err error)
// 负责验证secret并找到相应的用户返回password,email,mobile类型的不负责用户查找如果找不到用户UserID为空
VerifySecret(authID, authSecret string) (authBind *model.AuthBind, err error)
AddAuthBind(authBind *model.AuthBind, userName string) (err error)
VerifySecret(authID, authSecret string) (authBindEx *AuthBindEx, err error)
AddAuthBind(authBindEx *AuthBindEx, userName string) (err error)
UnbindAuth(authInfo *AuthInfo, authType string) (err error)
Logout(authInfo *AuthInfo) (err error)
}
@@ -110,7 +111,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 *AuthBindEx) (authInfo *AuthInfo) {
token, tokenType := createToken(user)
authInfo = &AuthInfo{
AuthBindInfo: authBindInfo,
@@ -119,7 +120,6 @@ func CreateAuthInfo(user IUser, authBindInfo *model.AuthBind, userData interface
ExpiresIn: time.Now().Add(DefTokenDuration).Unix(),
Token: token,
TokenType: tokenType,
UserData: userData,
}
if user != nil {
authInfo.UpdateByIUser(user)
@@ -177,7 +177,7 @@ func Login(authType, authID, authIDType, authSecret string) (authInfo *AuthInfo,
authType = strings.ToLower(authType)
authIDType = strings.ToLower(authIDType)
if handler := authers[authType]; handler != nil {
var authBind *model.AuthBind
var authBindEx *AuthBindEx
var user IUser
realAuthID := authID
if authType == AuthTypePassword {
@@ -189,21 +189,33 @@ func Login(authType, authID, authIDType, authSecret string) (authInfo *AuthInfo,
}
realAuthID = user.GetID()
}
if authBind, err = handler.VerifySecret(realAuthID, authSecret); err == nil {
if authBind == nil { // mobile, email会返回nil表示不会新建AuthBind实体
if authBindEx, err = handler.VerifySecret(realAuthID, authSecret); err == nil {
if authBindEx == nil { // mobile, email会返回nil表示不会新建AuthBind实体
user = userProvider.GetUser(authID, authIDType)
authBind = &model.AuthBind{
Type: authType,
AuthID: authID,
Status: model.AuthBindStatusNormal,
authBindEx = &AuthBindEx{
AuthBind: model.AuthBind{
Type: authType,
AuthID: authID,
Status: model.AuthBindStatusNormal,
},
}
} else {
// 返回authBind中UserID为空表示只是认证但本地没有记录这种情况会返回临时TOKEN
if authBind.UserID != "" {
user = userProvider.GetUser(authBind.UserID, UserIDID)
if authBindEx.UserHint != nil && authBindEx.UserID == "" {
if authBindEx.UserHint.Mobile != "" {
user = userProvider.GetUser(authBindEx.UserHint.Mobile, UserIDMobile)
}
if user == nil && authBindEx.UserHint.Email != "" {
user = userProvider.GetUser(authBindEx.UserHint.Email, UserIDEmail)
}
if user != nil {
authBindEx.UserID = user.GetID()
}
} else if authBindEx.UserID != "" {
user = userProvider.GetUser(authBindEx.UserID, UserIDID)
}
}
authInfo = CreateAuthInfo(user, authBind, nil)
authInfo = createAuthInfo(user, authBindEx)
}
} else {
err = ErrIllegalAuthType
@@ -223,7 +235,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)
}
} else {
err = ErrIllegalAuthType