Auth: Introduce authn.SSOClientConfig to get client config from SSOSettings service (#94618)

* wip

* possible solution

* Separate interface for SSO settings clients

* Rename interface

* Fix tests

* Rename

* Change GetClientConfig to comma ok idiom
This commit is contained in:
Misi
2024-10-16 16:27:44 +02:00
committed by GitHub
parent c4f906f7fa
commit 50a635bc7e
14 changed files with 156 additions and 39 deletions
+42 -8
View File
@@ -8,16 +8,39 @@ import (
"github.com/grafana/grafana/pkg/services/authn"
)
var _ authn.Service = new(FakeService)
var _ authn.IdentitySynchronizer = new(FakeService)
var _ authn.SSOClientConfig = new(FakeSSOClientConfig)
type FakeSSOClientConfig struct {
ExpectedName string
ExpectedIsAutoLoginEnabled bool
ExpectedIsSingleLogoutEnabled bool
}
func (f *FakeSSOClientConfig) GetDisplayName() string {
return f.ExpectedName
}
func (f *FakeSSOClientConfig) IsAutoLoginEnabled() bool {
return f.ExpectedIsAutoLoginEnabled
}
func (f *FakeSSOClientConfig) IsSingleLogoutEnabled() bool {
return f.ExpectedIsSingleLogoutEnabled
}
var (
_ authn.Service = new(FakeService)
_ authn.IdentitySynchronizer = new(FakeService)
)
type FakeService struct {
ExpectedErr error
ExpectedRedirect *authn.Redirect
ExpectedIdentity *authn.Identity
ExpectedErrs []error
ExpectedIdentities []*authn.Identity
CurrentIndex int
ExpectedClientConfig authn.SSOClientConfig
ExpectedErr error
ExpectedRedirect *authn.Redirect
ExpectedIdentity *authn.Identity
ExpectedErrs []error
ExpectedIdentities []*authn.Identity
CurrentIndex int
}
func (f *FakeService) Authenticate(ctx context.Context, r *authn.Request) (*authn.Identity, error) {
@@ -44,6 +67,13 @@ func (f *FakeService) IsClientEnabled(name string) bool {
return true
}
func (f *FakeService) GetClientConfig(name string) (authn.SSOClientConfig, bool) {
if f.ExpectedClientConfig == nil {
return nil, false
}
return f.ExpectedClientConfig, true
}
func (f *FakeService) RegisterPostAuthHook(hook authn.PostAuthHookFn, priority uint) {}
func (f *FakeService) RegisterPreLogoutHook(hook authn.PreLogoutHookFn, priority uint) {}
@@ -127,6 +157,10 @@ func (f *FakeClient) Authenticate(ctx context.Context, r *authn.Request) (*authn
func (f FakeClient) IsEnabled() bool { return true }
func (f *FakeClient) GetConfig() authn.SSOClientConfig {
return nil
}
func (f *FakeClient) Test(ctx context.Context, r *authn.Request) bool {
return f.ExpectedTest
}
+18 -6
View File
@@ -9,8 +9,10 @@ import (
"github.com/grafana/grafana/pkg/services/authn"
)
var _ authn.Service = new(MockService)
var _ authn.IdentitySynchronizer = new(MockService)
var (
_ authn.Service = new(MockService)
_ authn.IdentitySynchronizer = new(MockService)
)
type MockService struct {
SyncIdentityFunc func(ctx context.Context, identity *authn.Identity) error
@@ -25,6 +27,10 @@ func (m *MockService) IsClientEnabled(name string) bool {
panic("unimplemented")
}
func (m *MockService) GetClientConfig(name string) (authn.SSOClientConfig, bool) {
panic("unimplemented")
}
func (m *MockService) Login(ctx context.Context, client string, r *authn.Request) (*authn.Identity, error) {
panic("unimplemented")
}
@@ -66,10 +72,12 @@ func (m *MockService) SyncIdentity(ctx context.Context, identity *authn.Identity
return nil
}
var _ authn.HookClient = new(MockClient)
var _ authn.LogoutClient = new(MockClient)
var _ authn.ContextAwareClient = new(MockClient)
var _ authn.IdentityResolverClient = new(MockClient)
var (
_ authn.HookClient = new(MockClient)
_ authn.LogoutClient = new(MockClient)
_ authn.ContextAwareClient = new(MockClient)
_ authn.IdentityResolverClient = new(MockClient)
)
type MockClient struct {
NameFunc func() string
@@ -100,6 +108,10 @@ func (m MockClient) IsEnabled() bool {
return true
}
func (m MockClient) GetConfig() authn.SSOClientConfig {
return nil
}
func (m MockClient) Test(ctx context.Context, r *authn.Request) bool {
if m.TestFunc != nil {
return m.TestFunc(ctx, r)