aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorMai-Lapyst <67418776+Mai-Lapyst@users.noreply.github.com>2022-05-29 02:03:17 +0200
committerGitHub <noreply@github.com>2022-05-29 02:03:17 +0200
commit4698a1ec47847da177b6df2738336f039057d585 (patch)
treea6fb537d524522b13d78764504a8e11ffcf9cdbf /routers
parentf2439b7e4c47de018e1678ceaa434e4b3d734a83 (diff)
downloadgitea-4698a1ec47847da177b6df2738336f039057d585.tar.gz
gitea-4698a1ec47847da177b6df2738336f039057d585.zip
Adding button to link accounts from user settings (#19792)
* Adding button to link accounts from user settings * Only display button to link user accounts when at least one OAuth2 provider is active
Diffstat (limited to 'routers')
-rw-r--r--routers/web/auth/oauth.go12
-rw-r--r--routers/web/user/setting/security/security.go9
-rw-r--r--routers/web/web.go8
3 files changed, 24 insertions, 5 deletions
diff --git a/routers/web/auth/oauth.go b/routers/web/auth/oauth.go
index 9aa31c1c02..f646615968 100644
--- a/routers/web/auth/oauth.go
+++ b/routers/web/auth/oauth.go
@@ -847,7 +847,17 @@ func SignInOAuthCallback(ctx *context.Context) {
}
if u == nil {
- if !setting.Service.AllowOnlyInternalRegistration && setting.OAuth2Client.EnableAutoRegistration {
+ if ctx.Doer != nil {
+ // attach user to already logged in user
+ err = externalaccount.LinkAccountToUser(ctx.Doer, gothUser)
+ if err != nil {
+ ctx.ServerError("UserLinkAccount", err)
+ return
+ }
+
+ ctx.Redirect(setting.AppSubURL + "/user/settings/security")
+ return
+ } else if !setting.Service.AllowOnlyInternalRegistration && setting.OAuth2Client.EnableAutoRegistration {
// create new user with details from oauth2 provider
var missingFields []string
if gothUser.UserID == "" {
diff --git a/routers/web/user/setting/security/security.go b/routers/web/user/setting/security/security.go
index a87012c480..747bf64a17 100644
--- a/routers/web/user/setting/security/security.go
+++ b/routers/web/user/setting/security/security.go
@@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/services/auth/source/oauth2"
)
const (
@@ -109,6 +110,14 @@ func loadSecurityData(ctx *context.Context) {
}
ctx.Data["AccountLinks"] = sources
+ orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
+ if err != nil {
+ ctx.ServerError("GetActiveOAuth2Providers", err)
+ return
+ }
+ ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names
+ ctx.Data["OAuth2Providers"] = oauth2Providers
+
openid, err := user_model.GetUserOpenIDs(ctx.Doer.ID)
if err != nil {
ctx.ServerError("GetUserOpenIDs", err)
diff --git a/routers/web/web.go b/routers/web/web.go
index 97ea1e9035..1e550286f9 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -344,10 +344,6 @@ func RegisterRoutes(m *web.Route) {
}, openIDSignInEnabled)
m.Get("/sign_up", auth.SignUp)
m.Post("/sign_up", bindIgnErr(forms.RegisterForm{}), auth.SignUpPost)
- m.Group("/oauth2", func() {
- m.Get("/{provider}", auth.SignInOAuth)
- m.Get("/{provider}/callback", auth.SignInOAuthCallback)
- })
m.Get("/link_account", linkAccountEnabled, auth.LinkAccount)
m.Post("/link_account_signin", linkAccountEnabled, bindIgnErr(forms.SignInForm{}), auth.LinkAccountPostSignIn)
m.Post("/link_account_signup", linkAccountEnabled, bindIgnErr(forms.RegisterForm{}), auth.LinkAccountPostRegister)
@@ -452,6 +448,10 @@ func RegisterRoutes(m *web.Route) {
m.Get("/task/{task}", reqSignIn, user.TaskStatus)
m.Get("/stopwatches", reqSignIn, user.GetStopwatches)
m.Get("/search", ignExploreSignIn, user.Search)
+ m.Group("/oauth2", func() {
+ m.Get("/{provider}", auth.SignInOAuth)
+ m.Get("/{provider}/callback", auth.SignInOAuthCallback)
+ })
})
// ***** END: User *****