DetectAuthenticationMethodOperation.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. ArrayList<String> authHeaders = result.getAuthenticateHeaders();
  86. for (String header : authHeaders) {
  87. // currently we only support basic auth
  88. if (header.toLowerCase().startsWith("basic")) {
  89. authMethod = AuthenticationMethod.BASIC_HTTP_AUTH;
  90. break;
  91. }
  92. }
  93. // else - fall back to UNKNOWN
  94. } else if (result.isSuccess()) {
  95. authMethod = AuthenticationMethod.NONE;
  96. } else if (result.isIdPRedirection()) {
  97. authMethod = AuthenticationMethod.SAML_WEB_SSO;
  98. }
  99. // else - fall back to UNKNOWN
  100. Log_OC.d(TAG, "Authentication method found: " + authenticationMethodToString(authMethod));
  101. if (!authMethod.equals(AuthenticationMethod.UNKNOWN)) {
  102. result = new RemoteOperationResult(true, result.getHttpCode(), result.getHttpPhrase(), null);
  103. }
  104. ArrayList<Object> data = new ArrayList<>();
  105. data.add(authMethod);
  106. result.setData(data);
  107. return result; // same result instance, so that other errors
  108. // can be handled by the caller transparently
  109. }
  110. private String authenticationMethodToString(AuthenticationMethod value) {
  111. switch (value){
  112. case NONE:
  113. return "NONE";
  114. case BASIC_HTTP_AUTH:
  115. return "BASIC_HTTP_AUTH";
  116. case BEARER_TOKEN:
  117. return "BEARER_TOKEN";
  118. case SAML_WEB_SSO:
  119. return "SAML_WEB_SSO";
  120. default:
  121. return "UNKNOWN";
  122. }
  123. }
  124. }