Only add authorization header if token exists

This commit is contained in:
Erik Larsson
2021-01-30 04:03:43 +01:00
parent 5ba0804189
commit 320708e171
4 changed files with 24 additions and 19 deletions
@@ -49,13 +49,16 @@ export class CatalogImportClient implements CatalogImportApi {
repo: string;
}): Promise<PartialEntity[]> {
const idToken = await this.identityApi.getIdToken();
const headers = {
'Content-Type': 'application/json',
} as { [header: string]: string };
if (idToken) {
headers.authorization = `Bearer ${idToken}`;
}
const response = await fetch(
`${await this.discoveryApi.getBaseUrl('catalog')}/analyze-location`,
{
headers: {
authorization: `Bearer ${idToken}`,
'Content-Type': 'application/json',
},
headers,
method: 'POST',
body: JSON.stringify({
location: { type: 'url', target: repo },
@@ -80,13 +83,16 @@ export class CatalogImportClient implements CatalogImportApi {
location: string;
}): Promise<void> {
const idToken = await this.identityApi.getIdToken();
const headers = {
'Content-Type': 'application/json',
} as { [header: string]: string };
if (idToken) {
headers.authorization = `Bearer ${idToken}`;
}
const response = await fetch(
`${await this.discoveryApi.getBaseUrl('catalog')}/locations`,
{
headers: {
authorization: `Bearer ${idToken}`,
'Content-Type': 'application/json',
},
headers,
method: 'POST',
body: JSON.stringify({
type: 'url',
+2 -4
View File
@@ -39,11 +39,9 @@ export class FossaClient implements FossaApi {
private async callApi(path: string): Promise<any> {
const apiUrl = `${await this.discoveryApi.getBaseUrl('proxy')}/fossa`;
const headers: Record<string, string> = {
authorization: await `Bearer ${this.identityApi.getIdToken()}`,
};
const idToken = await this.identityApi.getIdToken();
const response = await fetch(`${apiUrl}/${path}`, {
headers,
headers: idToken ? { authorization: `Bearer ${idToken}` } : {},
});
if (response.status === 200) {
return await response.json();
@@ -39,12 +39,15 @@ export class KubernetesBackendClient implements KubernetesApi {
): Promise<any> {
const url = `${await this.discoveryApi.getBaseUrl('kubernetes')}${path}`;
const idToken = await this.identityApi.getIdToken();
const headers = {
'Content-Type': 'application/json',
} as { [header: string]: string };
if (idToken) {
headers.authorization = `Bearer ${idToken}`;
}
const response = await fetch(url, {
method: 'POST',
headers: {
authorization: `Bearer ${idToken}`,
'Content-Type': 'application/json',
},
headers,
body: JSON.stringify(requestBody),
});
+1 -3
View File
@@ -60,9 +60,7 @@ export class RollbarClient implements RollbarApi {
const url = `${await this.discoveryApi.getBaseUrl('rollbar')}${path}`;
const idToken = await this.identityApi.getIdToken();
const response = await fetch(url, {
headers: {
authorization: `Bearer ${idToken}`,
},
headers: idToken ? { authorization: `Bearer ${idToken}` } : {},
});
if (!response.ok) {