B4J Question NTLM again, now with 8.90

swChef

Active Member
Licensed User
Longtime User
I took the example from post 7 on this thread and tried to compile it with 8.90.
The rest of this post is just detailing things I tried to get that code working again under 8.90.


B4X:
Compiling generated Java code.    Error
javac 1.8.0_151
src\b4j\example\main.java:11: error: package com.squareup.okhttp does not exist
import com.squareup.okhttp.Authenticator;
                          ^
1 error

Swapped the inline imports for those in your B4A related post 4 on this thread and tried to figure out a mash-up. Didn't figure out yet how to deal with the NTLM javaobject setup.

B4X:
src\b4j\example\main.java:282: error: cannot find symbol
     OkHttpClient.client.setAuthenticator(new NTLMAuthenticator(username, password, domain, workstation));
                        ^
  symbol:   method setAuthenticator(NTLMAuthenticator)
  location: variable client of type OkHttpClient
1 error

etc (not showing everything tried).

I had also tried removing the included sources in the project, added jOkHttpUtils2 (2.95).
Ran into the HttpUtils2Service.Initialize issue (no Initialize).

Not asking for each thing here to be explained or tried, just showing what I did look into (besides reading forum posts and external postings and content).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Start with this code:
B4X:
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

Use jOkHttpUtils2 library. Remove the modules.

Add to build configuration: HU2_PUBLIC
 
Upvote 0

swChef

Active Member
Licensed User
Longtime User
Thank you.
I converted the example template but won't test it until Monday. Sharing the updated project here (also so I can grab it from my other computer)
 

Attachments

  • NTLM_okhttp3_compiles.zip
    3.7 KB · Views: 226
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…