Sub Process_Globals
Private MainForm As Form
Private TextFieldPW As TextField
Private TextFieldTParams As TextField
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Login") 'Load the layout file.
MainForm.Show
Dim NTLMAuthenticator As JavaObject
NTLMAuthenticator.InitializeNewInstance("b4j.example.main$NTLMAuthenticator", Array("user name", "password", "domain", "workstation"))
Dim jo As JavaObject = HttpUtils2Service.hc
Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
builder.RunMethod("authenticator", Array(NTLMAuthenticator))
jo.SetField("client", builder.RunMethod("build", Null))
End Sub
#if JAVA
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.Proxy;
import java.util.List;
import org.apache.http.impl.auth.NTLMEngine;
import org.apache.http.impl.auth.NTLMEngineException;
import org.apache.http.impl.auth.NTLMScheme;
import anywheresoftware.b4h.okhttp.OkHttpClientWrapper;
import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.JavaNetCookieJar;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.Route;
import okhttp3.internal.Util;
import okhttp3.internal.http.RequestLine;
public static class NTLMAuthenticator implements Authenticator {
final NTLMEngine engine;
private final String domain;
private final String username;
private final String password;
private final String ntlmMsg1;
private final String workstation;
public NTLMAuthenticator(String username, String password, String domain, String workstation) throws Exception {
this.domain = domain;
this.username = username;
this.workstation = workstation;
this.password = password;
NTLMScheme scheme = new NTLMScheme();
Field f = scheme.getClass().getDeclaredField("engine");
f.setAccessible(true);
engine = (NTLMEngine)f.get(scheme);
String localNtlmMsg1 = null;
localNtlmMsg1 = engine.generateType1Msg(domain, workstation);
ntlmMsg1 = localNtlmMsg1;
}
@Override
public Request authenticate(Route route, Response response) throws IOException {
final List<String> WWWAuthenticate = response.headers().values("WWW-Authenticate");
if (WWWAuthenticate.contains("NTLM")) {
return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg1).build();
}
String ntlmMsg3 = null;
try {
ntlmMsg3 = engine.generateType3Msg(username, password, domain, workstation, WWWAuthenticate.get(0).substring(5));
} catch (NTLMEngineException e) {
throw new RuntimeException(e);
}
return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg3).build();
}
}
#end if