Documentation

Looking for something in particular?

Secure Login with Multi-Factor Authentication (MFA) in Provar

This article provides information for users who want to use Multi-Factor Authentication (MFA) to securely log in to applications. Users can use this feature to launch apps and create Provar scripts with enhanced security features.

The feature allows support for various TOTP providers like SF Authenticator, Google Authenticator, and more. The established connection can be used in all execution modes, including Run, Debug, and Test Builder while maintaining the session. The Auth Handler page object file should manage MFA TOTP scenarios, including TOTP generation and code entry on the login page. 

Note:  The user must enter all of the information in the Auth Handler Page Object file just once. The login sequence and success condition must be maintained by the user in the Auth Handler Page object file.  Some key points to note for the secret key:

  • If the application provides the key in small letters then the user needs to convert it into capital letters.
  • If the application provides the key number of characters that are not divisible by 8. then the user needs to add the ‘=‘ sign to make it divisible by 8.

Follow these steps to use MFA with Provar.

1. Create UI Connection by selecting the MFA checkbox. 

2. Provide all required details along with the Auth Handler file and security key.

3. Verify connection using Test Connection and Click Ok

Provar’s increased security capability for logging into applications with MFA allows users to write Provar scripts in a secure and flexible manner. Secure login processes can be seamlessly integrated into users’ testing workflows.

Sample JAVA file

package pageobjects.Sample.SSO.SSOwithMFA;

import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.provar.core.model.base.api.IRuntimeConfiguration;
import com.provar.core.testapi.ILoginPage;
import com.provar.core.testapi.ILoginResult;
import com.provar.core.testapi.annotations.Page;
import com.provar.core.testapi.java.UiLoginResultImpl;

@Page(title = "MFA_Login", connection = "Connection Name")
public class MFA_Login implements ILoginPage {

    @Override
    public ILoginResult doLogin(IRuntimeConfiguration runtimeConfiguration, WebDriver driver,
            Map<String, String> credentials) {

        // Connection 'SSO Username' and 'SSO Password' stored in userName and password
        // variables

        String userName = credentials.get(CREDENTIAL_USER);
        String password = credentials.get(CREDENTIAL_PASSWORD);

        // User can put wait here to load the page

        // Check if the user is already logged in
        if (isLoggedIn(driver)) {
            return new UiLoginResultImpl(true, driver);
        } else {
            // Enter user secret details to login and submit

            driver.findElement(By.xpath("<Xpath for username field>")).sendKeys(userName);
            driver.findElement(By.xpath("<Xpath for password field>")).sendKeys(password);

            driver.findElement(By.xpath("<Xpath for submit/login button>")).click();

            // User can put wait here to load the page (Added 2 second wait)
            waitForPageToLoad(2000);

            // Check if user navigate to successfully on opt page(if this is true it means
            // user still on login page).
            if (isInvalidLoginCredentials(driver)) {
                return new UiLoginResultImpl(false, "Invalid login credentials", driver);
            }

            // Get Generated OTP and enter in otp in the field 
            
            int count = 0;
            WebElement otpElement = driver.findElement(By.xpath("<Xpath of otp field>"));
            while (count < 2) {
                try {
                    String otp = generateTOTP(credentials);
                    otpElement.clear();
                    otpElement.sendKeys(otp);
                    driver.findElement(By.xpath("<Xpath of Verify button>")).click();
                    otpElement = driver.findElement(By.xpath("<Xpath of otp field>"));
                    count++;
                } catch (Exception e) {
                    break;
                }
            }

            // Check again if user entered correct OTP and logged in successfully
            boolean loginSuccess = isLoggedIn(driver);
            if (loginSuccess) {
                return new UiLoginResultImpl(true, "Login Successful.", driver);
            } else {
                return new UiLoginResultImpl(false, "Login failed : Please check your credentials.", driver);

            }
        }

    }

    private boolean isLoggedIn(WebDriver driver) {
        try {
            driver.findElement(By.xpath("<Xpath for field to check user is logged in>"));
        } catch (NoSuchElementException ex) {
            return false;
        }
        return true;
    }

    private static void waitForPageToLoad(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private boolean isInvalidLoginCredentials(WebDriver driver) {
        try {
            driver.findElement(By.xpath("<Xpath for field available in login page(like: Password field Xpath)"));
        } catch (NoSuchElementException ex) {
            return false;
        }
        return true;
    }

}

Feedback

Was this article helpful for you?
Documentation library

Trying to raise a case with our support team?

We use cookies to better understand how our website is used so we can tailor content for you. For more information about the different cookies we use please take a look at our Privacy Policy.

Scroll to Top