DetectAuthenticationMethodOperation.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.common.utils.Log_OC;
  32. import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
  33. import android.content.Context;
  34. import android.net.Uri;
  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. /**
  60. * Constructor
  61. *
  62. * @param context Android context of the caller.
  63. * @param webdavUrl
  64. */
  65. public DetectAuthenticationMethodOperation(Context context) {
  66. mContext = context;
  67. }
  68. /**
  69. * Performs the operation.
  70. *
  71. * Triggers a check of existence on the root folder of the server, granting
  72. * that the request is not authenticated.
  73. *
  74. * Analyzes the result of check to find out what authentication method, if
  75. * any, is requested by the server.
  76. */
  77. @Override
  78. protected RemoteOperationResult run(OwnCloudClient client) {
  79. RemoteOperationResult result = null;
  80. AuthenticationMethod authMethod = AuthenticationMethod.UNKNOWN;
  81. RemoteOperation operation = new ExistenceCheckRemoteOperation("", mContext, false);
  82. client.clearCredentials();
  83. client.setFollowRedirects(false);
  84. // try to access the root folder, following redirections but not SAML SSO redirections
  85. result = operation.execute(client);
  86. String redirectedLocation = result.getRedirectedLocation();
  87. while (redirectedLocation != null && redirectedLocation.length() > 0 &&
  88. !result.isIdPRedirection()) {
  89. client.setBaseUri(Uri.parse(result.getRedirectedLocation()));
  90. result = operation.execute(client);
  91. redirectedLocation = result.getRedirectedLocation();
  92. }
  93. // analyze response
  94. if (result.getCode() == ResultCode.UNAUTHORIZED) {
  95. String authRequest = ((result.getAuthenticateHeader()).trim()).toLowerCase();
  96. if (authRequest.startsWith("basic")) {
  97. authMethod = AuthenticationMethod.BASIC_HTTP_AUTH;
  98. } else if (authRequest.startsWith("bearer")) {
  99. authMethod = AuthenticationMethod.BEARER_TOKEN;
  100. }
  101. // else - fall back to UNKNOWN
  102. } else if (result.isSuccess()) {
  103. authMethod = AuthenticationMethod.NONE;
  104. } else if (result.isIdPRedirection()) {
  105. authMethod = AuthenticationMethod.SAML_WEB_SSO;
  106. }
  107. // else - fall back to UNKNOWN
  108. Log_OC.d(TAG, "Authentication method found: " + authenticationMethodToString(authMethod));
  109. if (!authMethod.equals(AuthenticationMethod.UNKNOWN)) {
  110. result = new RemoteOperationResult(true, result.getHttpCode(), null);
  111. }
  112. ArrayList<Object> data = new ArrayList<Object>();
  113. data.add(authMethod);
  114. result.setData(data);
  115. return result; // same result instance, so that other errors can be handled by the caller transparently
  116. }
  117. private String authenticationMethodToString(AuthenticationMethod value) {
  118. switch (value){
  119. case NONE:
  120. return "NONE";
  121. case BASIC_HTTP_AUTH:
  122. return "BASIC_HTTP_AUTH";
  123. case BEARER_TOKEN:
  124. return "BEARER_TOKEN";
  125. case SAML_WEB_SSO:
  126. return "SAML_WEB_SSO";
  127. default:
  128. return "UNKNOWN";
  129. }
  130. }
  131. }