Skip to content

Commit b09c73b

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 2a2b46c commit b09c73b

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

models/user.go

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

245-
// IsOAuth2 returns true if user login type is LoginOAuth2.
246-
func (u *User) IsOAuth2() bool {
247-
return u.LoginType == LoginOAuth2
248-
}
249-
250245
// HasForkedRepo checks if user has already forked a repository with given ID.
251246
func (u *User) HasForkedRepo(repoID int64) bool {
252247
_, has := HasForkedRepo(u.ID, repoID)

routers/user/auth.go

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -653,41 +653,26 @@ func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context
653653
ctx.Redirect(setting.AppSubURL + "/user/two_factor")
654654
}
655655

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

661661
if err != nil {
662662
return nil, goth.User{}, err
663663
}
664664

665-
user := &models.User{
666-
LoginName: gothUser.UserID,
667-
LoginType: models.LoginOAuth2,
668-
LoginSource: loginSource.ID,
669-
}
670-
671-
hasUser, err := models.GetUser(user)
672-
if err != nil {
673-
return nil, goth.User{}, err
674-
}
675-
676-
if hasUser {
677-
return user, goth.User{}, nil
678-
}
679-
680665
// search in external linked users
681666
externalLoginUser := &models.ExternalLoginUser{
682667
ExternalID: gothUser.UserID,
683668
LoginSourceID: loginSource.ID,
684669
}
685-
hasUser, err = models.GetExternalLogin(externalLoginUser)
670+
hasUser, err := models.GetExternalLogin(externalLoginUser)
686671
if err != nil {
687672
return nil, goth.User{}, err
688673
}
689674
if hasUser {
690-
user, err = models.GetUserByID(externalLoginUser.UserID)
675+
user, err := models.GetUserByID(externalLoginUser.UserID)
691676
return user, goth.User{}, err
692677
}
693678

@@ -899,19 +884,11 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
899884
}
900885
}
901886

902-
loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.(goth.User).Provider)
903-
if err != nil {
904-
ctx.ServerError("CreateUser", err)
905-
}
906-
907887
u := &models.User{
908-
Name: form.UserName,
909-
Email: form.Email,
910-
Passwd: form.Password,
911-
IsActive: !setting.Service.RegisterEmailConfirm,
912-
LoginType: models.LoginOAuth2,
913-
LoginSource: loginSource.ID,
914-
LoginName: gothUser.(goth.User).UserID,
888+
Name: form.UserName,
889+
Email: form.Email,
890+
Passwd: form.Password,
891+
IsActive: !setting.Service.RegisterEmailConfirm,
915892
}
916893

917894
if err := models.CreateUser(u); err != nil {
@@ -935,6 +912,13 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
935912
}
936913
log.Trace("Account created: %s", u.Name)
937914

915+
// Link the oAuth account to the user
916+
err := models.LinkAccountToUser(u, gothUser.(goth.User))
917+
if err != nil {
918+
ctx.ServerError("UserLinkAccount", err)
919+
}
920+
log.Trace("Account %s linked to gothUser %s", u.Name, gothUser.(goth.User))
921+
938922
// Auto-set admin for the only user.
939923
if models.CountUsers() == 1 {
940924
u.IsAdmin = true
@@ -1235,7 +1219,7 @@ func ForgotPasswdPost(ctx *context.Context) {
12351219
return
12361220
}
12371221

1238-
if !u.IsLocal() && !u.IsOAuth2() {
1222+
if !u.IsLocal() {
12391223
ctx.Data["Err_Email"] = true
12401224
ctx.RenderWithErr(ctx.Tr("auth.non_local_account"), tplForgotPassword, nil)
12411225
return

0 commit comments

Comments
 (0)