diff --git a/deadlock-plugins/deadlock-extension/src/core/callApi.service.ts b/deadlock-plugins/deadlock-extension/src/core/callApi.service.ts index 3cefbb85e009f052d8331a1f3a7ba654a67582ea..ad487aabaf9e93d537198974f855414f3a8a41ea 100644 --- a/deadlock-plugins/deadlock-extension/src/core/callApi.service.ts +++ b/deadlock-plugins/deadlock-extension/src/core/callApi.service.ts @@ -9,7 +9,11 @@ import KeycloakOAuth2DeviceFlowConnection from './keycloakOAuth2DeviceFlowConnec export default class CallApiService { private callApi: AxiosInstance; - constructor(private keycloackConnection: KeycloakOAuth2DeviceFlowConnection, private extensionStore: ExtensionStore) { + constructor( + private keycloackConnection: KeycloakOAuth2DeviceFlowConnection, + private extensionStore: ExtensionStore, + private controller: Controller, + ) { this.callApi = axios.create({ baseURL: API_URL, headers: { @@ -24,8 +28,16 @@ export default class CallApiService { initApiInterceptor() { this.callApi.interceptors.request.use( async (config) => { - const accessToken = await this.extensionStore.getAccessToken(); + let accessToken = await this.extensionStore.getAccessToken(); + if (!accessToken) { + try { + this.controller.authenticate(); + accessToken = await this.extensionStore.getAccessToken(); + } catch (_error) { + return Promise.reject(_error); + } + } if (accessToken && config.headers) { config.headers['authorization'] = `BEARER ${accessToken}`; } @@ -65,7 +77,11 @@ export default class CallApiService { } else if (originalConfig._retry) { // IF REFRESH TOKEN NOT WORK, REQUEST NEW CONNECTION IN USER BROWSER try { - await this.keycloackConnection.getToken({ openLink: Controller.openBrowserWithUrl }); + const { accessToken, refreshToken } = await this.keycloackConnection.getToken({ + openLink: Controller.openBrowserWithUrl, + }); + await this.extensionStore.setAccessToken(accessToken); + await this.extensionStore.setRefreshToken(refreshToken); return this.callApi(originalConfig); } catch (_error) { return Promise.reject(_error); diff --git a/deadlock-plugins/deadlock-extension/src/core/controller.ts b/deadlock-plugins/deadlock-extension/src/core/controller.ts index 17339d057563e3836f55ca99cf617ca06d4658fa..fa128393125dd01a387efbee95126cb89028aa05 100644 --- a/deadlock-plugins/deadlock-extension/src/core/controller.ts +++ b/deadlock-plugins/deadlock-extension/src/core/controller.ts @@ -38,7 +38,7 @@ export default class Controller { KEYCLOAK_USER_INFO_URL, ); - this.callApiService = new CallApiService(this.connection, this.extensionStore); + this.callApiService = new CallApiService(this.connection, this.extensionStore, this); this.sshKeyManager = new SshKeyManager(); this.init();