diff --git a/x-pack/plugins/cloud/server/routes/chat.test.ts b/x-pack/plugins/cloud/server/routes/chat.test.ts index 9ed76eff6d081..e12278e264e5e 100644 --- a/x-pack/plugins/cloud/server/routes/chat.test.ts +++ b/x-pack/plugins/cloud/server/routes/chat.test.ts @@ -44,14 +44,16 @@ describe('chat route', () => { `); }); - test('returns user information and a token', async () => { + test('returns user information taken from saml metadata and a token', async () => { const security = securityMock.createSetup(); const username = 'user.name'; const email = 'user@elastic.co'; security.authc.getCurrentUser.mockReturnValueOnce({ username, - email, + metadata: { + saml_email: [email], + }, }); const router = httpServiceMock.createRouter(); diff --git a/x-pack/plugins/cloud/server/routes/chat.ts b/x-pack/plugins/cloud/server/routes/chat.ts index 62c4475c92ae5..1bc3505c087ae 100644 --- a/x-pack/plugins/cloud/server/routes/chat.ts +++ b/x-pack/plugins/cloud/server/routes/chat.ts @@ -6,11 +6,18 @@ */ import { IRouter } from '../../../../../src/core/server'; -import type { SecurityPluginSetup } from '../../../security/server'; +import type { SecurityPluginSetup, AuthenticatedUser } from '../../../security/server'; import { GET_CHAT_USER_DATA_ROUTE_PATH } from '../../common/constants'; import type { GetChatUserDataResponseBody } from '../../common/types'; import { generateSignedJwt } from '../util/generate_jwt'; +type MetaWithSaml = AuthenticatedUser['metadata'] & { + saml_name: [string]; + saml_email: [string]; + saml_roles: [string]; + saml_principal: [string]; +}; + export const registerChatRoute = ({ router, chatIdentitySecret, @@ -33,7 +40,9 @@ export const registerChatRoute = ({ }, async (_context, request, response) => { const user = security.authc.getCurrentUser(request); - let { email: userEmail, username: userId } = user || {}; + const { metadata, username } = user || {}; + let userId = username; + let [userEmail] = (metadata as MetaWithSaml)?.saml_email || []; // In local development, these values are not populated. This is a workaround // to allow for local testing.