feat(explore): adds optional token manager to collator to authenticate requests

Signed-off-by: Christopher Diaz <cdiaz@rvohealth.com>
This commit is contained in:
Christopher Diaz
2023-05-08 10:17:18 -04:00
parent 81b73f57e3
commit a5baeea2cb
2 changed files with 23 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-backend-module-explore': patch
---
Allows for an optional tokenManager to authenticate requests from the collator to the explore backend
@@ -14,7 +14,10 @@
* limitations under the License.
*/
import { PluginEndpointDiscovery } from '@backstage/backend-common';
import {
PluginEndpointDiscovery,
TokenManager,
} from '@backstage/backend-common';
import { Config } from '@backstage/config';
import { ExploreTool } from '@backstage/plugin-explore-common';
import {
@@ -40,6 +43,7 @@ export interface ToolDocument extends IndexableDocument, ExploreTool {}
export type ToolDocumentCollatorFactoryOptions = {
discovery: PluginEndpointDiscovery;
logger: Logger;
tokenManager?: TokenManager;
};
/**
@@ -52,10 +56,12 @@ export class ToolDocumentCollatorFactory implements DocumentCollatorFactory {
private readonly discovery: PluginEndpointDiscovery;
private readonly logger: Logger;
private readonly tokenManager?: TokenManager;
private constructor(options: ToolDocumentCollatorFactoryOptions) {
this.discovery = options.discovery;
this.logger = options.logger;
this.tokenManager = options.tokenManager;
}
static fromConfig(
@@ -87,7 +93,17 @@ export class ToolDocumentCollatorFactory implements DocumentCollatorFactory {
private async fetchTools() {
const baseUrl = await this.discovery.getBaseUrl('explore');
const response = await fetch(`${baseUrl}/tools`);
let headers = {};
if (this.tokenManager) {
const { token } = await this.tokenManager.getToken();
headers = {
Authorization: `Bearer ${token}`,
};
}
const response = await fetch(`${baseUrl}/tools`, headers);
if (!response.ok) {
throw new Error(