Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- 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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement