new stuff for account authenticator
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / authenticator / AuthUtils.java
diff --git a/src/eu/alefzero/owncloud/authenticator/AuthUtils.java b/src/eu/alefzero/owncloud/authenticator/AuthUtils.java
deleted file mode 100644 (file)
index 8287e12..0000000
+++ /dev/null
@@ -1,254 +0,0 @@
-/* ownCloud Android client application\r
- *   Copyright (C) 2011  Bartek Przybylski\r
- *\r
- *   This program is free software: you can redistribute it and/or modify\r
- *   it under the terms of the GNU General Public License as published by\r
- *   the Free Software Foundation, either version 3 of the License, or\r
- *   (at your option) any later version.\r
- *\r
- *   This program is distributed in the hope that it will be useful,\r
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- *   GNU General Public License for more details.\r
- *\r
- *   You should have received a copy of the GNU General Public License\r
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
- *\r
- */\r
-\r
-package eu.alefzero.owncloud.authenticator;\r
-\r
-import java.io.IOException;\r
-import java.net.MalformedURLException;\r
-import java.net.URL;\r
-import java.net.UnknownHostException;\r
-import org.apache.http.conn.ClientConnectionManager;\r
-import org.apache.http.conn.scheme.Scheme;\r
-import org.apache.http.conn.scheme.SchemeRegistry;\r
-\r
-import org.apache.http.impl.client.DefaultHttpClient;\r
-\r
-import org.apache.commons.httpclient.auth.BasicScheme;\r
-import org.apache.http.HttpHost;\r
-import org.apache.http.HttpResponse;\r
-import org.apache.http.HttpVersion;\r
-import org.apache.http.auth.AuthScope;\r
-import org.apache.http.auth.UsernamePasswordCredentials;\r
-import org.apache.http.client.ClientProtocolException;\r
-import org.apache.http.client.methods.HttpHead;\r
-import org.apache.http.conn.params.ConnManagerPNames;\r
-import org.apache.http.conn.params.ConnPerRouteBean;\r
-import org.apache.http.conn.scheme.PlainSocketFactory;\r
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;\r
-import org.apache.http.params.BasicHttpParams;\r
-import org.apache.http.params.HttpParams;\r
-import org.apache.http.params.HttpProtocolParams;\r
-import org.apache.http.protocol.BasicHttpContext;\r
-\r
-import eu.alefzero.owncloud.ui.activity.AuthenticatorActivity;\r
-\r
-\r
-import android.accounts.Account;\r
-import android.accounts.AccountManager;\r
-import android.content.Context;\r
-import android.content.SharedPreferences;\r
-import android.os.Handler;\r
-import android.preference.PreferenceManager;\r
-import android.util.Log;\r
-\r
-public class AuthUtils {\r
-  public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";\r
-  public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";\r
-  public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";\r
-  \r
-  private static String mResultMsg = "";\r
-  \r
-  public static boolean authenticate(URL url, String username, String password,\r
-                                     Handler handler, Context context) {\r
-    String strippedPath = url.toString().endsWith("/") ?\r
-                          url.toString().substring(0, url.toString().length()-1) :\r
-                          url.toString();\r
-    String webdatPath = strippedPath + WEBDAV_PATH_2_0;\r
-    URL complete_url = null;\r
-    try {\r
-      complete_url = new URL(webdatPath);\r
-    } catch (MalformedURLException e) {\r
-      // should never happend\r
-      sendResult(false, handler, context, "URL error");\r
-      return false;\r
-    }\r
-    \r
-    // version 2.0 success\r
-    if (tryGetWebdav(complete_url, username, password, handler, context)) {\r
-      sendResult(true, handler, context, complete_url.toString());\r
-      return true;\r
-    }\r
-    \r
-    if (mResultMsg.equals("401")) {\r
-       sendResult(false, handler, context, "Invalid login or/and password");\r
-       return false;\r
-    }\r
-    \r
-    if (!mResultMsg.equals("404")) {\r
-      sendResult(false, handler, context, "Server error: " + mResultMsg);\r
-      return false;\r
-    }\r
-    \r
-    webdatPath = strippedPath + WEBDAV_PATH_1_2;\r
-    try {\r
-      complete_url = new URL(webdatPath);\r
-    } catch (MalformedURLException e) {\r
-      // should never happend\r
-      sendResult(false, handler, context, "URL error");\r
-      return false;\r
-    }\r
-    \r
-    // version 1.2 success\r
-    if (tryGetWebdav(complete_url, username, password, handler, context)) {\r
-      sendResult(true, handler, context, complete_url.toString());\r
-      return true;\r
-    }\r
-    \r
-    if (mResultMsg.equals("401")) {\r
-      sendResult(false, handler, context, "Invalid login or/and password");\r
-      return false;\r
-    }\r
-    \r
-    if (mResultMsg.equals("404")) {\r
-      sendResult(false, handler, context, "Wrong path given");\r
-      return false;\r
-    }\r
-    \r
-    sendResult(false, handler, context, "Server error: " + mResultMsg);\r
-    return false;\r
-  }\r
-  \r
-  public static boolean tryGetWebdav(URL url, String username, String pwd,\r
-                                     Handler handler, Context context) {\r
-    SchemeRegistry schemeRegistry = new SchemeRegistry();\r
- // http scheme\r
- schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));\r
- // https scheme\r
- schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));\r
-\r
- HttpParams params = new BasicHttpParams();\r
- params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);\r
- params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));\r
- params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);\r
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);\r
-\r
- ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);\r
-    \r
-    DefaultHttpClient c = new DefaultHttpClient(cm, params);\r
-    \r
-    c.getCredentialsProvider().setCredentials(\r
-        new AuthScope(url.getHost(), (url.getPort() == -1)?80:url.getPort()), \r
-        new UsernamePasswordCredentials(username, pwd));\r
-    \r
-    BasicHttpContext localcontext  = new BasicHttpContext();\r
-    BasicScheme basicAuth = new BasicScheme();\r
-\r
-    localcontext.setAttribute("preemptive-auth", basicAuth);\r
-    HttpHost targetHost = new HttpHost(url.getHost(), (url.getPort() == -1)\r
-        ? 80\r
-        : url.getPort(), (url.getProtocol().equals("https")) ? "https" : "http");\r
-    HttpHead httpget = new HttpHead(url.toString());\r
-    httpget.setHeader("Host", url.getHost());\r
-    HttpResponse response = null;\r
-    try {\r
-      response = c.execute(targetHost, httpget, localcontext);\r
-    } catch (ClientProtocolException e1) {\r
-      sendResult(false, handler, context, "Protocol error: "\r
-          + e1.getLocalizedMessage());\r
-      return false;\r
-    } catch (UnknownHostException e1) {\r
-      mResultMsg = "Unknowh host: " + e1.getLocalizedMessage();\r
-      return false;\r
-    } catch (IOException e1) {\r
-      mResultMsg = "Error: " + e1.getLocalizedMessage();\r
-      return false;\r
-    }\r
-    String status = response.getStatusLine().toString();\r
-\r
-    status = status.split(" ")[1];\r
-    Log.i("AuthUtils", "Status returned: " + status);\r
-    if (status.equals("200")) {\r
-      return true;\r
-    } else if (status.equals("404")) {\r
-      mResultMsg = "404";\r
-      return false;\r
-    } else if (status.equals("401")) {\r
-      mResultMsg = "401";\r
-      return false;\r
-    }\r
-    mResultMsg = status;\r
-    return false;\r
-  }\r
-  \r
-  public static Thread performOnBackgroundThread(final Runnable r) {\r
-    final Thread t = new Thread() {\r
-      @Override\r
-      public void run() {\r
-        try {\r
-          r.run();\r
-        } finally {}\r
-      }\r
-    };\r
-    t.start();\r
-    return t;\r
-  }\r
-  \r
-  public static void sendResult(final Boolean result,\r
-                                final Handler handler,\r
-                                final Context context,\r
-                                final String message) {\r
-    if (handler == null || context == null) {\r
-      return;\r
-    }\r
-    handler.post(new Runnable() {\r
-      public void run() {\r
-        ((AuthenticatorActivity) context).onAuthenticationResult(result, message); \r
-      }\r
-    });\r
-  }\r
-  \r
-  public static Thread attemptAuth(final URL url, final String username,\r
-                                   final String password, final Handler handler,\r
-                                   final Context context) {\r
-    final Runnable r = new Runnable() {\r
-      \r
-      public void run() {\r
-        authenticate(url, username, password, handler, context);\r
-      }\r
-    };\r
-    return performOnBackgroundThread(r);\r
-  }\r
-  \r
-  /**\r
-   * Can be used to get the currently selected ownCloud account in the preferences\r
-   * \r
-   * @param context The current appContext\r
-   * @return The current account or null, if there is none yet.\r
-   */\r
-  public static Account getCurrentOwnCloudAccount(Context context){\r
-         Account[] ocAccounts = AccountManager.get(context).getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);\r
-         Account defaultAccount = null;\r
-         \r
-         SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);\r
-         String accountName = appPreferences.getString("select_oc_account", null);\r
-         \r
-         if(accountName != null){\r
-                 for(Account account : ocAccounts){\r
-                         if(account.name.equals(accountName)){\r
-                                 defaultAccount = account;\r
-                                 break;\r
-                         }\r
-                 }\r
-         } else if (ocAccounts.length != 0) {\r
-           // we at least need to take first account as fallback\r
-           defaultAccount = ocAccounts[0];\r
-         }\r
-         \r
-       return defaultAccount;\r
-  }\r
-}\r