Process share with password
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / DetectAuthenticationMethodOperation.java
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
21 package com.owncloud.android.operations;
22
23 import java.util.ArrayList;
24
25 import com.owncloud.android.lib.common.OwnCloudClient;
26 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
27 import com.owncloud.android.lib.common.operations.RemoteOperation;
28 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
29 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
30 import com.owncloud.android.lib.common.utils.Log_OC;
31 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
32
33 import android.content.Context;
34 import android.net.Uri;
35
36 /**
37 * Operation to find out what authentication method requires
38 * the server to access files.
39 *
40 * Basically, tries to access to the root folder without authorization
41 * and analyzes the response.
42 *
43 * When successful, the instance of {@link RemoteOperationResult} passed
44 * through {@link OnRemoteOperationListener#onRemoteOperationFinish(RemoteOperation,
45 * RemoteOperationResult)} returns in {@link RemoteOperationResult#getData()}
46 * a value of {@link AuthenticationMethod}.
47 */
48 public class DetectAuthenticationMethodOperation extends RemoteOperation {
49
50 private static final String TAG = DetectAuthenticationMethodOperation.class.getSimpleName();
51
52 public enum AuthenticationMethod {
53 UNKNOWN,
54 NONE,
55 BASIC_HTTP_AUTH,
56 SAML_WEB_SSO,
57 BEARER_TOKEN
58 }
59
60 private Context mContext;
61
62 /**
63 * Constructor
64 *
65 * @param context Android context of the caller.
66 * @param webdavUrl
67 */
68 public DetectAuthenticationMethodOperation(Context context) {
69 mContext = context;
70 }
71
72
73 /**
74 * Performs the operation.
75 *
76 * Triggers a check of existence on the root folder of the server, granting
77 * that the request is not authenticated.
78 *
79 * Analyzes the result of check to find out what authentication method, if
80 * any, is requested by the server.
81 */
82 @Override
83 protected RemoteOperationResult run(OwnCloudClient client) {
84 RemoteOperationResult result = null;
85 AuthenticationMethod authMethod = AuthenticationMethod.UNKNOWN;
86
87 RemoteOperation operation = new ExistenceCheckRemoteOperation("", mContext, false);
88 client.clearCredentials();
89 client.setFollowRedirects(false);
90
91 // try to access the root folder, following redirections but not SAML SSO redirections
92 result = operation.execute(client);
93 String redirectedLocation = result.getRedirectedLocation();
94 while (redirectedLocation != null && redirectedLocation.length() > 0 &&
95 !result.isIdPRedirection()) {
96 client.setBaseUri(Uri.parse(result.getRedirectedLocation()));
97 result = operation.execute(client);
98 redirectedLocation = result.getRedirectedLocation();
99 }
100
101 // analyze response
102 if (result.getCode() == ResultCode.UNAUTHORIZED) {
103 String authRequest = ((result.getAuthenticateHeader()).trim()).toLowerCase();
104 if (authRequest.startsWith("basic")) {
105 authMethod = AuthenticationMethod.BASIC_HTTP_AUTH;
106
107 } else if (authRequest.startsWith("bearer")) {
108 authMethod = AuthenticationMethod.BEARER_TOKEN;
109 }
110 // else - fall back to UNKNOWN
111
112 } else if (result.isSuccess()) {
113 authMethod = AuthenticationMethod.NONE;
114
115 } else if (result.isIdPRedirection()) {
116 authMethod = AuthenticationMethod.SAML_WEB_SSO;
117 }
118 // else - fall back to UNKNOWN
119 Log_OC.d(TAG, "Authentication method found: " + authenticationMethodToString(authMethod));
120
121 if (!authMethod.equals(AuthenticationMethod.UNKNOWN)) {
122 result = new RemoteOperationResult(true, result.getHttpCode(), null);
123 }
124 ArrayList<Object> data = new ArrayList<Object>();
125 data.add(authMethod);
126 result.setData(data);
127 return result; // same result instance, so that other errors can be handled by the caller transparently
128 }
129
130
131 private String authenticationMethodToString(AuthenticationMethod value) {
132 switch (value){
133 case NONE:
134 return "NONE";
135 case BASIC_HTTP_AUTH:
136 return "BASIC_HTTP_AUTH";
137 case BEARER_TOKEN:
138 return "BEARER_TOKEN";
139 case SAML_WEB_SSO:
140 return "SAML_WEB_SSO";
141 default:
142 return "UNKNOWN";
143 }
144 }
145
146 }