mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 01:12:09 +08:00

* wip * wip + tests * wip * wip opt2 * Use authn.Identity struct's SessionToken * Merge fixes * Handle disabling the feature flag correctly * Fix test * Cleanup * Remove HasOAuthEntry from the OAuthTokenService interface * Remove unused function
47 lines
1.6 KiB
Go
47 lines
1.6 KiB
Go
package oauthtokentest
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
|
"github.com/grafana/grafana/pkg/services/auth"
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
)
|
|
|
|
type MockOauthTokenService struct {
|
|
GetCurrentOauthTokenFunc func(ctx context.Context, usr identity.Requester, sessionToken *auth.UserToken) *oauth2.Token
|
|
IsOAuthPassThruEnabledFunc func(ds *datasources.DataSource) bool
|
|
InvalidateOAuthTokensFunc func(ctx context.Context, usr identity.Requester, sessionToken *auth.UserToken) error
|
|
TryTokenRefreshFunc func(ctx context.Context, usr identity.Requester, sessionToken *auth.UserToken) (*oauth2.Token, error)
|
|
}
|
|
|
|
func (m *MockOauthTokenService) GetCurrentOAuthToken(ctx context.Context, usr identity.Requester, sessionToken *auth.UserToken) *oauth2.Token {
|
|
if m.GetCurrentOauthTokenFunc != nil {
|
|
return m.GetCurrentOauthTokenFunc(ctx, usr, sessionToken)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockOauthTokenService) IsOAuthPassThruEnabled(ds *datasources.DataSource) bool {
|
|
if m.IsOAuthPassThruEnabledFunc != nil {
|
|
return m.IsOAuthPassThruEnabledFunc(ds)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (m *MockOauthTokenService) InvalidateOAuthTokens(ctx context.Context, usr identity.Requester, sessionToken *auth.UserToken) error {
|
|
if m.InvalidateOAuthTokensFunc != nil {
|
|
return m.InvalidateOAuthTokensFunc(ctx, usr, sessionToken)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockOauthTokenService) TryTokenRefresh(ctx context.Context, usr identity.Requester, sessionToken *auth.UserToken) (*oauth2.Token, error) {
|
|
if m.TryTokenRefreshFunc != nil {
|
|
return m.TryTokenRefreshFunc(ctx, usr, sessionToken)
|
|
}
|
|
return nil, nil
|
|
}
|