Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create external user for new OAuth2 user #21276

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions models/user/external_login_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,23 @@ func GetUserIDByExternalUserID(provider, userID string) (int64, error) {
}

// UpdateExternalUserByExternalID updates an external user's information
func UpdateExternalUserByExternalID(external *ExternalLoginUser) error {
func UpdateExternalUserByExternalID(external *ExternalLoginUser, upsert bool) error {
has, err := db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID).
NoAutoCondition().
Exist(external)
if err != nil {
return err
} else if !has {
}

if !has && !upsert {
return ErrExternalLoginUserNotExist{external.UserID, external.LoginSourceID}
}

_, err = db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID).AllCols().Update(external)
if has {
_, err = db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID).AllCols().Update(external)
} else {
_, err = db.GetEngine(db.DefaultContext).Insert(external)
}
return err
}

Expand Down
2 changes: 1 addition & 1 deletion routers/web/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.

// update external user information
if gothUser != nil {
if err := externalaccount.UpdateExternalUser(u, *gothUser); err != nil {
if err := externalaccount.UpdateExternalUser(u, *gothUser, true); err != nil {
log.Error("UpdateExternalUser failed: %v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion routers/web/auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *auth.Source, u *user_model
}

// update external user information
if err := externalaccount.UpdateExternalUser(u, gothUser); err != nil {
if err := externalaccount.UpdateExternalUser(u, gothUser, false); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why upsert=false?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike handleUserCreated, in handleOAuth2SignIn, the external user is expected to exist.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I tried to read the code, but the flow is quite complex.

I am not sure that handleOAuth2SignIn is guaranteed to be called after external user is prepared correctly.

Since the code was that before, I think there shouldn't be more problem. So LGTM.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm not even sure the stored external user data is being used. The code couldn't store any data before, but it worked fine, just caused user concern because of error logs.

log.Error("UpdateExternalUser failed: %v", err)
}

Expand Down
4 changes: 2 additions & 2 deletions services/externalaccount/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ func LinkAccountToUser(user *user_model.User, gothUser goth.User) error {
}

// UpdateExternalUser updates external user's information
func UpdateExternalUser(user *user_model.User, gothUser goth.User) error {
func UpdateExternalUser(user *user_model.User, gothUser goth.User, upsert bool) error {
externalLoginUser, err := toExternalLoginUser(user, gothUser)
if err != nil {
return err
}

return user_model.UpdateExternalUserByExternalID(externalLoginUser)
return user_model.UpdateExternalUserByExternalID(externalLoginUser, upsert)
}