Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ceaf11c
Add setting to OAuth handlers to override local 2FA settings
zeripath Aug 21, 2021
ddc1966
Fix regression from #16544
zeripath Aug 20, 2021
ca74f82
Add scopes settings
zeripath Aug 20, 2021
c282f85
fix trace logging in auth_openid
zeripath Aug 20, 2021
0d4874f
add required claim options
zeripath Aug 20, 2021
bd86307
Move UpdateExternalUser to externalaccount
zeripath Aug 20, 2021
0e27070
Allow OAuth2/OIDC to set Admin/Restricted status
zeripath Aug 20, 2021
b51c09c
Allow use of the same group claim name for the prohibit login value
zeripath Aug 21, 2021
11791e5
fixup! Move UpdateExternalUser to externalaccount
zeripath Aug 21, 2021
de07c25
Merge branch 'main' into oidc-claims
zeripath Aug 29, 2021
ea37fd0
Merge branch 'main' into oidc-claims
zeripath Aug 29, 2021
a730e6b
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Sep 25, 2021
4818a72
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Sep 27, 2021
8b8abaa
as per wxiaoguang
zeripath Sep 27, 2021
7a88d06
add label back in
zeripath Oct 14, 2021
1f0d1a0
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Oct 14, 2021
7cd84d7
adjust localisation
zeripath Oct 14, 2021
d119bc8
Merge branch 'main' into oidc-claims
6543 Oct 15, 2021
125747a
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Nov 20, 2021
a5d3887
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Nov 27, 2021
7545100
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Nov 28, 2021
47ae8f5
placate lint
zeripath Nov 28, 2021
3f4df7c
Merge branch 'main' into oidc-claims
lunny Dec 10, 2021
bc558ed
Merge branch 'main' into oidc-claims
lunny Dec 10, 2021
36da1a6
Merge branch 'main' into oidc-claims
lunny Dec 13, 2021
6041ea8
Merge branch 'main' into oidc-claims
lunny Dec 13, 2021
706532a
Merge remote-tracking branch 'origin/main' into oidc-claims
zeripath Dec 13, 2021
33e3af2
Merge branch 'main' into oidc-claims
lunny Dec 14, 2021
323782f
Merge branch 'main' into oidc-claims
techknowlogick Dec 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move UpdateExternalUser to externalaccount
Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Aug 21, 2021
commit bd86307cecf7ca0b89d0f4df23154a0846c5e733
37 changes: 6 additions & 31 deletions models/external_login_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"code.gitea.io/gitea/modules/structs"

"github.com/markbates/goth"
"xorm.io/builder"
)

Expand Down Expand Up @@ -99,42 +98,18 @@ func GetUserIDByExternalUserID(provider, userID string) (int64, error) {
return id, nil
}

// UpdateExternalUser updates external user's information
func UpdateExternalUser(user *User, gothUser goth.User) error {
loginSource, err := GetActiveOAuth2LoginSourceByName(gothUser.Provider)
if err != nil {
return err
}
externalLoginUser := &ExternalLoginUser{
ExternalID: gothUser.UserID,
UserID: user.ID,
LoginSourceID: loginSource.ID,
RawData: gothUser.RawData,
Provider: gothUser.Provider,
Email: gothUser.Email,
Name: gothUser.Name,
FirstName: gothUser.FirstName,
LastName: gothUser.LastName,
NickName: gothUser.NickName,
Description: gothUser.Description,
AvatarURL: gothUser.AvatarURL,
Location: gothUser.Location,
AccessToken: gothUser.AccessToken,
AccessTokenSecret: gothUser.AccessTokenSecret,
RefreshToken: gothUser.RefreshToken,
ExpiresAt: gothUser.ExpiresAt,
}

has, err := x.Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).
// UpdateExternalUserByExternalID updates an external user's information
func UpdateExternalUserByExternalID(external *ExternalLoginUser) error {
has, err := x.Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID).
NoAutoCondition().
Exist(externalLoginUser)
Exist(external)
if err != nil {
return err
} else if !has {
return ErrExternalLoginUserNotExist{user.ID, loginSource.ID}
return ErrExternalLoginUserNotExist{external.UserID, external.LoginSourceID}
}

_, err = x.Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).AllCols().Update(externalLoginUser)
_, err = x.Where("external_id=? AND login_source_id=?", external.ExternalID, external.LoginSourceID).AllCols().Update(external)
return err
}

Expand Down
24 changes: 4 additions & 20 deletions routers/web/user/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,8 @@ func TwoFactorPost(ctx *context.Context) {
}

if ctx.Session.Get("linkAccount") != nil {
gothUser := ctx.Session.Get("linkAccountGothUser")
if gothUser == nil {
ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
return
}

err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
if err != nil {
if err := externalaccount.LinkAccountFromStore(ctx.Session, u); err != nil {
ctx.ServerError("UserSignIn", err)
return
}
}

Expand Down Expand Up @@ -470,16 +462,8 @@ func U2FSign(ctx *context.Context) {
}

if ctx.Session.Get("linkAccount") != nil {
gothUser := ctx.Session.Get("linkAccountGothUser")
if gothUser == nil {
ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
return
}

err = externalaccount.LinkAccountToUser(user, gothUser.(goth.User))
if err != nil {
if err := externalaccount.LinkAccountFromStore(ctx.Session, user); err != nil {
ctx.ServerError("UserSignIn", err)
return
}
}
redirect := handleSignInFull(ctx, user, remember, false)
Expand Down Expand Up @@ -739,7 +723,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *models.LoginSource, u *mod
}

// update external user information
if err := models.UpdateExternalUser(u, gothUser); err != nil {
if err := externalaccount.UpdateExternalUser(u, gothUser); err != nil {
log.Error("UpdateExternalUser failed: %v", err)
}

Expand Down Expand Up @@ -1321,7 +1305,7 @@ func handleUserCreated(ctx *context.Context, u *models.User, gothUser *goth.User

// update external user information
if gothUser != nil {
if err := models.UpdateExternalUser(u, *gothUser); err != nil {
if err := externalaccount.UpdateExternalUser(u, *gothUser); err != nil {
log.Error("UpdateExternalUser failed: %v", err)
}
}
Expand Down
28 changes: 28 additions & 0 deletions services/externalaccount/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package externalaccount

import (
"fmt"

"code.gitea.io/gitea/models"

"github.com/markbates/goth"
)

type Store interface {
Get(interface{}) interface{}
Set(interface{}, interface{}) error
Release() error
}

func LinkAccountFromStore(store Store, user *models.User) error {
gothUser := store.Get("linkAccountGothUser")
if gothUser == nil {
return fmt.Errorf("not in LinkAccount session")
}

return LinkAccountToUser(user, gothUser.(goth.User))
}
26 changes: 21 additions & 5 deletions services/externalaccount/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import (
"github.com/markbates/goth"
)

// LinkAccountToUser link the gothUser to the user
func LinkAccountToUser(user *models.User, gothUser goth.User) error {
func toExternalLoginUser(user *models.User, gothUser goth.User) (*models.ExternalLoginUser, error) {
loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
if err != nil {
return err
return nil, err
}

externalLoginUser := &models.ExternalLoginUser{
return &models.ExternalLoginUser{
ExternalID: gothUser.UserID,
UserID: user.ID,
LoginSourceID: loginSource.ID,
Expand All @@ -38,6 +36,14 @@ func LinkAccountToUser(user *models.User, gothUser goth.User) error {
AccessTokenSecret: gothUser.AccessTokenSecret,
RefreshToken: gothUser.RefreshToken,
ExpiresAt: gothUser.ExpiresAt,
}, nil
}

// LinkAccountToUser link the gothUser to the user
func LinkAccountToUser(user *models.User, gothUser goth.User) error {
externalLoginUser, err := toExternalLoginUser(user, gothUser)
if err != nil {
return err
}

if err := models.LinkExternalToUser(user, externalLoginUser); err != nil {
Expand All @@ -60,3 +66,13 @@ func LinkAccountToUser(user *models.User, gothUser goth.User) error {

return nil
}

// UpdateExternalUser updates external user's information
func UpdateExternalUser(user *models.User, gothUser goth.User) error {
externalLoginUser, err := toExternalLoginUser(user, gothUser)
if err != nil {
return err
}

return models.UpdateExternalUserByExternalID(externalLoginUser)
}