How to Create NTLM Authentication Using Retrofit

Since 23 Android sdk classes have been excluded from classes:

org.apache.http.auth.AuthScheme;
org.apache.http.auth.AuthSchemeFactory;
org.apache.http.impl.auth.NTLMScheme;
org.apache.http.impl.auth.NTLMEngine;
org.apache.http.impl.auth.NTLMEngineException;

How is it now allowed in AD, with a username and password through a modification? There OKHttpklient can be through headers?

+4
source share
1 answer

I found the answer on okhttp github . It was sent to SelvinPL .

NTLM ( NTLMEngineImpl, org.apache.http.impl.auth.NTLMEngineImpl, SelvinPL). SelvinPL (2.1.0).

private static class NTLMAuthenticator implements Authenticator {
    final NTLMEngineImpl engine = new NTLMEngineImpl();
    private final String domain;
    private final String username;
    private final String password;
    private final String ntlmMsg1;

    private NTLMAuthenticator(String username, String password, String domain) {
        this.domain = domain;
        this.username = username;
        this.password = password;
        String localNtlmMsg1 = null;
        try {
            localNtlmMsg1 = engine.generateType1Msg(null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        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, "android-device", WWWAuthenticate.get(0).substring(5));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg3).build();
    }
}

, :

OkHttpClient client = new OkHttpClient.Builder()
            .authenticator(new NTLMAuthenticator(username, password, domain))
            .build();
Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(getURL(context))
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();
return retrofit.create(Api.class);

com.squareup.retrofit2: retrofit: 2.1.0.

0

All Articles