Skip to content

Commit 01d0770

Browse files
committed
Always put OAuth user info in the ExternalLoginUser table
Never set OAuth in User.LoginSource, which keeps referring to the username/password interpretation. Fixes #1124
1 parent 9182a35 commit 01d0770

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

models/user.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,6 @@ func (u *User) IsLocal() bool {
198198
return u.LoginType <= LoginPlain
199199
}
200200

201-
// IsOAuth2 returns true if user login type is LoginOAuth2.
202-
func (u *User) IsOAuth2() bool {
203-
return u.LoginType == LoginOAuth2
204-
}
205-
206201
// HasForkedRepo checks if user has already forked a repository with given ID.
207202
func (u *User) HasForkedRepo(repoID int64) bool {
208203
_, has := HasForkedRepo(u.ID, repoID)

routers/user/auth.go

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -448,41 +448,26 @@ func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context
448448
ctx.Redirect(setting.AppSubURL + "/user/two_factor")
449449
}
450450

451-
// OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful
452-
// login the user
451+
// OAuth2UserLoginCallback attempts to handle the callback from the
452+
// OAuth2 provider and if successful login the user
453453
func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) {
454454
gothUser, err := oauth2.ProviderCallback(loginSource.Name, request, response)
455455

456456
if err != nil {
457457
return nil, goth.User{}, err
458458
}
459459

460-
user := &models.User{
461-
LoginName: gothUser.UserID,
462-
LoginType: models.LoginOAuth2,
463-
LoginSource: loginSource.ID,
464-
}
465-
466-
hasUser, err := models.GetUser(user)
467-
if err != nil {
468-
return nil, goth.User{}, err
469-
}
470-
471-
if hasUser {
472-
return user, goth.User{}, nil
473-
}
474-
475460
// search in external linked users
476461
externalLoginUser := &models.ExternalLoginUser{
477462
ExternalID: gothUser.UserID,
478463
LoginSourceID: loginSource.ID,
479464
}
480-
hasUser, err = models.GetExternalLogin(externalLoginUser)
465+
hasUser, err := models.GetExternalLogin(externalLoginUser)
481466
if err != nil {
482467
return nil, goth.User{}, err
483468
}
484469
if hasUser {
485-
user, err = models.GetUserByID(externalLoginUser.UserID)
470+
user, err := models.GetUserByID(externalLoginUser.UserID)
486471
return user, goth.User{}, err
487472
}
488473

@@ -620,19 +605,11 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
620605
return
621606
}
622607

623-
loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.(goth.User).Provider)
624-
if err != nil {
625-
ctx.Handle(500, "CreateUser", err)
626-
}
627-
628608
u := &models.User{
629609
Name: form.UserName,
630610
Email: form.Email,
631611
Passwd: form.Password,
632612
IsActive: !setting.Service.RegisterEmailConfirm,
633-
LoginType: models.LoginOAuth2,
634-
LoginSource: loginSource.ID,
635-
LoginName: gothUser.(goth.User).UserID,
636613
}
637614

638615
if err := models.CreateUser(u); err != nil {
@@ -656,6 +633,13 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
656633
}
657634
log.Trace("Account created: %s", u.Name)
658635

636+
// Link the oAuth account to the user
637+
err := models.LinkAccountToUser(u, gothUser.(goth.User))
638+
if err != nil {
639+
ctx.Handle(500, "UserLinkAccount", err)
640+
}
641+
log.Trace("Account %s linked to gothUser %s", u.Name, gothUser.(goth.User))
642+
659643
// Auto-set admin for the only user.
660644
if models.CountUsers() == 1 {
661645
u.IsAdmin = true
@@ -913,7 +897,7 @@ func ForgotPasswdPost(ctx *context.Context) {
913897
return
914898
}
915899

916-
if !u.IsLocal() && !u.IsOAuth2() {
900+
if !u.IsLocal() {
917901
ctx.Data["Err_Email"] = true
918902
ctx.RenderWithErr(ctx.Tr("auth.non_local_account"), tplForgotPassword, nil)
919903
return

0 commit comments

Comments
 (0)