e42d1bf7458bb622f5ad82ca4195ac73913e6b23
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / accounts / AccountUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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 com.owncloud.android.oc_framework.accounts;
20
21 import com.owncloud.android.oc_framework.utils.OwnCloudVersion;
22
23 import android.accounts.Account;
24 import android.accounts.AccountManager;
25 import android.accounts.AccountsException;
26 import android.content.Context;
27 import android.net.Uri;
28
29 public class AccountUtils {
30 public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
31 public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
32 public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
33 private static final String ODAV_PATH = "/remote.php/odav";
34 private static final String SAML_SSO_PATH = "/remote.php/webdav";
35 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
36 public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";
37 public static final String STATUS_PATH = "/status.php";
38
39 // Key for UserName in Saml Cookie
40 private static final String KEY_OC_USERNAME_EQUALS = "oc_username=";
41
42 /**
43 *
44 * @param version version of owncloud
45 * @return webdav path for given OC version, null if OC version unknown
46 */
47 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth, boolean supportsSamlSso) {
48 if (version != null) {
49 if (supportsOAuth) {
50 return ODAV_PATH;
51 }
52 if (supportsSamlSso) {
53 return SAML_SSO_PATH;
54 }
55 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
56 return WEBDAV_PATH_4_0;
57 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
58 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
59 return WEBDAV_PATH_2_0;
60 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
61 return WEBDAV_PATH_1_2;
62 }
63 return null;
64 }
65
66 // /**
67 // * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
68 // * according to its version and the authorization method used.
69 // *
70 // * @param version Version of ownCloud server.
71 // * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
72 // * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
73 // */
74 // public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
75 // if (version != null) {
76 // if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) {
77 // return ODAV_PATH;
78 // }
79 // if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) {
80 // return SAML_SSO_PATH;
81 // }
82 // if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
83 // return WEBDAV_PATH_4_0;
84 // if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
85 // || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
86 // return WEBDAV_PATH_2_0;
87 // if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
88 // return WEBDAV_PATH_1_2;
89 // }
90 // return null;
91 // }
92
93 /**
94 * Constructs full url to host and webdav resource basing on host version
95 * @param context
96 * @param account
97 * @return url or null on failure
98 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
99 */
100 public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {
101 AccountManager ama = AccountManager.get(context);
102 String baseurl = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_BASE_URL);
103 String strver = ama.getUserData(account, OwnCloudAccount.Constants.KEY_OC_VERSION);
104 boolean supportsOAuth = (ama.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null);
105 boolean supportsSamlSso = (ama.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null);
106 OwnCloudVersion ver = new OwnCloudVersion(strver);
107 String webdavpath = getWebdavPath(ver, supportsOAuth, supportsSamlSso);
108
109 if (baseurl == null || webdavpath == null)
110 throw new AccountNotFoundException(account, "Account not found", null);
111
112 return baseurl + webdavpath;
113 }
114
115
116 public static class AccountNotFoundException extends AccountsException {
117
118 /** Generated - should be refreshed every time the class changes!! */
119 private static final long serialVersionUID = -9013287181793186830L;
120
121 private Account mFailedAccount;
122
123 public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {
124 super(message, cause);
125 mFailedAccount = failedAccount;
126 }
127
128 public Account getFailedAccount() {
129 return mFailedAccount;
130 }
131 }
132
133 /**
134 * Get the UserName for the SamlSso cookie
135 * @param authToken
136 * @return userName
137 */
138 public static String getUserNameForSamlSso(String authToken) {
139 if (authToken != null) {
140 String [] cookies = authToken.split(";");
141 for (int i=0; i<cookies.length; i++) {
142 if (cookies[i].startsWith(KEY_OC_USERNAME_EQUALS )) {
143 String value = Uri.decode(cookies[i].substring(KEY_OC_USERNAME_EQUALS.length()));
144 return value;
145 }
146 }
147 }
148 return "";
149 }
150
151 }