LdapAuthenticationHandler implements AuthenticationHandler { @Override public AuthenticationToken authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, AuthenticationException { AuthenticationToken token = null; String authorization = request.getHeader(HttpConstants.AUTHORIZATION_HEADER); if (authorization == null || !AuthenticationHandlerUtil.matchAuthScheme(HttpConstants.BASIC, authorization)) { response.setHeader(WWW_AUTHENTICATE, HttpConstants.BASIC); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); if (authorization == null) { logger.trace("Basic auth starting"); } else { logger.warn("'" + HttpConstants.AUTHORIZATION_HEADER + "' does not start with '" + HttpConstants.BASIC + "' : {}", authorization); } } else { authorization = authorization.substring(HttpConstants.BASIC.length()).trim(); final Base64 base64 = new Base64(0); String[] credentials = new String(base64.decode(authorization), StandardCharsets.UTF_8).split(":", 2); if (credentials.length == 2) { token = authenticateUser(credentials[0], credentials[1]); response.setStatus(HttpServletResponse.SC_OK); } } return token; }  @VisibleForTesting void setEnableStartTls(Boolean enableStartTls); @VisibleForTesting void setDisableHostNameVerification(
      Boolean disableHostNameVerification); @Override String getType(); @Override void init(Properties config); @Override void destroy(); @Override boolean managementOperation(AuthenticationToken token,
      HttpServletRequest request, HttpServletResponse response); @Override AuthenticationToken authenticate(HttpServletRequest request,
      HttpServletResponse response); static final String TYPE; static final String SECURITY_AUTHENTICATION; static final String PROVIDER_URL; static final String BASE_DN; static final String LDAP_BIND_DOMAIN; static final String ENABLE_START_TLS; }
@Test(timeout = 60000) public void testRequestWithoutAuthorization() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); Assert.assertNull(handler.authenticate(request, response)); Mockito.verify(response).setHeader(WWW_AUTHENTICATE, HttpConstants.BASIC); Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); }
@Test(timeout = 60000) public void testRequestWithInvalidAuthorization() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); final Base64 base64 = new Base64(0); String credentials = "bjones:invalidpassword"; Mockito.when(request.getHeader(HttpConstants.AUTHORIZATION_HEADER)) .thenReturn(base64.encodeToString(credentials.getBytes())); Assert.assertNull(handler.authenticate(request, response)); Mockito.verify(response).setHeader(WWW_AUTHENTICATE, HttpConstants.BASIC); Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); }
@Test(timeout = 60000) public void testRequestWithIncompleteAuthorization() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); Mockito.when(request.getHeader(HttpConstants.AUTHORIZATION_HEADER)) .thenReturn(HttpConstants.BASIC); Assert.assertNull(handler.authenticate(request, response)); }
@Test(timeout = 60000) public void testRequestWithWrongCredentials() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); final Base64 base64 = new Base64(0); String credentials = base64.encodeToString("bjones:foo123".getBytes()); String authHeader = HttpConstants.BASIC + " " + credentials; Mockito.when(request.getHeader(HttpConstants.AUTHORIZATION_HEADER)) .thenReturn(authHeader); try { handler.authenticate(request, response); Assert.fail(); } catch (AuthenticationException ex) { } catch (Exception ex) { Assert.fail(); } }