mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 04:22:13 +08:00

* Refactor OAuthToken service * introduce user.SessionAwareIdentityRequester * replace login.UserAuth parameters with user.SessionAwareIdentityRequester * Add nosec G101 to fake ID tokens * Opt 2, min changes * Revert a change to the current version
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package oauthtokentest
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
"github.com/grafana/grafana/pkg/services/login"
|
|
)
|
|
|
|
type MockOauthTokenService struct {
|
|
GetCurrentOauthTokenFunc func(ctx context.Context, usr identity.Requester) *oauth2.Token
|
|
IsOAuthPassThruEnabledFunc func(ds *datasources.DataSource) bool
|
|
HasOAuthEntryFunc func(ctx context.Context, usr identity.Requester) (*login.UserAuth, bool, error)
|
|
InvalidateOAuthTokensFunc func(ctx context.Context, usr identity.Requester) error
|
|
TryTokenRefreshFunc func(ctx context.Context, usr identity.Requester) (*oauth2.Token, error)
|
|
}
|
|
|
|
func (m *MockOauthTokenService) GetCurrentOAuthToken(ctx context.Context, usr identity.Requester) *oauth2.Token {
|
|
if m.GetCurrentOauthTokenFunc != nil {
|
|
return m.GetCurrentOauthTokenFunc(ctx, usr)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockOauthTokenService) IsOAuthPassThruEnabled(ds *datasources.DataSource) bool {
|
|
if m.IsOAuthPassThruEnabledFunc != nil {
|
|
return m.IsOAuthPassThruEnabledFunc(ds)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (m *MockOauthTokenService) HasOAuthEntry(ctx context.Context, usr identity.Requester) (*login.UserAuth, bool, error) {
|
|
if m.HasOAuthEntryFunc != nil {
|
|
return m.HasOAuthEntryFunc(ctx, usr)
|
|
}
|
|
return nil, false, nil
|
|
}
|
|
|
|
func (m *MockOauthTokenService) InvalidateOAuthTokens(ctx context.Context, usr identity.Requester) error {
|
|
if m.InvalidateOAuthTokensFunc != nil {
|
|
return m.InvalidateOAuthTokensFunc(ctx, usr)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockOauthTokenService) TryTokenRefresh(ctx context.Context, usr identity.Requester) (*oauth2.Token, error) {
|
|
if m.TryTokenRefreshFunc != nil {
|
|
return m.TryTokenRefreshFunc(ctx, usr)
|
|
}
|
|
return nil, nil
|
|
}
|