mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Improve IDP username handling.
This commit is contained in:
parent
39d208ccd0
commit
540adfed20
@ -135,13 +135,7 @@ func (c *ApiController) Login() {
|
||||
}
|
||||
|
||||
if form.Method == "signup" {
|
||||
userId := ""
|
||||
if provider.Type == "github" {
|
||||
userId = object.GetUserIdByField(application, "github", userInfo.Username)
|
||||
} else if provider.Type == "google" {
|
||||
userId = object.GetUserIdByField(application, "google", userInfo.Email)
|
||||
}
|
||||
|
||||
userId := object.GetUserIdByField(application, provider.Type, userInfo.Username)
|
||||
if userId != "" {
|
||||
//if object.IsForbidden(userId) {
|
||||
// c.forbiddenAccountResp(userId)
|
||||
@ -163,11 +157,7 @@ func (c *ApiController) Login() {
|
||||
if userId := object.GetUserIdByField(application, "email", userInfo.Email); userId != "" {
|
||||
resp = c.HandleLoggedIn(userId, &form)
|
||||
|
||||
if provider.Type == "github" {
|
||||
_ = object.LinkUserAccount(userId, "github", userInfo.Username)
|
||||
} else if provider.Type == "google" {
|
||||
_ = object.LinkUserAccount(userId, "google", userInfo.Email)
|
||||
}
|
||||
object.LinkUserAccount(userId, provider.Type, userInfo.Username)
|
||||
}
|
||||
}
|
||||
//resp = &Response{Status: "ok", Msg: "", Data: res}
|
||||
@ -180,16 +170,11 @@ func (c *ApiController) Login() {
|
||||
return
|
||||
}
|
||||
|
||||
linkRes := false
|
||||
if provider.Type == "github" {
|
||||
linkRes = object.LinkUserAccount(userId, "github", userInfo.Username)
|
||||
} else if provider.Type == "google" {
|
||||
linkRes = object.LinkUserAccount(userId, "google", userInfo.Email)
|
||||
}
|
||||
if linkRes {
|
||||
resp = &Response{Status: "ok", Msg: "", Data: linkRes}
|
||||
isLinked := object.LinkUserAccount(userId, provider.Type, userInfo.Username)
|
||||
if isLinked {
|
||||
resp = &Response{Status: "ok", Msg: "", Data: isLinked}
|
||||
} else {
|
||||
resp = &Response{Status: "error", Msg: "link account failed", Data: linkRes}
|
||||
resp = &Response{Status: "error", Msg: "link account failed", Data: isLinked}
|
||||
}
|
||||
//if len(object.GetMemberAvatar(userId)) == 0 {
|
||||
// avatar := UploadAvatarToOSS(tempUserAccount.AvatarUrl, userId)
|
||||
|
@ -32,8 +32,8 @@ type GoogleIdProvider struct {
|
||||
RedirectUrl string
|
||||
}
|
||||
|
||||
func NewGoogleIdProvider(clientId string, clientSecret string, redirectUrl string) *GithubIdProvider {
|
||||
idp := &GithubIdProvider{
|
||||
func NewGoogleIdProvider(clientId string, clientSecret string, redirectUrl string) *GoogleIdProvider {
|
||||
idp := &GoogleIdProvider{
|
||||
ClientId: clientId,
|
||||
ClientSecret: clientSecret,
|
||||
RedirectUrl: redirectUrl,
|
||||
@ -91,6 +91,7 @@ func (idp *GoogleIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error)
|
||||
return userInfo, errors.New("google email is empty")
|
||||
}
|
||||
|
||||
userInfo.Username = userResponse.Email
|
||||
userInfo.Email = userResponse.Email
|
||||
userInfo.AvatarUrl = userResponse.Picture
|
||||
return userInfo, nil
|
||||
|
@ -113,7 +113,7 @@ func (idp *QqIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) {
|
||||
return nil, errors.New("openId is empty")
|
||||
}
|
||||
|
||||
getUserInfoUrl := fmt.Sprintf("https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s", token, idp.ClientId, openId)
|
||||
getUserInfoUrl := fmt.Sprintf("https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s", token.AccessToken, idp.ClientId, openId)
|
||||
getUserInfoResponse, err := idp.Client.Get(getUserInfoUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -128,7 +128,7 @@ func (idp *QqIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) {
|
||||
defer getUserInfoResponse.Body.Close()
|
||||
userInfoContent, err := ioutil.ReadAll(getUserInfoResponse.Body)
|
||||
var userResponse response
|
||||
err = json.Unmarshal(userInfoContent, &userInfo)
|
||||
err = json.Unmarshal(userInfoContent, &userResponse)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -40,6 +40,7 @@ type User struct {
|
||||
|
||||
Github string `xorm:"varchar(100)" json:"github"`
|
||||
Google string `xorm:"varchar(100)" json:"google"`
|
||||
Qq string `xorm:"varchar(100)" json:"qq"`
|
||||
}
|
||||
|
||||
func GetGlobalUsers() []*User {
|
||||
|
Loading…
x
Reference in New Issue
Block a user