Advertisement
Guest User

Untitled

a guest
Oct 6th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. org.apache.http.auth.AuthScheme;
  2. org.apache.http.auth.AuthSchemeFactory;
  3. org.apache.http.impl.auth.NTLMScheme;
  4. org.apache.http.impl.auth.NTLMEngine;
  5. org.apache.http.impl.auth.NTLMEngineException;
  6.  
  7. private static class NTLMAuthenticator implements Authenticator {
  8. final NTLMEngineImpl engine = new NTLMEngineImpl();
  9. private final String domain;
  10. private final String username;
  11. private final String password;
  12. private final String ntlmMsg1;
  13.  
  14. private NTLMAuthenticator(String username, String password, String domain) {
  15. this.domain = domain;
  16. this.username = username;
  17. this.password = password;
  18. String localNtlmMsg1 = null;
  19. try {
  20. localNtlmMsg1 = engine.generateType1Msg(null, null);
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. ntlmMsg1 = localNtlmMsg1;
  25. }
  26.  
  27. @Override
  28. public Request authenticate(Route route, Response response) throws IOException {
  29. final List<String> WWWAuthenticate = response.headers().values("WWW-Authenticate");
  30. if (WWWAuthenticate.contains("NTLM")) {
  31. return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg1).build();
  32. }
  33. String ntlmMsg3 = null;
  34. try {
  35. ntlmMsg3 = engine.generateType3Msg(username, password, domain, "android-device", WWWAuthenticate.get(0).substring(5));
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg3).build();
  40. }
  41. }
  42.  
  43. OkHttpClient client = new OkHttpClient.Builder()
  44. .authenticator(new NTLMAuthenticator(username, password, domain))
  45. .build();
  46. Retrofit retrofit = new Retrofit.Builder()
  47. .baseUrl(getURL(context))
  48. .addConverterFactory(GsonConverterFactory.create())
  49. .client(client)
  50. .build();
  51. return retrofit.create(Api.class);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement