Skip to content

Commit

Permalink
fix: handle WeChat username conflicts (#771)
Browse files Browse the repository at this point in the history
* handle username conflicts

* Update auth.go

Co-authored-by: roobtyan <roobtyan@qq.com>
Co-authored-by: Yang Luo <hsluoyz@qq.com>
  • Loading branch information
3 people authored and nomeguy committed May 31, 2022
1 parent 5d8b710 commit f770593
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
20 changes: 17 additions & 3 deletions controllers/auth.go
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/proxy"
"github.com/casdoor/casdoor/util"
"github.com/google/uuid"
)

func codeToResponse(code *object.Code) *Response {
Expand Down Expand Up @@ -252,7 +253,7 @@ func (c *ApiController) Login() {
record := object.NewRecord(c.Ctx)
record.Organization = application.Organization
record.User = user.Name
util.SafeGoroutine(func() {object.AddRecord(record)})
util.SafeGoroutine(func() { object.AddRecord(record) })
}
} else if form.Provider != "" {
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
Expand Down Expand Up @@ -345,7 +346,7 @@ func (c *ApiController) Login() {
record := object.NewRecord(c.Ctx)
record.Organization = application.Organization
record.User = user.Name
util.SafeGoroutine(func() {object.AddRecord(record)})
util.SafeGoroutine(func() { object.AddRecord(record) })
} else if provider.Category == "OAuth" {
// Sign up via OAuth
if !application.EnableSignUp {
Expand All @@ -358,6 +359,19 @@ func (c *ApiController) Login() {
return
}

// Handle username conflicts
tmpUser := object.GetUser(fmt.Sprintf("%s/%s", application.Organization, userInfo.Username))
if tmpUser != nil {
uid, err := uuid.NewRandom()
if err != nil {
c.ResponseError(err.Error())
return
}

uidStr := strings.Split(uid.String(), "-")
userInfo.Username = fmt.Sprintf("%s_%s", userInfo.Username, uidStr[1])
}

properties := map[string]string{}
properties["no"] = strconv.Itoa(len(object.GetUsers(application.Organization)) + 2)
user = &object.User{
Expand Down Expand Up @@ -394,7 +408,7 @@ func (c *ApiController) Login() {
record := object.NewRecord(c.Ctx)
record.Organization = application.Organization
record.User = user.Name
util.SafeGoroutine(func() {object.AddRecord(record)})
util.SafeGoroutine(func() { object.AddRecord(record) })
} else if provider.Category == "SAML" {
resp = &Response{Status: "error", Msg: "The account does not exist"}
}
Expand Down
2 changes: 1 addition & 1 deletion idp/wechat.go
Expand Up @@ -185,7 +185,7 @@ func (idp *WeChatIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error)

userInfo := UserInfo{
Id: id,
Username: id,
Username: wechatUserInfo.Nickname,
DisplayName: wechatUserInfo.Nickname,
AvatarUrl: wechatUserInfo.Headimgurl,
}
Expand Down
2 changes: 1 addition & 1 deletion routers/record.go
Expand Up @@ -65,5 +65,5 @@ func RecordMessage(ctx *context.Context) {
record.Organization, record.User = util.GetOwnerAndNameFromId(userId)
}

util.SafeGoroutine(func() {object.AddRecord(record)})
util.SafeGoroutine(func() { object.AddRecord(record) })
}

0 comments on commit f770593

Please sign in to comment.