From 48885a860401983dbe19c85bbdae921cd1f57e24 Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Fri, 5 Mar 2021 11:20:46 +0100 Subject: [PATCH] Add response codes to login result --- src/index.ts | 6 +++++- src/scripts/classes/login-result.ts | 26 +++++++++++++++++++++++--- src/scripts/network-helper.ts | 17 +++++++++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8ce9c44..71d8c2b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -98,7 +98,11 @@ export async function login( await fetchPlatformData(); shared.setIsLogged(true); - return new LoginResult(true, `${username} already authenticated (session)`); + return new LoginResult( + true, + LoginResult.ALREADY_AUTHENTICATED, + `${username} already authenticated (session)` + ); } // Creating credentials and fetch unique platform token diff --git a/src/scripts/classes/login-result.ts b/src/scripts/classes/login-result.ts index d0f42ec..aadbc8f 100644 --- a/src/scripts/classes/login-result.ts +++ b/src/scripts/classes/login-result.ts @@ -9,17 +9,37 @@ * Object obtained in response to an attempt to login to the portal. */ export default class LoginResult { + //#region Login result codes + + static REQUIRE_2FA = 100; + static AUTH_SUCCESSFUL = 200; + static AUTH_SUCCESSFUL_2FA = 201; + static ALREADY_AUTHENTICATED = 202; + static UNKNOWN_ERROR = 400; + static INCORRECT_CREDENTIALS = 401; + static INCORRECT_2FA_CODE = 402; + + //#endregion Login result codes + + //#region Properties /** * Result of the login operation */ - success: boolean; + readonly success: boolean; + /** + * Code associated with the result of the login operation. + */ + readonly code: number; /** * Login response message */ - message: string; + readonly message: string; - constructor(success: boolean, message: string) { + //#endregion Properties + + constructor(success: boolean, code: number, message?: string) { this.success = success; + this.code = code; this.message = message; } } diff --git a/src/scripts/network-helper.ts b/src/scripts/network-helper.ts index 2f8ae0d..b41d951 100644 --- a/src/scripts/network-helper.ts +++ b/src/scripts/network-helper.ts @@ -129,6 +129,7 @@ export async function authenticate( ); authResult = new LoginResult( false, + LoginResult.UNKNOWN_ERROR, `Error ${e.message} while authenticating` ); } @@ -169,7 +170,13 @@ export async function send2faCode( const message = result ? "Authentication successful" : r.data.errors.join(","); - return new LoginResult(result, message); + const code = result + ? LoginResult.AUTH_SUCCESSFUL_2FA + : 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); }); } @@ -358,6 +365,7 @@ function manageLoginPOSTResponse(response: AxiosResponse) { if (response.config.url.startsWith(urls.F95_2FA_LOGIN)) { return new LoginResult( false, + LoginResult.REQUIRE_2FA, "Two-factor authentication is needed to continue" ); } @@ -371,7 +379,12 @@ function manageLoginPOSTResponse(response: AxiosResponse) { // Return the result of the authentication const result = errorMessage.trim() === ""; const message = result ? "Authentication successful" : errorMessage; - return new LoginResult(result, message); + const code = result + ? LoginResult.AUTH_SUCCESSFUL + : message === "Incorrect password. Please try again." + ? LoginResult.INCORRECT_CREDENTIALS + : LoginResult.UNKNOWN_ERROR; + return new LoginResult(result, code, message); } //#endregion