e4396a4822c14e06b0702a9b0925682ab866d17e
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / authenticator / AuthUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.authenticator;
20
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.net.UnknownHostException;
25
26 import org.apache.http.HttpHost;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.auth.AuthScope;
29 import org.apache.http.auth.UsernamePasswordCredentials;
30 import org.apache.http.client.ClientProtocolException;
31 import org.apache.http.client.methods.HttpHead;
32 import org.apache.http.impl.auth.BasicScheme;
33 import org.apache.http.impl.client.DefaultHttpClient;
34 import org.apache.http.protocol.BasicHttpContext;
35
36 import android.content.Context;
37 import android.os.Handler;
38 import android.util.Log;
39
40 public class AuthUtils {
41 public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
42 public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
43 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
44
45 private static String mResultMsg = "";
46
47 public static boolean authenticate(URL url, String username, String password,
48 Handler handler, Context context) {
49 String strippedPath = url.toString().endsWith("/") ?
50 url.toString().substring(0, url.toString().length()-1) :
51 url.toString();
52 String webdatPath = strippedPath + WEBDAV_PATH_2_0;
53 URL complete_url = null;
54 try {
55 complete_url = new URL(webdatPath);
56 } catch (MalformedURLException e) {
57 // should never happend
58 sendResult(false, handler, context, "URL error");
59 return false;
60 }
61
62 // version 2.0 success
63 if (tryGetWebdav(complete_url, username, password, handler, context)) {
64 sendResult(true, handler, context, complete_url.toString());
65 return true;
66 }
67
68 if (mResultMsg.equals("401")) {
69 sendResult(false, handler, context, "Invalid login or/and password");
70 return false;
71 }
72
73 if (!mResultMsg.equals("404")) {
74 sendResult(false, handler, context, "Server error: " + mResultMsg);
75 return false;
76 }
77
78 webdatPath = strippedPath + WEBDAV_PATH_1_2;
79 try {
80 complete_url = new URL(webdatPath);
81 } catch (MalformedURLException e) {
82 // should never happend
83 sendResult(false, handler, context, "URL error");
84 return false;
85 }
86
87 // version 1.2 success
88 if (tryGetWebdav(complete_url, username, password, handler, context)) {
89 sendResult(true, handler, context, complete_url.toString());
90 return true;
91 }
92
93 if (mResultMsg.equals("401")) {
94 sendResult(false, handler, context, "Invalid login or/and password");
95 return false;
96 }
97
98 if (mResultMsg.equals("404")) {
99 sendResult(false, handler, context, "Wrong path given");
100 return false;
101 }
102
103 sendResult(false, handler, context, "Server error: " + mResultMsg);
104 return false;
105 }
106
107 public static boolean tryGetWebdav(URL url, String username, String pwd,
108 Handler handler, Context context) {
109 DefaultHttpClient c = new DefaultHttpClient();
110 c.getCredentialsProvider().setCredentials(
111 new AuthScope(url.getHost(), (url.getPort() == -1)?80:url.getPort()),
112 new UsernamePasswordCredentials(username, pwd));
113
114 BasicHttpContext localcontext = new BasicHttpContext();
115 BasicScheme basicAuth = new BasicScheme();
116
117 localcontext.setAttribute("preemptive-auth", basicAuth);
118 HttpHost targetHost = new HttpHost(url.getHost(), (url.getPort() == -1)
119 ? 80
120 : url.getPort(), (url.getProtocol() == "https") ? "https" : "http");
121 HttpHead httpget = new HttpHead(url.toString());
122 HttpResponse response = null;
123 try {
124 response = c.execute(targetHost, httpget, localcontext);
125 } catch (ClientProtocolException e1) {
126 sendResult(false, handler, context, "Protocol error: "
127 + e1.getLocalizedMessage());
128 return false;
129 } catch (UnknownHostException e1) {
130 mResultMsg = "Unknowh host: " + e1.getLocalizedMessage();
131 return false;
132 } catch (IOException e1) {
133 mResultMsg = "Error: " + e1.getLocalizedMessage();
134 return false;
135 }
136 String status = response.getStatusLine().toString();
137 status = status.split(" ")[1];
138 Log.i("AuthUtils", "Status returned: " + status);
139 if (status.equals("200")) {
140 return true;
141 } else if (status.equals("404")) {
142 mResultMsg = "404";
143 return false;
144 } else if (status.equals("401")) {
145 mResultMsg = "401";
146 return false;
147 }
148 mResultMsg = status;
149 return false;
150 }
151
152 public static Thread performOnBackgroundThread(final Runnable r) {
153 final Thread t = new Thread() {
154 @Override
155 public void run() {
156 try {
157 r.run();
158 } finally {}
159 }
160 };
161 t.start();
162 return t;
163 }
164
165 public static void sendResult(final Boolean result,
166 final Handler handler,
167 final Context context,
168 final String message) {
169 if (handler == null || context == null) {
170 return;
171 }
172 handler.post(new Runnable() {
173 public void run() {
174 ((AuthenticatorActivity) context).onAuthenticationResult(result, message);
175 }
176 });
177 }
178
179 public static Thread attemptAuth(final URL url, final String username,
180 final String password, final Handler handler,
181 final Context context) {
182 final Runnable r = new Runnable() {
183
184 public void run() {
185 authenticate(url, username, password, handler, context);
186 }
187 };
188 return performOnBackgroundThread(r);
189 }
190 }