public DirectDigestAuthClient(final Authenticator digestAuthenticator, final ProfileCreator profileCreator) { defaultAuthenticator(digestAuthenticator); defaultProfileCreator(profileCreator); }
@Override protected void clientInit() { defaultCredentialsExtractor(new DigestAuthExtractor()); }
@Override public String toString() { return CommonHelper.toNiceString(this.getClass(), "name", getName(), "realm", this.realm, "extractor", getCredentialsExtractor(), "authenticator", getAuthenticator(), "profileCreator", getProfileCreator()); } }
@Test public void testAuthentication() { final DirectDigestAuthClient client = new DirectDigestAuthClient(new SimpleTestDigestAuthenticator()); client.setRealm(REALM); final MockWebContext context = MockWebContext.create(); context.addRequestHeader(AUTHORIZATION_HEADER, DIGEST_AUTHORIZATION_HEADER_VALUE); context.setRequestMethod(HTTP_METHOD.GET.name()); final DigestCredentials credentials = client.getCredentials(context); final CommonProfile profile = client.getUserProfile(credentials, context); String ha1 = CredentialUtil.encryptMD5(USERNAME + ":" + REALM + ":" +PASSWORD); String serverDigest1 = credentials.calculateServerDigest(true, ha1); String serverDigest2 = credentials.calculateServerDigest(false, PASSWORD); assertEquals(DIGEST_RESPONSE, serverDigest1); assertEquals(DIGEST_RESPONSE, serverDigest2); assertEquals(USERNAME, profile.getId()); } }
@Test public void testMissingUsernamePasswordAuthenticator() { final DirectDigestAuthClient digestAuthClient = new DirectDigestAuthClient(null); TestsHelper.expectException(() -> digestAuthClient.getCredentials(MockWebContext.create()), TechnicalException.class, "authenticator cannot be null"); }
@Test public void testHasDefaultProfileCreator() { final DirectDigestAuthClient digestAuthClient = new DirectDigestAuthClient(new SimpleTestTokenAuthenticator()); digestAuthClient.init(); }
@Test public void testMissingProfileCreator() { final DirectDigestAuthClient digestAuthClient = new DirectDigestAuthClient(new SimpleTestTokenAuthenticator(), null); TestsHelper.expectException(() -> digestAuthClient.getUserProfile(new DigestCredentials(TOKEN, HTTP_METHOD.POST.name(), null, null, null, null, null, null, null), MockWebContext.create()), TechnicalException.class, "profileCreator cannot be null"); }
public DirectDigestAuthClient(final Authenticator digestAuthenticator) { defaultAuthenticator(digestAuthenticator); }
/** Per RFC 2617 * If a server receives a request for an access-protected object, and an * acceptable Authorization header is not sent, the server responds with * a "401 Unauthorized" status code, and a WWW-Authenticate header */ @Override protected DigestCredentials retrieveCredentials(final WebContext context) { // set the www-authenticate in case of error final String nonce = calculateNonce(); context.setResponseHeader(HttpConstants.AUTHENTICATE_HEADER, "Digest realm=\"" + realm + "\", qop=\"auth\", nonce=\"" + nonce + "\""); return super.retrieveCredentials(context); }