Skip to content

Commit c269cfd

Browse files
rpatel-fanaticsrishabhp58
authored andcommitted
added backstage identity api to fetch bearer tokens and attach them to request headers for authenticating api requests within backstage
Signed-off-by: Rishabh Patel <rpatel@fanatics.com> Signed-off-by: Rishabh Patel <rishabhpatel58@gmail.com>
1 parent 1ae2810 commit c269cfd

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/api/GitlabCIClient.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
MergeRequestsSummary,
77
MergeRequestsStatusSummary,
88
} from './GitlabCIApi';
9-
import { DiscoveryApi } from '@backstage/core-plugin-api';
9+
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
1010
import {
1111
ContributorData,
1212
MergeRequest,
@@ -16,28 +16,34 @@ import {
1616
export class GitlabCIClient implements GitlabCIApi {
1717
discoveryApi: DiscoveryApi;
1818
baseUrl: string;
19+
identityApi: IdentityApi;
1920
constructor({
2021
discoveryApi,
2122
baseUrl = 'https://gitlab.com/',
23+
identityApi,
2224
}: {
2325
discoveryApi: DiscoveryApi;
2426
baseUrl?: string;
27+
identityApi: IdentityApi;
2528
}) {
2629
this.discoveryApi = discoveryApi;
2730
this.baseUrl = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
31+
this.identityApi = identityApi;
2832
}
2933

3034
private async callApi<T>(
3135
path: string,
32-
query: { [key in string]: any },
36+
query: { [key in string]: any }
3337
): Promise<T | []> {
3438
const apiUrl = `${await this.discoveryApi.getBaseUrl('proxy')}/gitlabci`;
39+
const { token } = await this.identityApi.getCredentials();
3540
const response = await fetch(
3641
`${apiUrl}/${path}?${new URLSearchParams(query).toString()}`,
42+
{ headers: { Authorization: `Bearer ${token}` } }
3743
);
3844
if (response.status === 200) {
3945
return (await response.json()) as T;
40-
}
46+
}
4147
return [];
4248
}
4349

@@ -142,7 +148,7 @@ export class GitlabCIClient implements GitlabCIApi {
142148

143149
async getProjectDetails(projectSlug?: string): Promise<Object | undefined> {
144150
let projectDetails: any;
145-
if(projectSlug){
151+
if (projectSlug) {
146152
projectDetails = await this.callApi<Object>(
147153
'projects/' + encodeURIComponent(projectSlug),
148154
{},

src/plugin.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { createPlugin,
1+
import {
2+
createPlugin,
23
createRoutableExtension,
34
createComponentExtension,
4-
} from '@backstage/core-plugin-api';
5+
} from '@backstage/core-plugin-api';
56

67
import {
78
configApiRef,
89
createApiFactory,
910
createRouteRef,
1011
discoveryApiRef,
12+
identityApiRef,
1113
} from '@backstage/core-plugin-api';
1214
import { GitlabCIApiRef, GitlabCIClient } from './api';
1315

@@ -20,22 +22,26 @@ export const gitlabPlugin = createPlugin({
2022
apis: [
2123
createApiFactory({
2224
api: GitlabCIApiRef,
23-
deps: { configApi: configApiRef, discoveryApi: discoveryApiRef },
24-
factory: ({ configApi, discoveryApi }) =>
25+
deps: {
26+
configApi: configApiRef,
27+
discoveryApi: discoveryApiRef,
28+
identityApi: identityApiRef,
29+
},
30+
factory: ({ configApi, discoveryApi, identityApi }) =>
2531
new GitlabCIClient({
2632
discoveryApi,
2733
baseUrl: configApi.getOptionalString('gitlab.baseUrl'),
34+
identityApi,
2835
}),
2936
}),
3037
],
3138
});
3239

3340
export const EntityGitlabContent = gitlabPlugin.provide(
3441
createRoutableExtension({
35-
component: () =>
36-
import('./Router').then(m => m.Router),
42+
component: () => import('./Router').then((m) => m.Router),
3743
mountPoint: rootRouteRef,
38-
}),
44+
})
3945
);
4046

4147
export const EntityGitlabLanguageCard = gitlabPlugin.provide(
@@ -86,4 +92,4 @@ export const EntityGitlabPipelinesTable = gitlabPlugin.provide(
8692
import('./components/widgets/index').then((m) => m.PipelinesTable),
8793
},
8894
})
89-
);
95+
);

0 commit comments

Comments
 (0)