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";
43 public static final String CARDDAV_PATH_2_0
= "/apps/contacts/carddav.php";
45 private static String mResultMsg
= "";
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) :
52 String webdatPath
= strippedPath
+ WEBDAV_PATH_2_0
;
53 URL complete_url
= null
;
55 complete_url
= new URL(webdatPath
);
56 } catch (MalformedURLException e
) {
57 // should never happend
58 sendResult(false
, handler
, context
, "URL error");
62 // version 2.0 success
63 if (tryGetWebdav(complete_url
, username
, password
, handler
, context
)) {
64 sendResult(true
, handler
, context
, complete_url
.toString());
68 if (mResultMsg
.equals("401")) {
69 sendResult(false
, handler
, context
, "Invalid login or/and password");
73 if (!mResultMsg
.equals("404")) {
74 sendResult(false
, handler
, context
, "Server error: " + mResultMsg
);
78 webdatPath
= strippedPath
+ WEBDAV_PATH_1_2
;
80 complete_url
= new URL(webdatPath
);
81 } catch (MalformedURLException e
) {
82 // should never happend
83 sendResult(false
, handler
, context
, "URL error");
87 // version 1.2 success
88 if (tryGetWebdav(complete_url
, username
, password
, handler
, context
)) {
89 sendResult(true
, handler
, context
, complete_url
.toString());
93 if (mResultMsg
.equals("401")) {
94 sendResult(false
, handler
, context
, "Invalid login or/and password");
98 if (mResultMsg
.equals("404")) {
99 sendResult(false
, handler
, context
, "Wrong path given");
103 sendResult(false
, handler
, context
, "Server error: " + mResultMsg
);
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
));
114 BasicHttpContext localcontext
= new BasicHttpContext();
115 BasicScheme basicAuth
= new BasicScheme();
117 localcontext
.setAttribute("preemptive-auth", basicAuth
);
118 HttpHost targetHost
= new HttpHost(url
.getHost(), (url
.getPort() == -1)
120 : url
.getPort(), (url
.getProtocol() == "https") ?
"https" : "http");
121 HttpHead httpget
= new HttpHead(url
.toString());
122 HttpResponse response
= null
;
124 response
= c
.execute(targetHost
, httpget
, localcontext
);
125 } catch (ClientProtocolException e1
) {
126 sendResult(false
, handler
, context
, "Protocol error: "
127 + e1
.getLocalizedMessage());
129 } catch (UnknownHostException e1
) {
130 mResultMsg
= "Unknowh host: " + e1
.getLocalizedMessage();
132 } catch (IOException e1
) {
133 mResultMsg
= "Error: " + e1
.getLocalizedMessage();
136 String status
= response
.getStatusLine().toString();
137 status
= status
.split(" ")[1];
138 Log
.i("AuthUtils", "Status returned: " + status
);
139 if (status
.equals("200")) {
141 } else if (status
.equals("404")) {
144 } else if (status
.equals("401")) {
152 public static Thread
performOnBackgroundThread(final Runnable r
) {
153 final Thread t
= new Thread() {
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
) {
172 handler
.post(new Runnable() {
174 ((AuthenticatorActivity
) context
).onAuthenticationResult(result
, message
);
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() {
185 authenticate(url
, username
, password
, handler
, context
);
188 return performOnBackgroundThread(r
);