From 3270a9c959dc332967bbf49307625ba79e046613 Mon Sep 17 00:00:00 2001 From: linoman <2051016+linoman@users.noreply.github.com> Date: Tue, 22 Oct 2024 10:57:36 +0200 Subject: [PATCH] Session Refactor: Add auth module label to session's list (#94958) * Add AuthModule to token API * Add badge to UserSessions * Change idp label rendering * Render IdP label for User Profile page * Add i18n labels --- pkg/api/dtos/user_token.go | 1 + pkg/api/user_token.go | 8 ++++++++ pkg/services/auth/authtest/testing.go | 4 ++++ public/app/features/admin/UserSessions.tsx | 10 +++++++++- public/app/features/admin/state/actions.ts | 1 + public/app/features/profile/UserSessions.tsx | 7 +++++++ public/app/features/profile/state/reducers.ts | 1 + public/app/types/user.ts | 1 + public/locales/en-US/grafana.json | 2 ++ public/locales/pseudo-LOCALE/grafana.json | 2 ++ 10 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkg/api/dtos/user_token.go b/pkg/api/dtos/user_token.go index 327aad85866..2409c3e09e1 100644 --- a/pkg/api/dtos/user_token.go +++ b/pkg/api/dtos/user_token.go @@ -11,6 +11,7 @@ type UserToken struct { OperatingSystemVersion string `json:"osVersion"` Browser string `json:"browser"` BrowserVersion string `json:"browserVersion"` + AuthModule string `json:"authModule"` CreatedAt time.Time `json:"createdAt"` SeenAt time.Time `json:"seenAt"` } diff --git a/pkg/api/user_token.go b/pkg/api/user_token.go index 12894a1a103..7770fee84ea 100644 --- a/pkg/api/user_token.go +++ b/pkg/api/user_token.go @@ -15,6 +15,7 @@ import ( "github.com/grafana/grafana/pkg/services/auth" "github.com/grafana/grafana/pkg/services/authn" contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" + "github.com/grafana/grafana/pkg/services/login" "github.com/grafana/grafana/pkg/services/user" "github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/web" @@ -224,6 +225,12 @@ func (hs *HTTPServer) getUserAuthTokensInternal(c *contextmodel.ReqContext, user seenAt = createdAt } + // Retrieve AuthModule from external session + authModule := "" + if externalSession, err := hs.AuthTokenService.GetExternalSession(c.Req.Context(), token.ExternalSessionId); err == nil { + authModule = login.GetAuthProviderLabel(externalSession.AuthModule) + } + result = append(result, &dtos.UserToken{ Id: token.Id, IsActive: isActive, @@ -233,6 +240,7 @@ func (hs *HTTPServer) getUserAuthTokensInternal(c *contextmodel.ReqContext, user OperatingSystemVersion: osVersion, Browser: client.UserAgent.Family, BrowserVersion: browserVersion, + AuthModule: authModule, CreatedAt: createdAt, SeenAt: seenAt, }) diff --git a/pkg/services/auth/authtest/testing.go b/pkg/services/auth/authtest/testing.go index 60265f4de8a..b63839a139e 100644 --- a/pkg/services/auth/authtest/testing.go +++ b/pkg/services/auth/authtest/testing.go @@ -2,6 +2,7 @@ package authtest import ( "context" + "errors" "net" "time" @@ -65,6 +66,9 @@ func NewFakeUserAuthTokenService() *FakeUserAuthTokenService { GetUserTokensProvider: func(ctx context.Context, userId int64) ([]*auth.UserToken, error) { return nil, nil }, + GetExternalSessionProvider: func(ctx context.Context, externalSessionID int64) (*auth.ExternalSession, error) { + return nil, errors.New("settings Provider table not found") + }, } } diff --git a/public/app/features/admin/UserSessions.tsx b/public/app/features/admin/UserSessions.tsx index 9197d962ce7..1c6695a0488 100644 --- a/public/app/features/admin/UserSessions.tsx +++ b/public/app/features/admin/UserSessions.tsx @@ -1,7 +1,9 @@ import { createRef, PureComponent } from 'react'; import { ConfirmButton, ConfirmModal, Button, Stack } from '@grafana/ui'; +import { TagBadge } from 'app/core/components/TagFilter/TagBadge'; import { contextSrv } from 'app/core/core'; +import { Trans } from 'app/core/internationalization'; import { formatDate } from 'app/core/internationalization/dates'; import { AccessControlAction, UserSession } from 'app/types'; @@ -60,7 +62,10 @@ class BaseUserSessions extends PureComponent { Last seen Logged on IP address - Browser and OS + Browser and OS + + Identity Provider + @@ -71,6 +76,9 @@ class BaseUserSessions extends PureComponent { {formatDate(session.createdAt, { dateStyle: 'long' })} {session.clientIp} {`${session.browser} on ${session.os} ${session.osVersion}`} + + {session.authModule && } + {canLogout && ( { clientIp: session.clientIp, browser: session.browser, browserVersion: session.browserVersion, + authModule: session.authModule, os: session.os, osVersion: session.osVersion, device: session.device, diff --git a/public/app/features/profile/UserSessions.tsx b/public/app/features/profile/UserSessions.tsx index 6ffb106a0ec..a6ed2d98543 100644 --- a/public/app/features/profile/UserSessions.tsx +++ b/public/app/features/profile/UserSessions.tsx @@ -4,6 +4,7 @@ import { PureComponent } from 'react'; import { selectors } from '@grafana/e2e-selectors'; import { Button, Icon, LoadingPlaceholder } from '@grafana/ui'; +import { TagBadge } from 'app/core/components/TagFilter/TagBadge'; import { Trans } from 'app/core/internationalization'; import { formatDate } from 'app/core/internationalization/dates'; import { UserSession } from 'app/types'; @@ -43,6 +44,9 @@ class UserSessions extends PureComponent { Browser & OS + + Identity Provider + @@ -56,6 +60,9 @@ class UserSessions extends PureComponent { {session.browser} on {session.os} {session.osVersion} + + {session.authModule && } +