Skip to content

Commit

Permalink
fixes api auth bug in tech-insights plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Larsson <erik.larsson@schibsted.com>
  • Loading branch information
erikxiv committed Dec 22, 2021
1 parent f30f67f commit a86f5c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-toys-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-tech-insights': minor
---

fixes api auth in tech-insights plugin
16 changes: 14 additions & 2 deletions plugins/tech-insights/src/api/TechInsightsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { TechInsightsApi } from './TechInsightsApi';
import { CheckResult } from '@backstage/plugin-tech-insights-common';
import { Check } from './types';
import { DiscoveryApi } from '@backstage/core-plugin-api';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
import { ResponseError } from '@backstage/errors';
import { EntityName } from '@backstage/catalog-model';

Expand All @@ -28,13 +28,16 @@ import {

export type Options = {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
};

export class TechInsightsClient implements TechInsightsApi {
private readonly discoveryApi: DiscoveryApi;
private readonly identityApi: IdentityApi;

constructor(options: Options) {
this.discoveryApi = options.discoveryApi;
this.identityApi = options.identityApi;
}

getScorecardsDefinition(
Expand All @@ -47,7 +50,14 @@ export class TechInsightsClient implements TechInsightsApi {

async getAllChecks(): Promise<Check[]> {
const url = await this.discoveryApi.getBaseUrl('tech-insights');
const response = await fetch(`${url}/checks`);
const token = await this.identityApi.getIdToken();
const response = await fetch(`${url}/checks`, {
headers: token
? {
Authorization: `Bearer ${token}`,
}
: undefined,
});
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
Expand All @@ -59,6 +69,7 @@ export class TechInsightsClient implements TechInsightsApi {
checks: Check[],
): Promise<CheckResult[]> {
const url = await this.discoveryApi.getBaseUrl('tech-insights');
const token = await this.identityApi.getIdToken();
const { namespace, kind, name } = entityParams;
const allChecks = checks ? checks : await this.getAllChecks();
const checkIds = allChecks.map((check: Check) => check.id);
Expand All @@ -71,6 +82,7 @@ export class TechInsightsClient implements TechInsightsApi {
body: JSON.stringify({ checks: checkIds }),
headers: {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token}` }),
},
},
);
Expand Down

0 comments on commit a86f5c1

Please sign in to comment.