1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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 com
.owncloud
.android
.oc_framework
.accounts
;
21 import com
.owncloud
.android
.oc_framework
.utils
.OwnCloudVersion
;
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
;
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";
39 // Key for UserName in Saml Cookie
40 private static final String KEY_OC_USERNAME_EQUALS
= "oc_username=";
44 * @param version version of owncloud
45 * @return webdav path for given OC version, null if OC version unknown
47 public static String
getWebdavPath(OwnCloudVersion version
, boolean supportsOAuth
, boolean supportsSamlSso
) {
48 if (version
!= null
) {
52 if (supportsSamlSso
) {
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
;
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.
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.
74 // public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
75 // if (version != null) {
76 // if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) {
79 // if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) {
80 // return SAML_SSO_PATH;
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;
94 * Constructs full url to host and webdav resource basing on host version
97 * @return url or null on failure
98 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
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
);
109 if (baseurl
== null
|| webdavpath
== null
)
110 throw new AccountNotFoundException(account
, "Account not found", null
);
112 return baseurl
+ webdavpath
;
116 public static class AccountNotFoundException
extends AccountsException
{
118 /** Generated - should be refreshed every time the class changes!! */
119 private static final long serialVersionUID
= -9013287181793186830L;
121 private Account mFailedAccount
;
123 public AccountNotFoundException(Account failedAccount
, String message
, Throwable cause
) {
124 super(message
, cause
);
125 mFailedAccount
= failedAccount
;
128 public Account
getFailedAccount() {
129 return mFailedAccount
;
134 * Get the UserName for the SamlSso cookie
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()));