Support mobile in DingTalk userinfo

This commit is contained in:
Yang Luo 2023-04-29 01:11:58 +08:00
parent fba4801a41
commit 3a6ab4cfc6
4 changed files with 32 additions and 4 deletions

View File

@ -458,6 +458,9 @@ func (c *ApiController) Login() {
Avatar: userInfo.AvatarUrl,
Address: []string{},
Email: userInfo.Email,
Phone: userInfo.Phone,
CountryCode: userInfo.CountryCode,
Region: userInfo.CountryCode,
Score: initScore,
IsAdmin: false,
IsGlobalAdmin: false,

View File

@ -23,6 +23,7 @@ import (
"strings"
"time"
"github.com/casdoor/casdoor/util"
"golang.org/x/oauth2"
)
@ -125,8 +126,8 @@ type DingTalkUserResponse struct {
UnionId string `json:"unionId"`
AvatarUrl string `json:"avatarUrl"`
Email string `json:"email"`
Errmsg string `json:"message"`
Errcode string `json:"code"`
Mobile string `json:"mobile"`
StateCode string `json:"stateCode"`
}
// GetUserInfo Use access_token to get UserInfo
@ -156,8 +157,9 @@ func (idp *DingTalkIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, erro
return nil, err
}
if dtUserInfo.Errmsg != "" {
return nil, fmt.Errorf("userIdResp.Errcode = %s, userIdResp.Errmsg = %s", dtUserInfo.Errcode, dtUserInfo.Errmsg)
countryCode, err := util.GetCountryCode(dtUserInfo.StateCode, dtUserInfo.Mobile)
if err != nil {
return nil, err
}
userInfo := UserInfo{
@ -166,6 +168,8 @@ func (idp *DingTalkIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, erro
DisplayName: dtUserInfo.Nick,
UnionId: dtUserInfo.UnionId,
Email: dtUserInfo.Email,
Phone: dtUserInfo.Mobile,
CountryCode: countryCode,
AvatarUrl: dtUserInfo.AvatarUrl,
}

View File

@ -27,6 +27,8 @@ type UserInfo struct {
DisplayName string
UnionId string
Email string
Phone string
CountryCode string
AvatarUrl string
}

View File

@ -15,6 +15,7 @@
package util
import (
"fmt"
"net/mail"
"regexp"
@ -48,3 +49,21 @@ func GetE164Number(phone string, countryCode string) (string, bool) {
phoneNumber, _ := phonenumbers.Parse(phone, countryCode)
return phonenumbers.Format(phoneNumber, phonenumbers.E164), phonenumbers.IsValidNumber(phoneNumber)
}
func GetCountryCode(prefix string, phone string) (string, error) {
if prefix == "" || phone == "" {
return "", nil
}
phoneNumber, err := phonenumbers.Parse(fmt.Sprintf("+%s%s", prefix, phone), "")
if err != nil {
return "", err
}
countryCode := phonenumbers.GetRegionCodeForNumber(phoneNumber)
if countryCode == "" {
return "", fmt.Errorf("country code not found for phone prefix: %s", prefix)
}
return countryCode, nil
}