Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/main/java/org/htmlunit/DefaultCredentialsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import java.util.HashMap;
import java.util.Map;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.Credentials;
import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.auth.NTCredentials;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.htmlunit.httpclient.HtmlUnitUsernamePasswordCredentials;

/**
Expand Down Expand Up @@ -74,14 +74,20 @@ protected PasswordAuthentication getPasswordAuthentication() {
return null;
}

final AuthScope authScope = new AuthScope(getRequestingHost(), getRequestingPort(), getRequestingScheme());
final Credentials credentials = credentialsProvider_.getCredentials(authScope);
final AuthScope authScope = new AuthScope.Builder()
.setHost(getRequestingHost())
.setPort(getRequestingPort())
.setScheme(getRequestingScheme())
.build();
final Credentials credentials = credentialsProvider_.getCredentials(authScope, null);
if (credentials == null) {
return null;
}

// HttpClient 5 uses char[] for passwords instead of String for better security
// (char arrays can be cleared from memory after use)
return new PasswordAuthentication(credentials.getUserPrincipal().getName(),
credentials.getPassword().toCharArray());
credentials.getPassword());
}
}

Expand Down Expand Up @@ -110,7 +116,11 @@ public void addCredentials(final String username, final char[] password) {
*/
public void addCredentials(final String username, final char[] password, final String host,
final int port, final String realm) {
final AuthScope authscope = new AuthScope(host, port, realm, ANY_SCHEME);
final AuthScope authscope = new AuthScope.Builder()
.setHost(host)
.setPort(port)
.setRealm(realm)
.build();
final HtmlUnitUsernamePasswordCredentials credentials =
new HtmlUnitUsernamePasswordCredentials(username, password);
setCredentials(authscope, credentials);
Expand All @@ -129,8 +139,11 @@ public void addCredentials(final String username, final char[] password, final S
*/
public void addNTLMCredentials(final String username, final char[] password, final String host,
final int port, final String workstation, final String domain) {
final AuthScope authscope = new AuthScope(host, port, ANY_REALM, ANY_SCHEME);
final NTCredentials credentials = new NTCredentials(username, String.valueOf(password), workstation, domain);
final AuthScope authscope = new AuthScope.Builder()
.setHost(host)
.setPort(port)
.build();
final NTCredentials credentials = new NTCredentials(username, password, workstation, domain);
setCredentials(authscope, credentials);
}

Expand All @@ -143,7 +156,10 @@ public void addNTLMCredentials(final String username, final char[] password, fin
*/
public void addSocksCredentials(final String username, final char[] password, final String host,
final int port) {
final AuthScope authscope = new AuthScope(host, port, ANY_REALM, ANY_SCHEME);
final AuthScope authscope = new AuthScope.Builder()
.setHost(host)
.setPort(port)
.build();
final HtmlUnitUsernamePasswordCredentials credentials =
new HtmlUnitUsernamePasswordCredentials(username, password);
setCredentials(authscope, credentials);
Expand Down Expand Up @@ -210,7 +226,8 @@ private static Credentials matchCredentials(final Map<AuthScopeProxy, Credential
* {@inheritDoc}
*/
@Override
public synchronized Credentials getCredentials(final AuthScope authscope) {
public synchronized Credentials getCredentials(final AuthScope authscope,
final org.apache.hc.core5.http.protocol.HttpContext context) {
if (authscope == null) {
throw new IllegalArgumentException("Authentication scope may not be null");
}
Expand Down Expand Up @@ -282,7 +299,12 @@ private void readObject(final ObjectInputStream stream) throws IOException, Clas
final int port = stream.readInt();
final String realm = (String) stream.readObject();
final String scheme = (String) stream.readObject();
authScope_ = new AuthScope(host, port, realm, scheme);
authScope_ = new AuthScope.Builder()
.setHost(host)
.setPort(port)
.setRealm(realm)
.setScheme(scheme)
.build();
}

@Override
Expand Down
Loading