Reduce cyclomatic complexity

pull/81/head
MillenniumEarl 2021-03-11 11:49:10 +01:00
parent a10755b28c
commit 2751b9bc3b
1 changed files with 56 additions and 37 deletions

View File

@ -26,10 +26,20 @@ import Credentials from "./classes/credentials";
// Configure axios to use the cookie jar // Configure axios to use the cookie jar
axiosCookieJarSupport(axios); axiosCookieJarSupport(axios);
// Types
type LookupMapCodeT = {
code: number;
message: string;
};
// Global variables // Global variables
const userAgent = const USER_AGENT =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) " + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) " +
"AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15"; "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15";
const AUTH_SUCCESSFUL_MESSAGE = "Authentication successful";
const INVALID_2FA_CODE_MESSAGE =
"The two-step verification value could not be confirmed. Please try again";
const INCORRECT_CREDENTIALS_MESSAGE = "Incorrect password. Please try again.";
/** /**
* Common configuration used to send request via Axios. * Common configuration used to send request via Axios.
@ -39,7 +49,7 @@ const commonConfig = {
* Headers to add to the request. * Headers to add to the request.
*/ */
headers: { headers: {
"User-Agent": userAgent, "User-Agent": USER_AGENT,
Connection: "keep-alive" Connection: "keep-alive"
}, },
/** /**
@ -114,24 +124,19 @@ export async function authenticate(
// Try to log-in // Try to log-in
let authResult: LoginResult = null; let authResult: LoginResult = null;
try {
// Fetch the response to the login request // Fetch the response to the login request
const response = await fetchPOSTResponse(urls.LOGIN, params, force); const response = await fetchPOSTResponse(secureURL, params, force);
// Parse the response // Parse the response
const result = response.applyOnSuccess((r) => manageLoginPOSTResponse(r)); const result = response.applyOnSuccess((r) => manageLoginPOSTResponse(r));
if (result.isSuccess()) authResult = result.value;
else throw response.value; // Manage result
} catch (e) { if (result.isFailure()) {
shared.logger.error( const message = `Error ${result.value.message} occurred while authenticating`;
`Error ${e.message} occurred while authenticating to ${secureURL}` shared.logger.error(message);
); authResult = new LoginResult(false, LoginResult.UNKNOWN_ERROR, message);
authResult = new LoginResult( } else authResult = result.value;
false,
LoginResult.UNKNOWN_ERROR,
`Error ${e.message} while authenticating`
);
}
return authResult; return authResult;
} }
@ -165,14 +170,9 @@ export async function send2faCode(
return response.applyOnSuccess((r: AxiosResponse<any>) => { return response.applyOnSuccess((r: AxiosResponse<any>) => {
// r.data.status is 'ok' if the authentication is successful // r.data.status is 'ok' if the authentication is successful
const result = r.data.status === "ok"; const result = r.data.status === "ok";
const message = result ? "Authentication successful" : r.data.errors.join(","); const message: string = result ? AUTH_SUCCESSFUL_MESSAGE : r.data.errors.join(",");
const code = result const code = messageToCode(message);
? LoginResult.AUTH_SUCCESSFUL_2FA return new LoginResult(result, code, message);
: message ===
"The two-step verification value could not be confirmed. Please try again"
? LoginResult.INCORRECT_2FA_CODE
: LoginResult.UNKNOWN_ERROR;
return new LoginResult(result, code, message as string);
}); });
} }
@ -249,12 +249,11 @@ export async function fetchPOSTResponse(
const response = await axios.post(secureURL, urlParams, config); const response = await axios.post(secureURL, urlParams, config);
return success(response); return success(response);
} catch (e) { } catch (e) {
shared.logger.error( const message = `(POST) Error ${e.message} occurred while trying to fetch ${secureURL}`;
`(POST) Error ${e.message} occurred while trying to fetch ${secureURL}` shared.logger.error(message);
);
const genericError = new GenericAxiosError({ const genericError = new GenericAxiosError({
id: 3, id: 3,
message: `(POST) Error ${e.message} occurred while trying to fetch ${secureURL}`, message: message,
error: e error: e
}); });
return failure(genericError); return failure(genericError);
@ -375,13 +374,33 @@ function manageLoginPOSTResponse(response: AxiosResponse<any>) {
// Return the result of the authentication // Return the result of the authentication
const result = errorMessage.trim() === ""; const result = errorMessage.trim() === "";
const message = result ? "Authentication successful" : errorMessage; const message = result ? AUTH_SUCCESSFUL_MESSAGE : errorMessage;
const code = result const code = messageToCode(message);
? LoginResult.AUTH_SUCCESSFUL
: message === "Incorrect password. Please try again."
? LoginResult.INCORRECT_CREDENTIALS
: LoginResult.UNKNOWN_ERROR;
return new LoginResult(result, code, message); return new LoginResult(result, code, message);
} }
/**
* Given the login message response of the
* platform, return the login result code.
*/
function messageToCode(message: string): number {
// Prepare the lookup dict
const mapDict: LookupMapCodeT[] = [];
mapDict.push({
code: LoginResult.AUTH_SUCCESSFUL,
message: AUTH_SUCCESSFUL_MESSAGE
});
mapDict.push({
code: LoginResult.INCORRECT_CREDENTIALS,
message: INCORRECT_CREDENTIALS_MESSAGE
});
mapDict.push({
code: LoginResult.INCORRECT_2FA_CODE,
message: INVALID_2FA_CODE_MESSAGE
});
const result = mapDict.find((e) => e.message === message);
return result ? result.code : LoginResult.UNKNOWN_ERROR;
}
//#endregion //#endregion