Skip to content
Snippets Groups Projects
Commit e27a7a5c authored by Christian ZHENG's avatar Christian ZHENG
Browse files

refactor: factor code inside methods

parent e0174be0
Branches
Tags
3 merge requests!14feat: added mounted, .bashrc, .zshrc, added tests, added keycloak tests,!8feat(extension): login, open in devcontainer, automaticly save code, open briefing, publish extension,!1fix(view): open new views as tabs + show Getting Started button by default
......@@ -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,21 +151,32 @@ 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}`);
await this.handleCreateUserAuthenticationBadRequest(userAuthenticationRequestResponse);
break;
case HttpStatusCode.OK: {
await this.handleCreateUserAuthenticationSuccessRequest(userAuthenticationRequestResponse);
break;
}
default: {
throw new Error(`tokenIsValid: Unhandled HTTP status: ${userAuthenticationRequestResponseCode}`);
}
}
}
}
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: {
// I have to keep this `await sleep` and the while in the same function context
await sleep(this.waitDuration.getCurrentDuration());
continue;
break;
}
case TokenFetchErrorCode.slow_down: {
this.waitDuration.increase();
......@@ -178,19 +188,12 @@ export default class KeycloakOAuth2DeviceFlowConnection {
}
}
}
break;
case HttpStatusCode.OK: {
private async handleCreateUserAuthenticationSuccessRequest(userAuthenticationRequestResponse: Response) {
const successRequestResponse =
(await userAuthenticationRequestResponse.json()) as SuccessfulAuthenticationResponseData;
this.accessToken = successRequestResponse.access_token ?? '';
this.refreshToken = successRequestResponse.refresh_token ?? '';
break;
}
default: {
throw new Error(`tokenIsValid: Unhandled HTTP status: ${userAuthenticationRequestResponseCode}`);
}
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment