1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.authenticator
;
21 import java
.io
.IOException
;
22 import java
.net
.MalformedURLException
;
24 import java
.net
.UnknownHostException
;
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
;
36 import android
.content
.Context
;
37 import android
.os
.Handler
;
38 import android
.util
.Log
;
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";
44 private static String mResultMsg
= "";
46 public static boolean authenticate(URL url
, String username
, String password
,
47 Handler handler
, Context context
) {
48 String strippedPath
= url
.toString().endsWith("/") ?
49 url
.toString().substring(0, url
.toString().length()-1) :
51 String webdatPath
= strippedPath
+ WEBDAV_PATH_2_0
;
52 URL complete_url
= null
;
54 complete_url
= new URL(webdatPath
);
55 } catch (MalformedURLException e
) {
56 // should never happend
57 sendResult(false
, handler
, context
, "URL error");
61 // version 2.0 success
62 if (tryGetWebdav(complete_url
, username
, password
, handler
, context
)) {
63 sendResult(true
, handler
, context
, complete_url
.toString());
67 if (mResultMsg
.equals("401")) {
68 sendResult(false
, handler
, context
, "Invalid login or/and password");
72 if (!mResultMsg
.equals("404")) {
73 sendResult(false
, handler
, context
, "Server error: " + mResultMsg
);
77 webdatPath
= strippedPath
+ WEBDAV_PATH_1_2
;
79 complete_url
= new URL(webdatPath
);
80 } catch (MalformedURLException e
) {
81 // should never happend
82 sendResult(false
, handler
, context
, "URL error");
86 // version 1.2 success
87 if (tryGetWebdav(complete_url
, username
, password
, handler
, context
)) {
88 sendResult(true
, handler
, context
, complete_url
.toString());
92 if (mResultMsg
.equals("401")) {
93 sendResult(false
, handler
, context
, "Invalid login or/and password");
97 if (mResultMsg
.equals("404")) {
98 sendResult(false
, handler
, context
, "Wrong path given");
102 sendResult(false
, handler
, context
, "Server error: " + mResultMsg
);
106 public static boolean tryGetWebdav(URL url
, String username
, String pwd
,
107 Handler handler
, Context context
) {
108 DefaultHttpClient c
= new DefaultHttpClient();
109 c
.getCredentialsProvider().setCredentials(
110 new AuthScope(url
.getHost(), (url
.getPort() == -1)?
80:url
.getPort()),
111 new UsernamePasswordCredentials(username
, pwd
));
113 BasicHttpContext localcontext
= new BasicHttpContext();
114 BasicScheme basicAuth
= new BasicScheme();
116 localcontext
.setAttribute("preemptive-auth", basicAuth
);
117 HttpHost targetHost
= new HttpHost(url
.getHost(), (url
.getPort() == -1)
119 : url
.getPort(), (url
.getProtocol() == "https") ?
"https" : "http");
120 HttpHead httpget
= new HttpHead(url
.toString());
121 HttpResponse response
= null
;
123 response
= c
.execute(targetHost
, httpget
, localcontext
);
124 } catch (ClientProtocolException e1
) {
125 sendResult(false
, handler
, context
, "Protocol error: "
126 + e1
.getLocalizedMessage());
128 } catch (UnknownHostException e1
) {
129 mResultMsg
= "Unknowh host: " + e1
.getLocalizedMessage();
131 } catch (IOException e1
) {
132 mResultMsg
= "Error: " + e1
.getLocalizedMessage();
135 String status
= response
.getStatusLine().toString();
136 status
= status
.split(" ")[1];
137 Log
.i("AuthUtils", "Status returned: " + status
);
138 if (status
.equals("200")) {
140 } else if (status
.equals("404")) {
143 } else if (status
.equals("401")) {
151 public static Thread
performOnBackgroundThread(final Runnable r
) {
152 final Thread t
= new Thread() {
164 public static void sendResult(final Boolean result
,
165 final Handler handler
,
166 final Context context
,
167 final String message
) {
168 if (handler
== null
|| context
== null
) {
171 handler
.post(new Runnable() {
173 ((AuthenticatorActivity
) context
).onAuthenticationResult(result
, message
);
178 public static Thread
attemptAuth(final URL url
, final String username
,
179 final String password
, final Handler handler
,
180 final Context context
) {
181 final Runnable r
= new Runnable() {
184 authenticate(url
, username
, password
, handler
, context
);
187 return performOnBackgroundThread(r
);