Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions x-pack/plugins/cloud/server/routes/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]';

security.authc.getCurrentUser.mockReturnValueOnce({
username,
email,
metadata: {
saml_email: [email],
},
});

const router = httpServiceMock.createRouter();
Expand Down
13 changes: 11 additions & 2 deletions x-pack/plugins/cloud/server/routes/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down