diff --git a/controllers/auth.go b/controllers/auth.go index f55e669b..5784c0d9 100644 --- a/controllers/auth.go +++ b/controllers/auth.go @@ -417,8 +417,15 @@ func (c *ApiController) Login() { } else if provider.Category == "OAuth" { // Sign up via OAuth if application.EnableLinkWithEmail { - // find user that has the same email - user = object.GetUserByField(application.Organization, "email", userInfo.Email) + if userInfo.Email != "" { + // Find existing user with Email + user = object.GetUserByField(application.Organization, "email", userInfo.Email) + } + + if user == nil && userInfo.Phone != "" { + // Find existing user with phone number + user = object.GetUserByField(application.Organization, "phone", userInfo.Phone) + } } if user == nil || user.IsDeleted { diff --git a/idp/dingtalk.go b/idp/dingtalk.go index 58555aaf..b890a638 100644 --- a/idp/dingtalk.go +++ b/idp/dingtalk.go @@ -179,8 +179,12 @@ func (idp *DingTalkIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, erro return nil, err } - corpEmail, jobNumber, err := idp.getUserCorpEmail(userId, corpAccessToken) + corpMobile, corpEmail, jobNumber, err := idp.getUserCorpEmail(userId, corpAccessToken) if err == nil { + if corpMobile != "" { + userInfo.Phone = corpMobile + } + if corpEmail != "" { userInfo.Email = corpEmail } @@ -264,27 +268,29 @@ func (idp *DingTalkIdProvider) getUserId(unionId string, accessToken string) (st return data.Result.UserId, nil } -func (idp *DingTalkIdProvider) getUserCorpEmail(userId string, accessToken string) (string, string, error) { +func (idp *DingTalkIdProvider) getUserCorpEmail(userId string, accessToken string) (string, string, string, error) { + // https://open.dingtalk.com/document/isvapp/query-user-details body := make(map[string]string) body["userid"] = userId respBytes, err := idp.postWithBody(body, "https://oapi.dingtalk.com/topapi/v2/user/get?access_token="+accessToken) if err != nil { - return "", "", err + return "", "", "", err } var data struct { ErrMessage string `json:"errmsg"` Result struct { + Mobile string `json:"mobile"` Email string `json:"email"` JobNumber string `json:"job_number"` } `json:"result"` } err = json.Unmarshal(respBytes, &data) if err != nil { - return "", "", err + return "", "", "", err } if data.ErrMessage != "ok" { - return "", "", fmt.Errorf(data.ErrMessage) + return "", "", "", fmt.Errorf(data.ErrMessage) } - return data.Result.Email, data.Result.JobNumber, nil + return data.Result.Mobile, data.Result.Email, data.Result.JobNumber, nil }