DetectAuthenticationMethodOperation.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.operations;
  21. import android.content.Context;
  22. import android.net.Uri;
  23. import com.owncloud.android.lib.common.OwnCloudClient;
  24. import com.owncloud.android.lib.common.operations.RemoteOperation;
  25. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  26. import com.owncloud.android.lib.common.utils.Log_OC;
  27. import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
  28. import org.apache.commons.httpclient.HttpStatus;
  29. import java.util.ArrayList;
  30. /**
  31. * Operation to find out what authentication method requires the server to access files.
  32. *
  33. * Basically, tries to access to the root folder without authorization and analyzes the response.
  34. *
  35. * When successful, the instance of {@link RemoteOperationResult} passed through
  36. * {@link com.owncloud.android.lib.common.operations.OnRemoteOperationListener
  37. * #onRemoteOperationFinish(RemoteOperation, RemoteOperationResult)} returns in
  38. * {@link RemoteOperationResult#getData()} a value of {@link AuthenticationMethod}.
  39. */
  40. public class DetectAuthenticationMethodOperation extends RemoteOperation {
  41. private static final String TAG = DetectAuthenticationMethodOperation.class.getSimpleName();
  42. public enum AuthenticationMethod {
  43. UNKNOWN,
  44. NONE,
  45. BASIC_HTTP_AUTH,
  46. SAML_WEB_SSO,
  47. BEARER_TOKEN
  48. }
  49. private Context mContext;
  50. /**
  51. * Constructor
  52. *
  53. * @param context Android context of the caller.
  54. */
  55. public DetectAuthenticationMethodOperation(Context context) {
  56. mContext = context;
  57. }
  58. /**
  59. * Performs the operation.
  60. *
  61. * Triggers a check of existence on the root folder of the server, granting
  62. * that the request is not authenticated.
  63. *
  64. * Analyzes the result of check to find out what authentication method, if
  65. * any, is requested by the server.
  66. */
  67. @Override
  68. protected RemoteOperationResult run(OwnCloudClient client) {
  69. RemoteOperationResult result = null;
  70. AuthenticationMethod authMethod = AuthenticationMethod.UNKNOWN;
  71. RemoteOperation operation = new ExistenceCheckRemoteOperation("", mContext, false);
  72. client.clearCredentials();
  73. client.setFollowRedirects(false);
  74. // try to access the root folder, following redirections but not SAML SSO redirections
  75. result = operation.execute(client);
  76. String redirectedLocation = result.getRedirectedLocation();
  77. while (redirectedLocation != null && redirectedLocation.length() > 0 &&
  78. !result.isIdPRedirection()) {
  79. client.setBaseUri(Uri.parse(result.getRedirectedLocation()));
  80. result = operation.execute(client);
  81. redirectedLocation = result.getRedirectedLocation();
  82. }
  83. // analyze response
  84. if (result.getHttpCode() == HttpStatus.SC_UNAUTHORIZED) {
  85. String authRequest = ((result.getAuthenticateHeader()).trim()).toLowerCase();
  86. if (authRequest.startsWith("basic")) {
  87. authMethod = AuthenticationMethod.BASIC_HTTP_AUTH;
  88. } else if (authRequest.startsWith("bearer")) {
  89. authMethod = AuthenticationMethod.BEARER_TOKEN;
  90. }
  91. // else - fall back to UNKNOWN
  92. } else if (result.isSuccess()) {
  93. authMethod = AuthenticationMethod.NONE;
  94. } else if (result.isIdPRedirection()) {
  95. authMethod = AuthenticationMethod.SAML_WEB_SSO;
  96. }
  97. // else - fall back to UNKNOWN
  98. Log_OC.d(TAG, "Authentication method found: " + authenticationMethodToString(authMethod));
  99. if (!authMethod.equals(AuthenticationMethod.UNKNOWN)) {
  100. result = new RemoteOperationResult(true, result.getHttpCode(), null);
  101. }
  102. ArrayList<Object> data = new ArrayList<Object>();
  103. data.add(authMethod);
  104. result.setData(data);
  105. return result; // same result instance, so that other errors
  106. // can be handled by the caller transparently
  107. }
  108. private String authenticationMethodToString(AuthenticationMethod value) {
  109. switch (value){
  110. case NONE:
  111. return "NONE";
  112. case BASIC_HTTP_AUTH:
  113. return "BASIC_HTTP_AUTH";
  114. case BEARER_TOKEN:
  115. return "BEARER_TOKEN";
  116. case SAML_WEB_SSO:
  117. return "SAML_WEB_SSO";
  118. default:
  119. return "UNKNOWN";
  120. }
  121. }
  122. }