AuthInfo: Revert #81013. Fix cache invalidation (#81050)

* Revert "Auth: Revert "Auth: Cache Auth Info" (#81013)"

This reverts commit ce84f7c5405b9696bbe443409b5130db490856cf.

* fix cache invalidation during user takeover

* fix incomplete test
This commit is contained in:
Jo
2024-01-23 15:26:38 +01:00
committed by GitHub
parent 8ff4e488d9
commit 9f5a8bf926
4 changed files with 185 additions and 16 deletions

View File

@ -10,12 +10,16 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
"golang.org/x/sync/singleflight"
"github.com/grafana/grafana/pkg/infra/remotecache"
"github.com/grafana/grafana/pkg/login/social/socialtest"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/login/authinfoimpl"
"github.com/grafana/grafana/pkg/services/secrets/fakes"
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
)
@ -114,13 +118,12 @@ func TestService_TryTokenRefresh_ValidToken(t *testing.T) {
socialConnector.On("TokenSource", mock.Anything, mock.Anything).Return(oauth2.StaticTokenSource(token))
err := srv.TryTokenRefresh(ctx, usr)
assert.Nil(t, err)
require.Nil(t, err)
socialConnector.AssertNumberOfCalls(t, "TokenSource", 1)
authInfoQuery := &login.GetAuthInfoQuery{}
authInfoQuery := &login.GetAuthInfoQuery{UserId: 1}
resultUsr, err := srv.AuthInfoService.GetAuthInfo(ctx, authInfoQuery)
assert.Nil(t, err)
require.Nil(t, err)
// User's token data had not been updated
assert.Equal(t, resultUsr.OAuthAccessToken, token.AccessToken)
@ -175,6 +178,8 @@ func TestService_TryTokenRefresh_ExpiredToken(t *testing.T) {
usr := &login.UserAuth{
AuthModule: "oauth_generic_oauth",
UserId: 1,
AuthId: "test",
OAuthAccessToken: token.AccessToken,
OAuthRefreshToken: token.RefreshToken,
OAuthExpiry: token.Expiry,
@ -187,13 +192,13 @@ func TestService_TryTokenRefresh_ExpiredToken(t *testing.T) {
err := srv.TryTokenRefresh(ctx, usr)
assert.Nil(t, err)
require.Nil(t, err)
socialConnector.AssertNumberOfCalls(t, "TokenSource", 1)
authInfoQuery := &login.GetAuthInfoQuery{}
authInfoQuery := &login.GetAuthInfoQuery{UserId: 1}
authInfo, err := srv.AuthInfoService.GetAuthInfo(ctx, authInfoQuery)
assert.Nil(t, err)
require.Nil(t, err)
// newToken should be returned after the .Token() call, therefore the User had to be updated
assert.Equal(t, authInfo.OAuthAccessToken, newToken.AccessToken)
@ -229,7 +234,8 @@ func setupOAuthTokenService(t *testing.T) (*Service, *FakeAuthInfoStore, *social
}
authInfoStore := &FakeAuthInfoStore{}
authInfoService := authinfoimpl.ProvideService(authInfoStore)
authInfoService := authinfoimpl.ProvideService(authInfoStore, remotecache.NewFakeCacheStorage(),
secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore()))
return &Service{
Cfg: setting.NewCfg(),
SocialService: socialService,