@@ -5,7 +5,6 @@ package auth
55
66import  (
77	"errors" 
8- 	"fmt" 
98	"net/http" 
109	"strings" 
1110
@@ -21,8 +20,6 @@ import (
2120	"code.gitea.io/gitea/services/context" 
2221	"code.gitea.io/gitea/services/externalaccount" 
2322	"code.gitea.io/gitea/services/forms" 
24- 
25- 	"github.com/markbates/goth" 
2623)
2724
2825var  tplLinkAccount  templates.TplName  =  "user/auth/link_account" 
@@ -52,28 +49,28 @@ func LinkAccount(ctx *context.Context) {
5249	ctx .Data ["SignInLink" ] =  setting .AppSubURL  +  "/user/link_account_signin" 
5350	ctx .Data ["SignUpLink" ] =  setting .AppSubURL  +  "/user/link_account_signup" 
5451
55- 	gothUser ,  ok   :=  ctx . Session . Get ( "linkAccountGothUser" ).(goth. User )
52+ 	linkAccountData   :=  oauth2GetLinkAccountData ( ctx )
5653
5754	// If you'd like to quickly debug the "link account" page layout, just uncomment the blow line 
5855	// Don't worry, when the below line exists, the lint won't pass: ineffectual assignment to gothUser (ineffassign) 
59- 	// gothUser, ok = goth.User{Email: "invalid-email", Name: "."}, true  // intentionally use invalid data to avoid pass the registration check 
56+ 	// linkAccountData = &LinkAccountData{authSource, gothUser}  // intentionally use invalid data to avoid pass the registration check 
6057
61- 	if  ! ok  {
58+ 	if  linkAccountData   ==   nil  {
6259		// no account in session, so just redirect to the login page, then the user could restart the process 
6360		ctx .Redirect (setting .AppSubURL  +  "/user/login" )
6461		return 
6562	}
6663
67- 	if  missingFields , ok  :=  gothUser .RawData ["__giteaAutoRegMissingFields" ].([]string ); ok  {
68- 		ctx .Data ["AutoRegistrationFailedPrompt" ] =  ctx .Tr ("auth.oauth_callback_unable_auto_reg" , gothUser .Provider , strings .Join (missingFields , "," ))
64+ 	if  missingFields , ok  :=  linkAccountData . GothUser .RawData ["__giteaAutoRegMissingFields" ].([]string ); ok  {
65+ 		ctx .Data ["AutoRegistrationFailedPrompt" ] =  ctx .Tr ("auth.oauth_callback_unable_auto_reg" , linkAccountData . GothUser .Provider , strings .Join (missingFields , "," ))
6966	}
7067
71- 	uname , err  :=  extractUserNameFromOAuth2 (& gothUser )
68+ 	uname , err  :=  extractUserNameFromOAuth2 (& linkAccountData . GothUser )
7269	if  err  !=  nil  {
7370		ctx .ServerError ("UserSignIn" , err )
7471		return 
7572	}
76- 	email  :=  gothUser .Email 
73+ 	email  :=  linkAccountData . GothUser .Email 
7774	ctx .Data ["user_name" ] =  uname 
7875	ctx .Data ["email" ] =  email 
7976
@@ -152,8 +149,8 @@ func LinkAccountPostSignIn(ctx *context.Context) {
152149	ctx .Data ["SignInLink" ] =  setting .AppSubURL  +  "/user/link_account_signin" 
153150	ctx .Data ["SignUpLink" ] =  setting .AppSubURL  +  "/user/link_account_signup" 
154151
155- 	gothUser  :=  ctx . Session . Get ( "linkAccountGothUser" )
156- 	if  gothUser  ==  nil  {
152+ 	linkAccountData  :=  oauth2GetLinkAccountData ( ctx )
153+ 	if  linkAccountData  ==  nil  {
157154		ctx .ServerError ("UserSignIn" , errors .New ("not in LinkAccount session" ))
158155		return 
159156	}
@@ -169,11 +166,11 @@ func LinkAccountPostSignIn(ctx *context.Context) {
169166		return 
170167	}
171168
172- 	linkAccount (ctx , u , gothUser .(goth. User ) , signInForm .Remember )
169+ 	oauth2LinkAccount (ctx , u , linkAccountData , signInForm .Remember )
173170}
174171
175- func  linkAccount (ctx  * context.Context , u  * user_model.User , gothUser  goth. User , remember  bool ) {
176- 	updateAvatarIfNeed (ctx , gothUser .AvatarURL , u )
172+ func  oauth2LinkAccount (ctx  * context.Context , u  * user_model.User , linkAccountData   * LinkAccountData , remember  bool ) {
173+ 	// no need to call  updateAvatarIfNeed(ctx, gothUser.AvatarURL, u) be cause 
177174
178175	// If this user is enrolled in 2FA, we can't sign the user in just yet. 
179176	// Instead, redirect them to the 2FA authentication page. 
@@ -185,7 +182,7 @@ func linkAccount(ctx *context.Context, u *user_model.User, gothUser goth.User, r
185182			return 
186183		}
187184
188- 		err  =  externalaccount .LinkAccountToUser (ctx , u , gothUser )
185+ 		err  =  externalaccount .LinkAccountToUser (ctx , linkAccountData . AuthSource . ID ,  u , linkAccountData . GothUser )
189186		if  err  !=  nil  {
190187			ctx .ServerError ("UserLinkAccount" , err )
191188			return 
@@ -243,17 +240,11 @@ func LinkAccountPostRegister(ctx *context.Context) {
243240	ctx .Data ["SignInLink" ] =  setting .AppSubURL  +  "/user/link_account_signin" 
244241	ctx .Data ["SignUpLink" ] =  setting .AppSubURL  +  "/user/link_account_signup" 
245242
246- 	gothUserInterface  :=  ctx . Session . Get ( "linkAccountGothUser" )
247- 	if  gothUserInterface  ==  nil  {
243+ 	linkAccountData  :=  oauth2GetLinkAccountData ( ctx )
244+ 	if  linkAccountData  ==  nil  {
248245		ctx .ServerError ("UserSignUp" , errors .New ("not in LinkAccount session" ))
249246		return 
250247	}
251- 	gothUser , ok  :=  gothUserInterface .(goth.User )
252- 	if  ! ok  {
253- 		ctx .ServerError ("UserSignUp" , fmt .Errorf ("session linkAccountGothUser type is %t but not goth.User" , gothUserInterface ))
254- 		return 
255- 	}
256- 
257248	if  ctx .HasError () {
258249		ctx .HTML (http .StatusOK , tplLinkAccount )
259250		return 
@@ -296,31 +287,33 @@ func LinkAccountPostRegister(ctx *context.Context) {
296287		}
297288	}
298289
299- 	authSource , err  :=  auth .GetActiveOAuth2SourceByName (ctx , gothUser .Provider )
300- 	if  err  !=  nil  {
301- 		ctx .ServerError ("CreateUser" , err )
302- 		return 
303- 	}
304- 
305290	u  :=  & user_model.User {
306291		Name :        form .UserName ,
307292		Email :       form .Email ,
308293		Passwd :      form .Password ,
309294		LoginType :   auth .OAuth2 ,
310- 		LoginSource : authSource .ID ,
311- 		LoginName :   gothUser .UserID ,
295+ 		LoginSource : linkAccountData . AuthSource .ID ,
296+ 		LoginName :   linkAccountData . GothUser .UserID ,
312297	}
313298
314- 	if  ! createAndHandleCreatedUser (ctx , tplLinkAccount , form , u , nil , & gothUser ,  false ) {
299+ 	if  ! createAndHandleCreatedUser (ctx , tplLinkAccount , form , u , nil , linkAccountData ) {
315300		// error already handled 
316301		return 
317302	}
318303
319- 	source  :=  authSource .Cfg .(* oauth2.Source )
320- 	if  err  :=  syncGroupsToTeams (ctx , source , & gothUser , u ); err  !=  nil  {
304+ 	source  :=  linkAccountData . AuthSource .Cfg .(* oauth2.Source )
305+ 	if  err  :=  syncGroupsToTeams (ctx , source , & linkAccountData . GothUser , u ); err  !=  nil  {
321306		ctx .ServerError ("SyncGroupsToTeams" , err )
322307		return 
323308	}
324309
325310	handleSignIn (ctx , u , false )
326311}
312+ 
313+ func  linkAccountFromContext (ctx  * context.Context , user  * user_model.User ) error  {
314+ 	linkAccountData  :=  oauth2GetLinkAccountData (ctx )
315+ 	if  linkAccountData  ==  nil  {
316+ 		return  errors .New ("not in LinkAccount session" )
317+ 	}
318+ 	return  externalaccount .LinkAccountToUser (ctx , linkAccountData .AuthSource .ID , user , linkAccountData .GothUser )
319+ }
0 commit comments