Add identity token to api requests

This commit is contained in:
Erik Larsson
2021-01-05 11:15:13 +01:00
parent 2132233fc8
commit 6ed2b47d60
20 changed files with 317 additions and 68 deletions
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { DiscoveryApi } from '@backstage/core';
import { DiscoveryApi, IdentityApi } from '@backstage/core';
import { KubernetesApi } from './types';
import {
KubernetesRequestBody,
@@ -23,9 +23,14 @@ import {
export class KubernetesBackendClient implements KubernetesApi {
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 getRequired(
@@ -33,9 +38,11 @@ export class KubernetesBackendClient implements KubernetesApi {
requestBody: KubernetesRequestBody,
): Promise<any> {
const url = `${await this.discoveryApi.getBaseUrl('kubernetes')}${path}`;
const idToken = await this.identityApi.getIdToken();
const response = await fetch(url, {
method: 'POST',
headers: {
authorization: `Bearer ${idToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
+4 -3
View File
@@ -18,6 +18,7 @@ import {
createPlugin,
createRouteRef,
discoveryApiRef,
identityApiRef,
googleAuthApiRef,
} from '@backstage/core';
import { KubernetesBackendClient } from './api/KubernetesBackendClient';
@@ -35,9 +36,9 @@ export const plugin = createPlugin({
apis: [
createApiFactory({
api: kubernetesApiRef,
deps: { discoveryApi: discoveryApiRef },
factory: ({ discoveryApi }) =>
new KubernetesBackendClient({ discoveryApi }),
deps: { discoveryApi: discoveryApiRef, identityApi: identityApiRef },
factory: ({ discoveryApi, identityApi }) =>
new KubernetesBackendClient({ discoveryApi, identityApi }),
}),
createApiFactory({
api: kubernetesAuthProvidersApiRef,