Login: Fix AuthInfo update process (#49556)

* Login: Fix AuthInfo update process

* Fix GoDoc

* Add regression test for oauth info overwrite

Co-authored-by: jguer <joao.guerreiro@grafana.com>
This commit is contained in:
Joan López de la Franca Beltran 2022-05-25 10:00:21 +02:00 committed by GitHub
parent eab806620c
commit 73a729bbe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -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
})
}

View File

@ -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) }