Skip to content

Commit d104a7d

Browse files
committed
fakerequest handling
1 parent 82d82df commit d104a7d

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

x-pack/platform/plugins/shared/onechat/server/services/agents/persisted/client/client.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
type ToolSelection,
1919
type UserIdAndName,
2020
} from '@kbn/onechat-common';
21+
import { getUserFromRequest } from '../../../utils';
2122
import type {
2223
AgentCreateRequest,
2324
AgentDeleteRequest,
@@ -56,14 +57,9 @@ export const createClient = async ({
5657
toolsService: ToolsServiceStart;
5758
logger: Logger;
5859
}): Promise<AgentClient> => {
59-
const authUser = security.authc.getCurrentUser(request);
60-
if (!authUser) {
61-
throw new Error('No user bound to the provided request');
62-
}
63-
60+
const user = getUserFromRequest(request, security);
6461
const esClient = elasticsearch.client.asScoped(request).asInternalUser;
6562
const storage = createStorage({ logger, esClient });
66-
const user = { id: authUser.profile_uid!, username: authUser.username };
6763

6864
return new AgentClientImpl({ storage, user, request, space, toolsService });
6965
};

x-pack/platform/plugins/shared/onechat/server/services/conversation/conversation_service.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { SpacesPluginStart } from '@kbn/spaces-plugin/server';
1515
import { getCurrentSpaceId } from '../../utils/spaces';
1616
import type { ConversationClient } from './client';
1717
import { createClient } from './client';
18+
import { getUserFromRequest } from '@kbn/onechat-plugin/server/services/utils';
1819

1920
export interface ConversationService {
2021
getScopedClient(options: { request: KibanaRequest }): Promise<ConversationClient>;
@@ -41,13 +42,8 @@ export class ConversationServiceImpl implements ConversationService {
4142
}
4243

4344
async getScopedClient({ request }: { request: KibanaRequest }): Promise<ConversationClient> {
44-
const authUser = this.security.authc.getCurrentUser(request);
45-
if (!authUser) {
46-
throw new Error('No user bound to the provided request');
47-
}
48-
45+
const user = getUserFromRequest(request, this.security);
4946
const esClient = this.elasticsearch.client.asScoped(request).asInternalUser;
50-
const user = { id: authUser.profile_uid!, username: authUser.username };
5147
const space = getCurrentSpaceId({ request, spaces: this.spaces });
5248

5349
return createClient({ user, esClient, logger: this.logger, space });
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import type { KibanaRequest } from '@kbn/core-http-server';
9+
import type { SecurityServiceStart } from '@kbn/core-security-server';
10+
11+
export const getUserFromRequest = (request: KibanaRequest, security: SecurityServiceStart) => {
12+
const authUser = security.authc.getCurrentUser(request);
13+
const user = authUser
14+
? { id: authUser.profile_uid!, username: authUser.username }
15+
: { id: 'anonymous', username: 'anonymous' };
16+
return user;
17+
};

0 commit comments

Comments
 (0)