Skip to content

Commit

Permalink
Provide refresh token arg to the che workspace client
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Pinkney <joshpinkney@gmail.com>
  • Loading branch information
JPinkney committed Jan 25, 2021
1 parent a7c5645 commit dbf0364
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
59 changes: 30 additions & 29 deletions src/services/cheWorkspaceClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Red Hat, Inc. - initial API and implementation
*/

import { AxiosInstance } from 'axios';
import { AxiosInstance, AxiosRequestConfig } from 'axios';
import { injectable } from 'inversify';
import WorkspaceClient, { IWorkspaceMasterApi, IRemoteAPI } from '@eclipse-che/workspace-client';
import { KeycloakAuthService } from '../keycloak/auth';
Expand All @@ -26,7 +26,6 @@ export class CheWorkspaceClient {
private readonly axios: AxiosInstance;
private originLocation: string;
private baseUrl: string;
private websocketContext: string;
private _restApiClient: IRemoteAPI;
private _jsonRpcMasterApi: IWorkspaceMasterApi;
private _failingWebSockets: string[];
Expand All @@ -38,7 +37,6 @@ export class CheWorkspaceClient {
*/
constructor() {
this.baseUrl = '/api';
this.websocketContext = '/api/websocket';
this._failingWebSockets = [];
this.webSocketEventEmitter = new EventEmitter();

Expand Down Expand Up @@ -67,18 +65,7 @@ export class CheWorkspaceClient {
if (keycloak && keycloak.updateToken && !isUpdated) {
updateTimer();
try {
await new Promise((resolve, reject) => {
keycloak.updateToken(5).success((refreshed: boolean) => {
if (refreshed && keycloak.token) {
const header = 'Authorization';
this.axios.defaults.headers.common[header] = `Bearer ${keycloak.token}`;
request.headers.common[header] = `Bearer ${keycloak.token}`;
}
resolve(keycloak);
}).error((error: any) => {
reject(new Error(error));
});
});
await this.refreshToken(request);
} catch (e) {
console.error('Failed to update token.', e);
window.sessionStorage.setItem('oidcDashboardRedirectUrl', location.href);
Expand Down Expand Up @@ -116,10 +103,6 @@ export class CheWorkspaceClient {
return this.baseUrl;
}

getWebsocketContext(): string {
return this.websocketContext;
}

setBaseUrl(baseUrl: string): void {
this.baseUrl = baseUrl;
}
Expand All @@ -131,15 +114,9 @@ export class CheWorkspaceClient {
}

async updateJsonRpcMasterApi(): Promise<void> {
let jsonRpcApiLocation = this.originLocation.replace('http', 'ws') + '/api/websocket';
// connect
if (this.token) {
const header = 'Authorization';
this.axios.defaults.headers.common[header] = `Bearer ${this.token}`;

jsonRpcApiLocation += `?token=${this.token}`;
}
this._jsonRpcMasterApi = WorkspaceClient.getJsonRpcApi(jsonRpcApiLocation);
const jsonRpcApiLocation = this.originLocation.replace('http', 'ws');
const tokenRefresher = () => this.refreshToken();
this._jsonRpcMasterApi = WorkspaceClient.getJsonRpcApi(jsonRpcApiLocation, tokenRefresher);
this._jsonRpcMasterApi.onDidWebSocketStatusChange((websockets: string[]) => {
this._failingWebSockets = [];
for (const websocket of websockets) {
Expand All @@ -148,7 +125,7 @@ export class CheWorkspaceClient {
}
this.webSocketEventEmitter.emit(this.webSocketEventName);
});
await this._jsonRpcMasterApi.connect(jsonRpcApiLocation);
await this._jsonRpcMasterApi.connect();
const clientId = this._jsonRpcMasterApi.getClientId();
console.log('WebSocket connection clientId', clientId);
}
Expand All @@ -164,4 +141,28 @@ export class CheWorkspaceClient {
get failingWebSockets(): string[] {
return Array.from(this._failingWebSockets);
}

private refreshToken(request?: AxiosRequestConfig): Promise<string | Error> {
const { keycloak } = KeycloakAuthService;
if (keycloak) {
return new Promise((resolve, reject) => {
keycloak.updateToken(5).success((refreshed: boolean) => {
if (refreshed && keycloak.token) {
const header = 'Authorization';
this.axios.defaults.headers.common[header] = `Bearer ${keycloak.token}`;
if (request) {
request.headers.common[header] = `Bearer ${keycloak.token}`;
}
}
resolve(keycloak.token as string);
}).error((error: any) => {
reject(new Error(error));
});
});
}
if (!this.token) {
return Promise.reject(new Error('Unable to resolve token'));
}
return Promise.resolve(this.token);
}
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,9 @@
integrity sha512-KnnnDpnxxK0TBgR0Ux3oOYCvmCv6d4cRA+1j9wiLkaJighCqgCUaxnHWXEfolYbRq1+o/sfsxN+BxRDuF+H8wQ==

"@eclipse-che/workspace-client@^0.0.1-1608729566":
version "0.0.1-1610629049"
resolved "https://registry.yarnpkg.com/@eclipse-che/workspace-client/-/workspace-client-0.0.1-1610629049.tgz#fd34b7b3f22bc969570c391cd4683c05782c2c9f"
integrity sha512-GF/1b3F/BsrqeF+BZ1QXZrEpQFLoiztD6QTQw0rcGBIr04K6IL+QpI+i3z6MbaI59yWvxEkhA/VL2oFTprEyyA==
version "0.0.1-1611344228"
resolved "https://registry.yarnpkg.com/@eclipse-che/workspace-client/-/workspace-client-0.0.1-1611344228.tgz#983eb6ca71717ee665dff001006ab07815734e4a"
integrity sha512-Ru658debzMS+JZfiZC3Jr5y3DQSK0JAp6/dq81PmZm07UPwg0FdFiOH6lN/lr8yYJTu+Hl0+4rOhCS3SSwvJPA==
dependencies:
"@eclipse-che/api" "^7.0.0-beta-4.0"
axios "0.20.0"
Expand Down

0 comments on commit dbf0364

Please sign in to comment.