DetectAuthenticationMethodOperation.java 5.2 KB

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