BasicAuthAuthenticator implements WebappAuthenticator { @Override public AuthenticationInfo authenticate(Request request, Response response) { AuthenticationInfo authenticationInfo = new AuthenticationInfo(); Credentials credentials = getCredentials(request); try { int tenantId = Utils.getTenantIdOFUser(credentials.getUsername()); UserStoreManager userStore = AuthenticatorFrameworkDataHolder.getInstance().getRealmService(). getTenantUserRealm(tenantId).getUserStoreManager(); String username = MultitenantUtils.getTenantAwareUsername(credentials.getUsername()); boolean authenticated = userStore.authenticate(username, credentials.getPassword()); if (authenticated) { authenticationInfo.setStatus(Status.CONTINUE); authenticationInfo.setUsername(username); authenticationInfo.setTenantDomain(Utils.getTenantDomain(tenantId)); authenticationInfo.setTenantId(tenantId); } else { authenticationInfo.setMessage("Failed to authorize incoming request."); authenticationInfo.setStatus(Status.FAILURE); } } catch (UserStoreException e) { log.error("Error occurred while authenticating the user." + credentials.getUsername(), e); } catch (AuthenticationException e) { log.error("Error occurred while obtaining the tenant Id for user." + credentials.getUsername(), e); } return authenticationInfo; }  @Override void init(); @Override boolean canHandle(Request request); @Override AuthenticationInfo authenticate(Request request, Response response); @Override String getName(); @Override void setProperties(Properties properties); @Override Properties getProperties(); @Override String getProperty(String name);  }
@Test(description = "This method tests the behaviour of the authenticate method in BasicAuthenticator with valid " + "credentials", dependsOnMethods = "testCanHandleWithRequireParameters") public void testAuthenticateWithValidCredentials() throws EncoderException, IllegalAccessException { String encodedString = new String(Base64.getEncoder().encode((ADMIN_USER + ":" + ADMIN_USER).getBytes())); request = new Request(); context = new StandardContext(); context.addParameter("basicAuth", "true"); request.setContext(context); mimeHeaders = new MimeHeaders(); bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); bytes.setString(BASIC_HEADER + encodedString); coyoteRequest = new org.apache.coyote.Request(); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); AuthenticationInfo authenticationInfo = basicAuthAuthenticator.authenticate(request, null); Assert.assertEquals(authenticationInfo.getStatus(), WebappAuthenticator.Status.CONTINUE, "For a valid user authentication failed."); Assert.assertEquals(authenticationInfo.getUsername(), ADMIN_USER, "Authenticated username for from BasicAuthenticator is not matching with the original user."); Assert.assertEquals(authenticationInfo.getTenantDomain(), MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, "Authenticated user's tenant domain from BasicAuthenticator is not matching with the " + "original user's tenant domain"); Assert.assertEquals(authenticationInfo.getTenantId(), MultitenantConstants.SUPER_TENANT_ID, "Authenticated user's tenant ID from BasicAuthenticator is not matching with the " + "original user's tenant ID"); }
@Test(description = "This method tests the behaviour of the authenticate method in BasicAuthenticator with " + "in-valid credentials", dependsOnMethods = {"testAuthenticateWithValidCredentials"}) public void testAuthenticateWithWrongCredentials() throws IllegalAccessException { String encodedString = new String(Base64.getEncoder().encode((ADMIN_USER + ":test" + ADMIN_USER).getBytes())); mimeHeaders = new MimeHeaders(); bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); bytes.setString(BASIC_HEADER + encodedString); coyoteRequest = new org.apache.coyote.Request(); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); AuthenticationInfo authenticationInfo = basicAuthAuthenticator.authenticate(request, null); Assert.assertEquals(authenticationInfo.getStatus(), WebappAuthenticator.Status.FAILURE, "For a wrong credentials authentication succeeded."); encodedString = new String(Base64.getEncoder().encode((ADMIN_USER).getBytes())); mimeHeaders = new MimeHeaders(); bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); bytes.setString(BASIC_HEADER + encodedString); coyoteRequest = new org.apache.coyote.Request(); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); authenticationInfo = basicAuthAuthenticator.authenticate(request, null); Assert.assertEquals(authenticationInfo.getStatus(), WebappAuthenticator.Status.FAILURE, "For a request with missing password authentication succeeded."); }