diff --git a/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts b/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts index 9ff0ef5fc0e83b7ef9adbbef2197fc53206be95f..983debab52bb614f91b9b1e37b23f2278b08a5e4 100644 --- a/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts +++ b/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts @@ -95,7 +95,6 @@ export default class KeycloakOAuth2DeviceFlowConnection { }); return Promise.resolve({ accessToken: this.accessToken, refreshToken: this.refreshToken }); } catch (error) { - err(error); return Promise.reject(error); } } @@ -152,38 +151,10 @@ export default class KeycloakOAuth2DeviceFlowConnection { userAuthenticationRequestResponseCode = userAuthenticationRequestResponse.status; switch (userAuthenticationRequestResponseCode) { case HttpStatusCode.BAD_REQUEST: - { - const badRequestResponse = - (await userAuthenticationRequestResponse.json()) as FailedAuthenticationReponseData; - log(`${badRequestResponse.error!}: ${badRequestResponse.error_description}`); - const errorCode = TokenFetchErrorCode[badRequestResponse.error!]; - switch (errorCode) { - case TokenFetchErrorCode.invalid_client: - case TokenFetchErrorCode.invalid_grant: - case TokenFetchErrorCode.unsupported_grant_type: { - throw new Error('createUserAuthentication: ' + errorCode); - } - case TokenFetchErrorCode.authorization_pending: { - // I have to keep this `await sleep` and the while in the same function context - await sleep(this.waitDuration.getCurrentDuration()); - continue; - } - case TokenFetchErrorCode.slow_down: { - this.waitDuration.increase(); - await sleep(this.waitDuration.getCurrentDuration()); - break; - } - default: { - throw new Error(`createUserAuthentication: Unhandled error code [ ${badRequestResponse.error} ]`); - } - } - } + await this.handleCreateUserAuthenticationBadRequest(userAuthenticationRequestResponse); break; case HttpStatusCode.OK: { - const successRequestResponse = - (await userAuthenticationRequestResponse.json()) as SuccessfulAuthenticationResponseData; - this.accessToken = successRequestResponse.access_token ?? ''; - this.refreshToken = successRequestResponse.refresh_token ?? ''; + await this.handleCreateUserAuthenticationSuccessRequest(userAuthenticationRequestResponse); break; } default: { @@ -192,6 +163,38 @@ export default class KeycloakOAuth2DeviceFlowConnection { } } } + + private async handleCreateUserAuthenticationBadRequest(userAuthenticationRequestResponse: Response) { + const badRequestResponse = (await userAuthenticationRequestResponse.json()) as FailedAuthenticationReponseData; + const errorCode = TokenFetchErrorCode[badRequestResponse.error!]; + switch (errorCode) { + case TokenFetchErrorCode.invalid_client: + case TokenFetchErrorCode.invalid_grant: + case TokenFetchErrorCode.unsupported_grant_type: { + err(`${badRequestResponse.error!}: ${badRequestResponse.error_description}`); + throw new Error('createUserAuthentication: ' + errorCode); + } + case TokenFetchErrorCode.authorization_pending: { + await sleep(this.waitDuration.getCurrentDuration()); + break; + } + case TokenFetchErrorCode.slow_down: { + this.waitDuration.increase(); + await sleep(this.waitDuration.getCurrentDuration()); + break; + } + default: { + throw new Error(`createUserAuthentication: Unhandled error code [ ${badRequestResponse.error} ]`); + } + } + } + + private async handleCreateUserAuthenticationSuccessRequest(userAuthenticationRequestResponse: Response) { + const successRequestResponse = + (await userAuthenticationRequestResponse.json()) as SuccessfulAuthenticationResponseData; + this.accessToken = successRequestResponse.access_token ?? ''; + this.refreshToken = successRequestResponse.refresh_token ?? ''; + } } /**