Add auth token to KafkaBackendClient via IdentityApi

Signed-off-by: Xavier Serrano <zombispormedio007@gmail.com>
Signed-off-by: Xavier Serrano <xavier.serrano@lansweeper.com>
This commit is contained in:
Xavier Serrano
2021-10-27 19:15:13 +02:00
parent 88a01d9320
commit 91f8644f17
3 changed files with 18 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-kafka': patch
---
Use IdentityApi to provide Auth Token for KafkaBackendClient Api calls
+9 -2
View File
@@ -15,21 +15,28 @@
*/
import { KafkaApi, ConsumerGroupOffsetsResponse } from './types';
import { DiscoveryApi } from '@backstage/core-plugin-api';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
export class KafkaBackendClient implements KafkaApi {
private readonly discoveryApi: DiscoveryApi;
private readonly identityApi: IdentityApi;
constructor(options: { discoveryApi: DiscoveryApi }) {
constructor(options: {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
}) {
this.discoveryApi = options.discoveryApi;
this.identityApi = options.identityApi;
}
private async internalGet(path: string): Promise<any> {
const url = `${await this.discoveryApi.getBaseUrl('kafka')}${path}`;
const idToken = await this.identityApi.getIdToken();
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
...(idToken && { Authorization: `Bearer ${idToken}` }),
},
});
+4 -2
View File
@@ -21,6 +21,7 @@ import {
createRoutableExtension,
createRouteRef,
discoveryApiRef,
identityApiRef,
} from '@backstage/core-plugin-api';
export const rootCatalogKafkaRouteRef = createRouteRef({
@@ -33,8 +34,9 @@ export const kafkaPlugin = createPlugin({
apis: [
createApiFactory({
api: kafkaApiRef,
deps: { discoveryApi: discoveryApiRef },
factory: ({ discoveryApi }) => new KafkaBackendClient({ discoveryApi }),
deps: { discoveryApi: discoveryApiRef, identityApi: identityApiRef },
factory: ({ discoveryApi, identityApi }) =>
new KafkaBackendClient({ discoveryApi, identityApi }),
}),
],
routes: {