DefaultAuthenticator extends AuthenticatorBase implements Authenticator { public final HTTPRequest prepareAuthentication(final HTTPRequest request, final HTTPResponse response) { HTTPRequest req = request; HTTPHost host = new HTTPHost(request.getRequestURI()); if (response == null && registry.matches(host)) { AuthScheme authScheme = registry.get(host); Pair<AuthenticatorStrategy, AuthScheme> selected = select(Arrays.asList(authScheme)); return selected.getKey().prepare(request, authScheme); } if (response != null && response.getStatus() == Status.UNAUTHORIZED) { List<AuthScheme> schemes = toAuthSchemes(response, HeaderConstants.WWW_AUTHENTICATE); if (!schemes.isEmpty() && request.getChallenge() != null) { Pair<AuthenticatorStrategy, AuthScheme> selected = select(schemes); if (selected.getValue() != null) { req = selected.getKey().prepare(request, selected.getValue()); if (req != request) { registry.register(host, selected.getValue()); } } } } return req; } DefaultAuthenticator();  DefaultAuthenticator(List<AuthenticatorStrategy> strategies); final HTTPRequest prepareAuthentication(final HTTPRequest request, final HTTPResponse response); boolean canAuthenticatePreemptively(HTTPRequest request); HTTPRequest preparePreemptiveAuthentication(HTTPRequest request); void afterSuccessfulAuthentication(HTTPRequest request, Headers responseHeaders); void afterFailedAuthentication(HTTPRequest request, Headers responseHeaders);  }
@Test public void testNoAuthExpectTheSameRequest() { final HTTPRequest req = new DefaultAuthenticator().prepareAuthentication(defaultRequest, new HTTPResponse(Status.CREATED, new Headers())); Assert.assertSame(req, defaultRequest); }
@Test public void testAuthBasic() { final HTTPRequest request = defaultRequest.challenge(new UsernamePasswordChallenge("foo", "bar")); final HTTPRequest req = new DefaultAuthenticator().prepareAuthentication(request, new HTTPResponse(Status.UNAUTHORIZED, new Headers().add(HeaderConstants.WWW_AUTHENTICATE, "Basic realm=\"foo\""))); Assert.assertNotSame(req, defaultRequest); Assert.assertTrue("No auth header", req.getHeaders().contains("Authorization")); }
@Test public void testAuthAlmostBasic() { final HTTPRequest request = defaultRequest.challenge(new UsernamePasswordChallenge("foo", "bar")); final HTTPRequest req = new DefaultAuthenticator().prepareAuthentication(request, new HTTPResponse(Status.UNAUTHORIZED, new Headers().add(HeaderConstants.WWW_AUTHENTICATE, "Basicssss raealm=\"foo\""))); Assert.assertSame(req, request); Assert.assertFalse("Auth header was present", req.getHeaders().contains("Authorization")); }