diff --git a/conf/defaults.ini b/conf/defaults.ini index 640c93856db..61204f45577 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -857,7 +857,6 @@ auto_sign_up = false url_login = false allow_assign_grafana_admin = false skip_org_role_sync = false -signout_redirect_url = #################################### Auth LDAP ########################### [auth.ldap] diff --git a/pkg/api/login.go b/pkg/api/login.go index 8a4d1524d73..7f758150b36 100644 --- a/pkg/api/login.go +++ b/pkg/api/login.go @@ -258,6 +258,7 @@ func (hs *HTTPServer) Logout(c *contextmodel.ReqContext) { if err != nil { hs.log.Error("Failed perform proper logout", "error", err) c.Redirect(hs.Cfg.AppSubURL + "/login") + return } _, id := c.SignedInUser.GetNamespacedID() diff --git a/pkg/services/authn/authnimpl/service.go b/pkg/services/authn/authnimpl/service.go index 28874c37943..ae84de3a42e 100644 --- a/pkg/services/authn/authnimpl/service.go +++ b/pkg/services/authn/authnimpl/service.go @@ -345,6 +345,9 @@ func (s *Service) Logout(ctx context.Context, user identity.Requester, sessionTo defer span.End() redirect := &authn.Redirect{URL: s.cfg.AppSubURL + "/login"} + if s.cfg.SignoutRedirectUrl != "" { + redirect.URL = s.cfg.SignoutRedirectUrl + } namespace, id := user.GetNamespacedID() if namespace != authn.NamespaceUser { @@ -353,7 +356,7 @@ func (s *Service) Logout(ctx context.Context, user identity.Requester, sessionTo userID, err := identity.IntIdentifier(namespace, id) if err != nil { - s.log.FromContext(ctx).Debug("Invalid user id", "id", userID, "err", err) + s.log.FromContext(ctx).Debug("Invalid user id", "id", id, "err", err) return redirect, nil } @@ -382,7 +385,7 @@ func (s *Service) Logout(ctx context.Context, user identity.Requester, sessionTo } Default: - if err = s.sessionService.RevokeToken(ctx, sessionToken, false); err != nil { + if err = s.sessionService.RevokeToken(ctx, sessionToken, false); err != nil && !errors.Is(err, auth.ErrUserTokenNotFound) { return nil, err } diff --git a/pkg/services/authn/authnimpl/service_test.go b/pkg/services/authn/authnimpl/service_test.go index b053569d5ee..08b7f8b6af6 100644 --- a/pkg/services/authn/authnimpl/service_test.go +++ b/pkg/services/authn/authnimpl/service_test.go @@ -311,7 +311,8 @@ func TestService_Logout(t *testing.T) { sessionToken *usertoken.UserToken info *login.UserAuth - client authn.Client + client authn.Client + signoutRedirectURL string expectedErr error expectedTokenRevoked bool @@ -345,6 +346,14 @@ func TestService_Logout(t *testing.T) { client: &authntest.FakeClient{ExpectedName: "auth.client.azuread"}, expectedTokenRevoked: true, }, + { + desc: "should use signout redirect url if configured", + identity: &authn.Identity{ID: authn.NamespacedID(authn.NamespaceUser, 1), AuthenticatedBy: "azuread"}, + expectedRedirect: &authn.Redirect{URL: "some-url"}, + client: &authntest.FakeClient{ExpectedName: "auth.client.azuread"}, + signoutRedirectURL: "some-url", + expectedTokenRevoked: true, + }, { desc: "should redirect to client specific url", identity: &authn.Identity{ID: authn.NamespacedID(authn.NamespaceUser, 1)}, @@ -381,6 +390,10 @@ func TestService_Logout(t *testing.T) { return nil }, } + + if tt.signoutRedirectURL != "" { + svc.cfg.SignoutRedirectUrl = tt.signoutRedirectURL + } }) redirect, err := s.Logout(context.Background(), tt.identity, tt.sessionToken)