diff --git a/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts b/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts index 40e8dbbe100905f60b0baa0dd0dc42c835449106..327a9f29fb745c4c23e77027f39da89d4e3c5c39 100644 --- a/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts +++ b/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts @@ -7,25 +7,25 @@ import { error as err, log } from '../recorder/utils'; process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; // TODO: remove when SSL will work export default class KeycloakOAuth2DeviceFlowConnection { - private _waitDuration: WaitDuration; - private _accessToken: string; - private _refreshToken: string; - private _deviceAuthorizationRequestResponseData: DeviceAuthorizationRequestResponseData; + private waitDuration: WaitDuration; + private accessToken: string; + private refreshToken: string; + private deviceAuthorizationRequestResponseData: DeviceAuthorizationRequestResponseData; - constructor(private _deviceUrl: string, private _tokenUrl: string, private _userInfoUrl?: string) { - this._waitDuration = new WaitDuration([5_000, 5_000, 5_000, 10_000, 10_000, 10_000, 30_000, 30_000, 100_000]); - this._accessToken = ''; - this._refreshToken = ''; - this._deviceAuthorizationRequestResponseData = {}; + constructor(private deviceUrl: string, private tokenUrl: string, private userInfoUrl?: string) { + this.waitDuration = new WaitDuration([5_000, 5_000, 5_000, 10_000, 10_000, 10_000, 30_000, 30_000, 100_000]); + this.accessToken = ''; + this.refreshToken = ''; + this.deviceAuthorizationRequestResponseData = {}; } /** - * _userInfoUrl must be passed in constructor in order to use this + * userInfoUrl must be passed in constructor in order to use this * @param accessToken * @returns Promise */ public async tokenIsValid(accessToken: string) { - const url = this._userInfoUrl; + const url = this.userInfoUrl; if (!url) { return Promise.reject('tokenIsValid: missing user_info API endpoint'); } @@ -64,7 +64,7 @@ export default class KeycloakOAuth2DeviceFlowConnection { const { refreshToken, openLink } = args; if (!!refreshToken) { await this.createUserAuthentication({ - url: this._tokenUrl, + url: this.tokenUrl, body: (() => { const params = new URLSearchParams(); params.append('response_type', 'token'); @@ -74,25 +74,25 @@ export default class KeycloakOAuth2DeviceFlowConnection { return params.toString(); })(), }); - return Promise.resolve({ accessToken: this._accessToken, refreshToken: this._refreshToken }); + return Promise.resolve({ accessToken: this.accessToken, refreshToken: this.refreshToken }); } if (!this._deviceIsRegistered()) { await this.registerDevice(); } try { - openLink(this._deviceAuthorizationRequestResponseData.verification_uri_complete!); + openLink(this.deviceAuthorizationRequestResponseData.verification_uri_complete!); await this.createUserAuthentication({ - url: this._tokenUrl, + url: this.tokenUrl, body: (() => { const params = new URLSearchParams(); params.append('response_type', 'token'); - params.append('device_code', this._deviceAuthorizationRequestResponseData.device_code ?? ''); + params.append('device_code', this.deviceAuthorizationRequestResponseData.device_code ?? ''); params.append('grant_type', 'urn:ietf:params:oauth:grant-type:device_code'); params.append('client_id', 'deadlock-desktop'); return params.toString(); })(), }); - return Promise.resolve({ accessToken: this._accessToken, refreshToken: this._refreshToken }); + return Promise.resolve({ accessToken: this.accessToken, refreshToken: this.refreshToken }); } catch (error: unknown) { err(error); return Promise.reject(error); @@ -100,20 +100,20 @@ export default class KeycloakOAuth2DeviceFlowConnection { } private _deviceIsRegistered(): boolean { - return !!this._deviceAuthorizationRequestResponseData.device_code; + return !!this.deviceAuthorizationRequestResponseData.device_code; } public async registerDevice() { log('Device not registered. Registering ...'); const deviceAuthorizationRequestResponse: Response = await this.createDeviceAuthorization({ - url: this._deviceUrl, + url: this.deviceUrl, body: (() => { const params = new URLSearchParams(); params.append('client_id', 'deadlock-desktop'); return params.toString(); })(), }); - this._deviceAuthorizationRequestResponseData = + this.deviceAuthorizationRequestResponseData = (await deviceAuthorizationRequestResponse.json()) as DeviceAuthorizationRequestResponseData; } @@ -164,12 +164,12 @@ export default class KeycloakOAuth2DeviceFlowConnection { } case TokenFetchErrorCode.authorization_pending: { // I have to keep this `await sleep` and the while in the same function context - await sleep(this._waitDuration.getCurrentDuration()); + await sleep(this.waitDuration.getCurrentDuration()); continue; } case TokenFetchErrorCode.slow_down: { - this._waitDuration.increase(); - await sleep(this._waitDuration.getCurrentDuration()); + this.waitDuration.increase(); + await sleep(this.waitDuration.getCurrentDuration()); break; } default: { @@ -181,8 +181,8 @@ export default class KeycloakOAuth2DeviceFlowConnection { case HttpStatusCode.OK: { const successRequestResponse = (await userAuthenticationRequestResponse.json()) as SuccessfulAuthenticationResponseData; - this._accessToken = successRequestResponse.access_token ?? ''; - this._refreshToken = successRequestResponse.refresh_token ?? ''; + this.accessToken = successRequestResponse.access_token ?? ''; + this.refreshToken = successRequestResponse.refresh_token ?? ''; break; } default: {