DetectAuthenticationMethodOperation.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* ownCloud Android Library is available under MIT license
  2. * Copyright (C) 2014 ownCloud Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. *
  23. */
  24. package com.owncloud.android.operations;
  25. import java.util.ArrayList;
  26. import com.owncloud.android.lib.common.OwnCloudClient;
  27. import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
  28. import com.owncloud.android.lib.common.operations.RemoteOperation;
  29. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  30. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  31. import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
  32. import android.content.Context;
  33. import android.net.Uri;
  34. import android.util.Log;
  35. /**
  36. * Operation to find out what authentication method requires
  37. * the server to access files.
  38. *
  39. * Basically, tries to access to the root folder without authorization
  40. * and analyzes the response.
  41. *
  42. * When successful, the instance of {@link RemoteOperationResult} passed
  43. * through {@link OnRemoteOperationListener#onRemoteOperationFinish(RemoteOperation,
  44. * RemoteOperationResult)} returns in {@link RemoteOperationResult#getData()}
  45. * a value of {@link AuthenticationMethod}.
  46. *
  47. * @author David A. Velasco
  48. */
  49. public class DetectAuthenticationMethodOperation extends RemoteOperation {
  50. private static final String TAG = DetectAuthenticationMethodOperation.class.getSimpleName();
  51. public enum AuthenticationMethod {
  52. UNKNOWN,
  53. NONE,
  54. BASIC_HTTP_AUTH,
  55. SAML_WEB_SSO,
  56. BEARER_TOKEN
  57. }
  58. private Context mContext;
  59. private String mWebDavUrl;
  60. /**
  61. * Constructor
  62. *
  63. * @param context Android context of the caller.
  64. * @param webdavUrl
  65. */
  66. public DetectAuthenticationMethodOperation(Context context, String webdavUrl) {
  67. mContext = context;
  68. mWebDavUrl = webdavUrl;
  69. }
  70. /**
  71. * Performs the operation.
  72. *
  73. * Triggers a check of existence on the root folder of the server, granting
  74. * that the request is not authenticated.
  75. *
  76. * Analyzes the result of check to find out what authentication method, if
  77. * any, is requested by the server.
  78. */
  79. @Override
  80. protected RemoteOperationResult run(OwnCloudClient client) {
  81. RemoteOperationResult result = null;
  82. AuthenticationMethod authMethod = AuthenticationMethod.UNKNOWN;
  83. RemoteOperation operation = new ExistenceCheckRemoteOperation("", mContext, false);
  84. client.setWebdavUri(Uri.parse(mWebDavUrl));
  85. client.setBasicCredentials("", "");
  86. client.setFollowRedirects(false);
  87. // try to access the root folder, following redirections but not SAML SSO redirections
  88. result = operation.execute(client);
  89. String redirectedLocation = result.getRedirectedLocation();
  90. while (redirectedLocation != null && redirectedLocation.length() > 0 && !result.isIdPRedirection()) {
  91. client.setWebdavUri(Uri.parse(result.getRedirectedLocation()));
  92. result = operation.execute(client);
  93. redirectedLocation = result.getRedirectedLocation();
  94. }
  95. // analyze response
  96. if (result.getCode() == ResultCode.UNAUTHORIZED) {
  97. String authRequest = ((result.getAuthenticateHeader()).trim()).toLowerCase();
  98. if (authRequest.startsWith("basic")) {
  99. authMethod = AuthenticationMethod.BASIC_HTTP_AUTH;
  100. } else if (authRequest.startsWith("bearer")) {
  101. authMethod = AuthenticationMethod.BEARER_TOKEN;
  102. }
  103. // else - fall back to UNKNOWN
  104. } else if (result.isSuccess()) {
  105. authMethod = AuthenticationMethod.NONE;
  106. } else if (result.isIdPRedirection()) {
  107. authMethod = AuthenticationMethod.SAML_WEB_SSO;
  108. }
  109. // else - fall back to UNKNOWN
  110. Log.d(TAG, "Authentication method found: " + authenticationMethodToString(authMethod));
  111. if (!authMethod.equals(AuthenticationMethod.UNKNOWN)) {
  112. result = new RemoteOperationResult(true, result.getHttpCode(), null);
  113. }
  114. ArrayList<Object> data = new ArrayList<Object>();
  115. data.add(authMethod);
  116. result.setData(data);
  117. return result; // same result instance, so that other errors can be handled by the caller transparently
  118. }
  119. private String authenticationMethodToString(AuthenticationMethod value) {
  120. switch (value){
  121. case NONE:
  122. return "NONE";
  123. case BASIC_HTTP_AUTH:
  124. return "BASIC_HTTP_AUTH";
  125. case BEARER_TOKEN:
  126. return "BEARER_TOKEN";
  127. case SAML_WEB_SSO:
  128. return "SAML_WEB_SSO";
  129. default:
  130. return "UNKNOWN";
  131. }
  132. }
  133. }