From 73a729bbe8f77e0c120e9a0cf83daf84d6c4da53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez=20de=20la=20Franca=20Beltran?= <5459617+joanlopez@users.noreply.github.com> Date: Wed, 25 May 2022 10:00:21 +0200 Subject: [PATCH] Login: Fix AuthInfo update process (#49556) * Login: Fix AuthInfo update process * Fix GoDoc * Add regression test for oauth info overwrite Co-authored-by: jguer --- .../login/authinfoservice/database/database.go | 6 +++--- pkg/services/login/authinfoservice/user_auth_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/services/login/authinfoservice/database/database.go b/pkg/services/login/authinfoservice/database/database.go index b2c2ea070bc..434479d70a3 100644 --- a/pkg/services/login/authinfoservice/database/database.go +++ b/pkg/services/login/authinfoservice/database/database.go @@ -146,8 +146,8 @@ func (s *AuthInfoStore) SetAuthInfo(ctx context.Context, cmd *models.SetAuthInfo }) } -// UpdateAuthInfo updates the auth info for the user with the latest date. Avoids overlapping entries hiding -// the last used one (ex: LDAP->SAML->LDAP) +// UpdateAuthInfoDate updates the auth info for the user with the latest date. +// Avoids overlapping entries hiding the last used one (ex: LDAP->SAML->LDAP). func (s *AuthInfoStore) UpdateAuthInfoDate(ctx context.Context, authInfo *models.UserAuth) error { authInfo.Created = GetTime() @@ -157,7 +157,7 @@ func (s *AuthInfoStore) UpdateAuthInfoDate(ctx context.Context, authInfo *models AuthModule: authInfo.AuthModule, } return s.sqlStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error { - _, err := sess.Update(authInfo, cond) + _, err := sess.Cols("created").Update(authInfo, cond) return err }) } diff --git a/pkg/services/login/authinfoservice/user_auth_test.go b/pkg/services/login/authinfoservice/user_auth_test.go index 2e07458b436..e03640be847 100644 --- a/pkg/services/login/authinfoservice/user_auth_test.go +++ b/pkg/services/login/authinfoservice/user_auth_test.go @@ -278,6 +278,16 @@ func TestUserAuth(t *testing.T) { // Now reuse first auth module and make sure it's updated to the most recent database.GetTime = func() time.Time { return fixedTime } + + // add oauth info to auth_info to make sure update date does not overwrite it + updateAuthCmd := &models.UpdateAuthInfoCommand{UserId: user.Id, AuthModule: "test1", AuthId: "test1", OAuthToken: &oauth2.Token{ + AccessToken: "access_token", + TokenType: "token_type", + RefreshToken: "refresh_token", + Expiry: fixedTime, + }} + err = authInfoStore.UpdateAuthInfo(context.Background(), updateAuthCmd) + require.Nil(t, err) user, err = srv.LookupAndUpdate(context.Background(), queryOne) require.Nil(t, err) @@ -287,6 +297,8 @@ func TestUserAuth(t *testing.T) { require.Nil(t, err) require.Equal(t, "test1", getAuthQuery.Result.AuthModule) + // make sure oauth info is not overwritten by update date + require.Equal(t, "access_token", getAuthQuery.Result.OAuthAccessToken) // Now reuse second auth module and make sure it's updated to the most recent database.GetTime = func() time.Time { return fixedTime.AddDate(0, 0, 1) }