aboutsummaryrefslogtreecommitdiffstats
path: root/routers/user/auth.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-10-14 14:10:42 +0800
committerLauris BH <lauris@nix.lv>2019-10-14 09:10:42 +0300
commite3e44a59d01da3af2be3a830f4a90394e7af4ff4 (patch)
treef66e8ead94693225668bacd0ba603bf3712aeae6 /routers/user/auth.go
parentba201aaa44b19f633fab0c4682d5f97558b3205e (diff)
downloadgitea-e3e44a59d01da3af2be3a830f4a90394e7af4ff4.tar.gz
gitea-e3e44a59d01da3af2be3a830f4a90394e7af4ff4.zip
Update migrated repositories' issues/comments/prs poster id if user has a github external user saved (#7751)
* update migrated issues/comments when login as github * add get userid when migrating or login with github oauth2 * fix lint * add migrations for repository service type * fix build * remove unnecessary dependencies on migrations * add cron task to update migrations poster ids and fix posterid when migrating * fix lint * fix lint * improve code * fix lint * improve code * replace releases publish id to actual author id * fix import * fix bug * fix lint * fix rawdata definition * fix some bugs * fix error message
Diffstat (limited to 'routers/user/auth.go')
-rw-r--r--routers/user/auth.go88
1 files changed, 51 insertions, 37 deletions
diff --git a/routers/user/auth.go b/routers/user/auth.go
index 3def867f64..212d535a06 100644
--- a/routers/user/auth.go
+++ b/routers/user/auth.go
@@ -21,6 +21,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
+ "code.gitea.io/gitea/services/externalaccount"
"code.gitea.io/gitea/services/mailer"
"gitea.com/macaron/captcha"
@@ -277,7 +278,7 @@ func TwoFactorPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
return
}
- err = models.LinkAccountToUser(u, gothUser.(goth.User))
+ err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
if err != nil {
ctx.ServerError("UserSignIn", err)
return
@@ -452,7 +453,7 @@ func U2FSign(ctx *context.Context, signResp u2f.SignResponse) {
return
}
- err = models.LinkAccountToUser(user, gothUser.(goth.User))
+ err = externalaccount.LinkAccountToUser(user, gothUser.(goth.User))
if err != nil {
ctx.ServerError("UserSignIn", err)
return
@@ -601,36 +602,42 @@ func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context
// Instead, redirect them to the 2FA authentication page.
_, err = models.GetTwoFactorByUID(u.ID)
if err != nil {
- if models.IsErrTwoFactorNotEnrolled(err) {
- err = ctx.Session.Set("uid", u.ID)
- if err != nil {
- log.Error(fmt.Sprintf("Error setting session: %v", err))
- }
- err = ctx.Session.Set("uname", u.Name)
- if err != nil {
- log.Error(fmt.Sprintf("Error setting session: %v", err))
- }
+ if !models.IsErrTwoFactorNotEnrolled(err) {
+ ctx.ServerError("UserSignIn", err)
+ return
+ }
- // Clear whatever CSRF has right now, force to generate a new one
- ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
+ err = ctx.Session.Set("uid", u.ID)
+ if err != nil {
+ log.Error(fmt.Sprintf("Error setting session: %v", err))
+ }
+ err = ctx.Session.Set("uname", u.Name)
+ if err != nil {
+ log.Error(fmt.Sprintf("Error setting session: %v", err))
+ }
- // Register last login
- u.SetLastLogin()
- if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
- ctx.ServerError("UpdateUserCols", err)
- return
- }
+ // Clear whatever CSRF has right now, force to generate a new one
+ ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
- if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 {
- ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
- ctx.RedirectToFirst(redirectTo)
- return
- }
+ // Register last login
+ u.SetLastLogin()
+ if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
+ ctx.ServerError("UpdateUserCols", err)
+ return
+ }
- ctx.Redirect(setting.AppSubURL + "/")
- } else {
- ctx.ServerError("UserSignIn", err)
+ // update external user information
+ if err := models.UpdateExternalUser(u, gothUser); err != nil {
+ log.Error("UpdateExternalUser failed: %v", err)
+ }
+
+ if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 {
+ ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
+ ctx.RedirectToFirst(redirectTo)
+ return
}
+
+ ctx.Redirect(setting.AppSubURL + "/")
return
}
@@ -675,7 +682,7 @@ func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Requ
}
if hasUser {
- return user, goth.User{}, nil
+ return user, gothUser, nil
}
// search in external linked users
@@ -689,7 +696,7 @@ func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Requ
}
if hasUser {
user, err = models.GetUserByID(externalLoginUser.UserID)
- return user, goth.User{}, err
+ return user, gothUser, err
}
// no user found to login
@@ -789,16 +796,18 @@ func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
// Instead, redirect them to the 2FA authentication page.
_, err = models.GetTwoFactorByUID(u.ID)
if err != nil {
- if models.IsErrTwoFactorNotEnrolled(err) {
- err = models.LinkAccountToUser(u, gothUser.(goth.User))
- if err != nil {
- ctx.ServerError("UserLinkAccount", err)
- } else {
- handleSignIn(ctx, u, signInForm.Remember)
- }
- } else {
+ if !models.IsErrTwoFactorNotEnrolled(err) {
+ ctx.ServerError("UserLinkAccount", err)
+ return
+ }
+
+ err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
+ if err != nil {
ctx.ServerError("UserLinkAccount", err)
+ return
}
+
+ handleSignIn(ctx, u, signInForm.Remember)
return
}
@@ -947,6 +956,11 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
}
}
+ // update external user information
+ if err := models.UpdateExternalUser(u, gothUser.(goth.User)); err != nil {
+ log.Error("UpdateExternalUser failed: %v", err)
+ }
+
// Send confirmation email
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
mailer.SendActivateAccountMail(ctx.Locale, u)