Auth: Introduce pre-logout hooks + add GCOM LogoutHook (#88475)
* Introduce preLogoutHooks in authn service * Add gcom_logout_hook * Config the api token from the Grafana config file * Simplify * Add tests for logout hook * Clean up * Update * Address PR comment * Fix
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/apikey"
|
||||
"github.com/grafana/grafana/pkg/services/auth"
|
||||
"github.com/grafana/grafana/pkg/services/auth/gcomsso"
|
||||
"github.com/grafana/grafana/pkg/services/authn"
|
||||
"github.com/grafana/grafana/pkg/services/authn/authnimpl/sync"
|
||||
"github.com/grafana/grafana/pkg/services/authn/clients"
|
||||
@@ -106,6 +107,7 @@ func ProvideRegistration(
|
||||
rbacSync := sync.ProvideRBACSync(accessControlService)
|
||||
if features.IsEnabledGlobally(featuremgmt.FlagCloudRBACRoles) {
|
||||
authnSvc.RegisterPostAuthHook(rbacSync.SyncCloudRoles, 110)
|
||||
authnSvc.RegisterPreLogoutHook(gcomsso.ProvideGComSSOService(cfg).LogoutHook, 50)
|
||||
}
|
||||
|
||||
authnSvc.RegisterPostAuthHook(rbacSync.SyncPermissionsHook, 120)
|
||||
|
||||
@@ -57,6 +57,7 @@ func ProvideService(
|
||||
tracer: tracer,
|
||||
metrics: newMetrics(registerer),
|
||||
sessionService: sessionService,
|
||||
preLogoutHooks: newQueue[authn.PreLogoutHookFn](),
|
||||
postAuthHooks: newQueue[authn.PostAuthHookFn](),
|
||||
postLoginHooks: newQueue[authn.PostLoginHookFn](),
|
||||
}
|
||||
@@ -83,6 +84,8 @@ type Service struct {
|
||||
postAuthHooks *queue[authn.PostAuthHookFn]
|
||||
// postLoginHooks are called after a login request is performed, both for failing and successful requests.
|
||||
postLoginHooks *queue[authn.PostLoginHookFn]
|
||||
// preLogoutHooks are called before a logout request is performed.
|
||||
preLogoutHooks *queue[authn.PreLogoutHookFn]
|
||||
}
|
||||
|
||||
func (s *Service) Authenticate(ctx context.Context, r *authn.Request) (*authn.Identity, error) {
|
||||
@@ -259,6 +262,10 @@ func (s *Service) RedirectURL(ctx context.Context, client string, r *authn.Reque
|
||||
return redirectClient.RedirectURL(ctx, r)
|
||||
}
|
||||
|
||||
func (s *Service) RegisterPreLogoutHook(hook authn.PreLogoutHookFn, priority uint) {
|
||||
s.preLogoutHooks.insert(hook, priority)
|
||||
}
|
||||
|
||||
func (s *Service) Logout(ctx context.Context, user authn.Requester, sessionToken *auth.UserToken) (*authn.Redirect, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "authn.Logout")
|
||||
defer span.End()
|
||||
@@ -278,6 +285,12 @@ func (s *Service) Logout(ctx context.Context, user authn.Requester, sessionToken
|
||||
return redirect, nil
|
||||
}
|
||||
|
||||
for _, hook := range s.preLogoutHooks.items {
|
||||
if err := hook.v(ctx, user, sessionToken); err != nil {
|
||||
s.log.Error("Failed to run pre logout hook. Skipping...", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
if authModule := user.GetAuthenticatedBy(); authModule != "" {
|
||||
client := authn.ClientWithPrefix(strings.TrimPrefix(authModule, "oauth_"))
|
||||
|
||||
|
||||
@@ -561,6 +561,7 @@ func setupTests(t *testing.T, opts ...func(svc *Service)) *Service {
|
||||
metrics: newMetrics(nil),
|
||||
postAuthHooks: newQueue[authn.PostAuthHookFn](),
|
||||
postLoginHooks: newQueue[authn.PostLoginHookFn](),
|
||||
preLogoutHooks: newQueue[authn.PreLogoutHookFn](),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
|
||||
Reference in New Issue
Block a user