Skip to content

Commit b5ae054

Browse files
committed
fix
1 parent ddfa2e4 commit b5ae054

File tree

23 files changed

+396
-177
lines changed

23 files changed

+396
-177
lines changed

models/asymkey/ssh_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,13 @@ func AddPublicKeysBySource(ctx context.Context, usr *user_model.User, s *auth.So
355355
return sshKeysNeedUpdate
356356
}
357357

358-
// SynchronizePublicKeys updates a users public keys. Returns true if there are changes.
358+
// SynchronizePublicKeys updates a user's public keys. Returns true if there are changes.
359359
func SynchronizePublicKeys(ctx context.Context, usr *user_model.User, s *auth.Source, sshPublicKeys []string) bool {
360360
var sshKeysNeedUpdate bool
361361

362362
log.Trace("synchronizePublicKeys[%s]: Handling Public SSH Key synchronization for user %s", s.Name, usr.Name)
363363

364-
// Get Public Keys from DB with current LDAP source
364+
// Get Public Keys from DB with the current auth source
365365
var giteaKeys []string
366366
keys, err := db.Find[PublicKey](ctx, FindPublicKeyOptions{
367367
OwnerID: usr.ID,

models/auth/oauth2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ func (err ErrOAuthApplicationNotFound) Unwrap() error {
612612
return util.ErrNotExist
613613
}
614614

615-
// GetActiveOAuth2SourceByName returns a OAuth2 AuthSource based on the given name
616-
func GetActiveOAuth2SourceByName(ctx context.Context, name string) (*Source, error) {
615+
// GetActiveOAuth2SourceByAuthName returns a OAuth2 AuthSource based on the given name
616+
func GetActiveOAuth2SourceByAuthName(ctx context.Context, name string) (*Source, error) {
617617
authSource := new(Source)
618618
has, err := db.GetEngine(ctx).Where("name = ? and type = ? and is_active = ?", name, OAuth2, true).Get(authSource)
619619
if err != nil {

modules/setting/oauth2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/log"
1313
)
1414

15-
// OAuth2UsernameType is enum describing the way gitea 'name' should be generated from oauth2 data
15+
// OAuth2UsernameType is enum describing the way gitea generates its 'username' from oauth2 data
1616
type OAuth2UsernameType string
1717

1818
const (

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,6 +3202,7 @@ auths.attribute_name = First Name Attribute
32023202
auths.attribute_surname = Surname Attribute
32033203
auths.attribute_mail = Email Attribute
32043204
auths.attribute_ssh_public_key = Public SSH Key Attribute
3205+
auths.attribute_full_name = Full Name Attribute
32053206
auths.attribute_avatar = Avatar Attribute
32063207
auths.attributes_in_bind = Fetch Attributes in Bind DN Context
32073208
auths.allow_deactivate_all = Allow an empty search result to deactivate all users

routers/web/admin/auths.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
199199
AdminGroup: form.Oauth2AdminGroup,
200200
GroupTeamMap: form.Oauth2GroupTeamMap,
201201
GroupTeamMapRemoval: form.Oauth2GroupTeamMapRemoval,
202+
203+
AttributeSSHPublicKey: form.Oauth2AttributeSSHPublicKey,
204+
AttributeFullName: form.Oauth2AttributeFullName,
202205
}
203206
}
204207

routers/web/auth/2fa.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"code.gitea.io/gitea/modules/templates"
1515
"code.gitea.io/gitea/modules/web"
1616
"code.gitea.io/gitea/services/context"
17-
"code.gitea.io/gitea/services/externalaccount"
1817
"code.gitea.io/gitea/services/forms"
1918
)
2019

@@ -75,7 +74,7 @@ func TwoFactorPost(ctx *context.Context) {
7574
}
7675

7776
if ctx.Session.Get("linkAccount") != nil {
78-
err = externalaccount.LinkAccountFromStore(ctx, ctx.Session, u)
77+
err = linkAccountFromContext(ctx, u)
7978
if err != nil {
8079
ctx.ServerError("UserSignIn", err)
8180
return

routers/web/auth/auth.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ func handleSignInFull(ctx *context.Context, u *user_model.User, remember, obeyRe
329329
"twofaUid",
330330
"twofaRemember",
331331
"linkAccount",
332+
"linkAccountData",
332333
}, map[string]any{
333334
session.KeyUID: u.ID,
334335
session.KeyUname: u.Name,
@@ -519,7 +520,7 @@ func SignUpPost(ctx *context.Context) {
519520
Passwd: form.Password,
520521
}
521522

522-
if !createAndHandleCreatedUser(ctx, tplSignUp, form, u, nil, nil, false) {
523+
if !createAndHandleCreatedUser(ctx, tplSignUp, form, u, nil, nil) {
523524
// error already handled
524525
return
525526
}
@@ -530,22 +531,22 @@ func SignUpPost(ctx *context.Context) {
530531

531532
// createAndHandleCreatedUser calls createUserInContext and
532533
// then handleUserCreated.
533-
func createAndHandleCreatedUser(ctx *context.Context, tpl templates.TplName, form any, u *user_model.User, overwrites *user_model.CreateUserOverwriteOptions, gothUser *goth.User, allowLink bool) bool {
534-
if !createUserInContext(ctx, tpl, form, u, overwrites, gothUser, allowLink) {
534+
func createAndHandleCreatedUser(ctx *context.Context, tpl templates.TplName, form any, u *user_model.User, overwrites *user_model.CreateUserOverwriteOptions, possibleLinkAccountData *LinkAccountData) bool {
535+
if !createUserInContext(ctx, tpl, form, u, overwrites, possibleLinkAccountData) {
535536
return false
536537
}
537-
return handleUserCreated(ctx, u, gothUser)
538+
return handleUserCreated(ctx, u, possibleLinkAccountData)
538539
}
539540

540541
// createUserInContext creates a user and handles errors within a given context.
541-
// Optionally a template can be specified.
542-
func createUserInContext(ctx *context.Context, tpl templates.TplName, form any, u *user_model.User, overwrites *user_model.CreateUserOverwriteOptions, gothUser *goth.User, allowLink bool) (ok bool) {
542+
// Optionally, a template can be specified.
543+
func createUserInContext(ctx *context.Context, tpl templates.TplName, form any, u *user_model.User, overwrites *user_model.CreateUserOverwriteOptions, possibleLinkAccountData *LinkAccountData) (ok bool) {
543544
meta := &user_model.Meta{
544545
InitialIP: ctx.RemoteAddr(),
545546
InitialUserAgent: ctx.Req.UserAgent(),
546547
}
547548
if err := user_model.CreateUser(ctx, u, meta, overwrites); err != nil {
548-
if allowLink && (user_model.IsErrUserAlreadyExist(err) || user_model.IsErrEmailAlreadyUsed(err)) {
549+
if possibleLinkAccountData != nil && (user_model.IsErrUserAlreadyExist(err) || user_model.IsErrEmailAlreadyUsed(err)) {
549550
switch setting.OAuth2Client.AccountLinking {
550551
case setting.OAuth2AccountLinkingAuto:
551552
var user *user_model.User
@@ -561,15 +562,15 @@ func createUserInContext(ctx *context.Context, tpl templates.TplName, form any,
561562
}
562563

563564
// TODO: probably we should respect 'remember' user's choice...
564-
linkAccount(ctx, user, *gothUser, true)
565+
oauth2LinkAccount(ctx, user, possibleLinkAccountData, true)
565566
return false // user is already created here, all redirects are handled
566567
case setting.OAuth2AccountLinkingLogin:
567-
showLinkingLogin(ctx, *gothUser)
568+
showLinkingLogin(ctx, &possibleLinkAccountData.AuthSource, possibleLinkAccountData.GothUser)
568569
return false // user will be created only after linking login
569570
}
570571
}
571572

572-
// handle error without template
573+
// handle error without a template
573574
if len(tpl) == 0 {
574575
ctx.ServerError("CreateUser", err)
575576
return false
@@ -610,7 +611,7 @@ func createUserInContext(ctx *context.Context, tpl templates.TplName, form any,
610611
// handleUserCreated does additional steps after a new user is created.
611612
// It auto-sets admin for the only user, updates the optional external user and
612613
// sends a confirmation email if required.
613-
func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.User) (ok bool) {
614+
func handleUserCreated(ctx *context.Context, u *user_model.User, possibleLinkAccountData *LinkAccountData) (ok bool) {
614615
// Auto-set admin for the only user.
615616
hasUsers, err := user_model.HasUsers(ctx)
616617
if err != nil {
@@ -631,8 +632,8 @@ func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.
631632
}
632633

633634
// update external user information
634-
if gothUser != nil {
635-
if err := externalaccount.EnsureLinkExternalToUser(ctx, u, *gothUser); err != nil {
635+
if possibleLinkAccountData != nil {
636+
if err := externalaccount.EnsureLinkExternalToUser(ctx, possibleLinkAccountData.AuthSource.ID, u, possibleLinkAccountData.GothUser); err != nil {
636637
log.Error("EnsureLinkExternalToUser failed: %v", err)
637638
}
638639
}

routers/web/auth/linkaccount.go

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package auth
55

66
import (
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

2825
var 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

Comments
 (0)