[cloud] Get user email from saml metadata (#124301)

This commit is contained in:
Clint Andrew Hall 2022-02-02 08:02:39 -06:00 committed by GitHub
parent 7b9901eac8
commit c877725dfc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View file

@ -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();

View file

@ -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.