Generic OAuth: Prevent adding duplicated users (#32286)

* Add special check for generic oauth case

* Converted from Convey to testify

* Fix according to reviewer's comments

* More changes according to reviewer's comments

* Handle error if user is not found

* Move generic oauth test from user_test.go to user_auth_test.go

* Update pkg/services/sqlstore/user_auth_test.go

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>

* Created genericOAuthModule const

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
This commit is contained in:
Dimitris Sotirakis
2021-04-09 13:28:35 +02:00
committed by GitHub
co-authored by Marcus Efraimsson
parent e6a98ce1e4
commit b867ceda9b
3 changed files with 715 additions and 631 deletions
+14 -1
View File
@@ -13,6 +13,8 @@ import (
var getTime = time.Now
const genericOAuthModule = "oauth_generic_oauth"
func init() {
bus.AddHandler("sql", GetUserByAuthInfo)
bus.AddHandler("sql", GetExternalUserInfoByLogin)
@@ -101,7 +103,17 @@ func GetUserByAuthInfo(query *models.GetUserByAuthInfoQuery) error {
return models.ErrUserNotFound
}
// create authInfo record to link accounts
// Special case for generic oauth duplicates
if query.AuthModule == genericOAuthModule && user.Id != 0 {
authQuery.UserId = user.Id
authQuery.AuthModule = query.AuthModule
err = GetAuthInfo(authQuery)
if !errors.Is(err, models.ErrUserNotFound) {
if err != nil {
return err
}
}
}
if authQuery.Result == nil && query.AuthModule != "" {
cmd2 := &models.SetAuthInfoCommand{
UserId: user.Id,
@@ -151,6 +163,7 @@ func GetAuthInfo(query *models.GetAuthInfoQuery) error {
if err != nil {
return err
}
if !has {
return models.ErrUserNotFound
}