diff --git a/library/build.gradle b/library/build.gradle index 8b0eb4a9c28d4ba1759c540f8a16d834039bb00a..b6e9667b6a696817e10746e9756f3cff78ebe489 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -47,7 +47,7 @@ configurations { def versionMajor = 1 def versionMinor = 0 -def versionPatch = 5 +def versionPatch = 6 def releasePatch = "release" def libName = "Nextcloud-Android-Library" def groupName = "foundation.e" diff --git a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java index 201db4a7b026945952f0691c94fedd366813cd35..760b4f047da0673530a671329da130cd47d6708e 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java +++ b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java @@ -82,7 +82,7 @@ public class OwnCloudClientFactory { Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); // TODO avoid calling to getUserData here - String userId = AccountUtils.getUserId(am, account); + String userId = AccountUtils.getUpdatedUserId(am, account); OwnCloudClient client = createOwnCloudClient(baseUri, appContext, true); client.setUserId(userId); @@ -106,7 +106,7 @@ public class OwnCloudClientFactory { Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); // TODO avoid calling to getUserData here - String userId = AccountUtils.getUserId(am, account); + String userId = AccountUtils.getUpdatedUserId(am, account); OwnCloudClient client = createOwnCloudClient(baseUri, appContext, true); client.setUserId(userId); @@ -212,7 +212,7 @@ public class OwnCloudClientFactory { Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); // TODO avoid calling to getUserData here - String userId = AccountUtils.getUserId(am, account); + String userId = AccountUtils.getUpdatedUserId(am, account); String username = AccountUtils.getUsernameForAccount(account); String password; try { diff --git a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManager.java b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManager.java index 1d07c40942714dfe0e47f56d91b37bec3ecd05ba..99a5c79c6884a3765d3f9b776f5ad7fb6a97b2f9 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManager.java +++ b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManager.java @@ -122,7 +122,7 @@ public class OwnCloudClientManager { Account savedAccount = account.getSavedAccount(); if (savedAccount != null) { - final String userId = AccountUtils.getUserId(accountManager, account.getSavedAccount()); + final String userId = AccountUtils.getUpdatedUserId(accountManager, account.getSavedAccount()); client.setUserId(userId); } @@ -213,7 +213,7 @@ public class OwnCloudClientManager { String userId; if (savedAccount != null) { - userId = AccountUtils.getUserId(accountManager, account.getSavedAccount()); + userId = AccountUtils.getUpdatedUserId(accountManager, account.getSavedAccount()); } else { userId = ""; } diff --git a/library/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java b/library/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java index dfb0efe03e388ee3d408482466dce43e0c083b07..cd95db68a12a005cd31f62aeac4e58e7e5b239f1 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java +++ b/library/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java @@ -55,9 +55,6 @@ public class AccountUtils { public static final String DAV_UPLOAD = "/remote.php/dav/uploads"; public static final String STATUS_PATH = "/status.php"; - private static final String AT_SIGN = "@"; - private static final String EELO_ACCOUNT_END_PART = "@e.email"; - /** * Extracts url server from the account * @@ -108,17 +105,11 @@ public class AccountUtils { } String username = account.name; - - if (!username.contains(AT_SIGN) || username.endsWith(EELO_ACCOUNT_END_PART)) { - return username; + if (username == null) { + return null; } - try { - username = account.name.substring(0, account.name.lastIndexOf('@')); - } catch (Exception e) { - Log_OC.e(TAG, "Couldn't get a username for the given account", e); - } - return username; + return username.trim(); } /** @@ -171,9 +162,9 @@ public class AccountUtils { try { password = accountManager.blockingGetAuthToken( - account, - AccountTypeUtils.getAuthTokenTypePass(account.type), - false + account, + AccountTypeUtils.getAuthTokenTypePass(account.type), + false ); } catch (AuthenticatorException | IOException | OperationCanceledException e) { Log_OC.w(TAG, "failed to retrieve authToken for account: " + account.name); @@ -323,14 +314,52 @@ public class AccountUtils { } @Nullable - public static String getUserId(@NonNull AccountManager accountManager, @NonNull Account account) { - String userId = accountManager.getUserData(account, AccountUtils.Constants.KEY_USER_ID); + public static String getUpdatedUserId(@NonNull AccountManager accountManager, @NonNull Account account) { + final String userId = accountManager.getUserData(account, Constants.KEY_USER_ID); + + if (userId != null && !userId.isBlank()) { + return userId.trim(); + } + + final String userName = account.name; + if (userName == null || userName.trim().isBlank()) { + return userName; + } + + final String host = getHostForAccount(accountManager, account); + final String newUserId = retrieveUserId(host, userName); + + accountManager.setUserData(account, Constants.KEY_USER_ID, newUserId); + return newUserId; + } + + static String retrieveUserId(@Nullable String host, @NonNull String userName) { + userName = userName.trim(); + + if (host == null) { + return userName; + } + + final String userNameEndPart = ("@" + host.trim()).toLowerCase(); + + if (!userName.toLowerCase().endsWith(userNameEndPart)) { + return userName; + } + + int lengthOfUserId = userName.length() - userNameEndPart.length(); // abc@ff.com - @ff.com = abc + return userName.substring(0, lengthOfUserId); + } + + @Nullable + private static String getHostForAccount(@NonNull AccountManager accountManager, @NonNull Account account) { + final String baseurl = accountManager.getUserData(account, Constants.KEY_OC_BASE_URL); - if (userId == null || userId.isBlank()) { - userId = account.name; + if (baseurl == null) { + return null; } - return userId; + final Uri baseUri = Uri.parse(baseurl); + return baseUri.getHost(); } diff --git a/library/src/test/java/com/owncloud/android/lib/common/accounts/AccountUtilsTest.java b/library/src/test/java/com/owncloud/android/lib/common/accounts/AccountUtilsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..57d16861d19cab787749283152c8c9fbeceabd25 --- /dev/null +++ b/library/src/test/java/com/owncloud/android/lib/common/accounts/AccountUtilsTest.java @@ -0,0 +1,59 @@ +/* + * Copyright MURENA SAS 2024 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.owncloud.android.lib.common.accounts; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class AccountUtilsTest { + + @Test + public void test_retrieveUserId_with_empty_input() { + var userName = ""; + var expected = ""; + var actual = AccountUtils.retrieveUserId(null, userName); + assertEquals(expected, actual); + } + + @Test + public void test_retrieveUserId_with_input_without_url_part() { + var userName = "username"; + var host = "murena.io"; + var expected = "username"; + var actual = AccountUtils.retrieveUserId(host, userName); + assertEquals(expected, actual); + } + + @Test + public void test_retrieveUserId_with_case_sensitivity() { + var userName = "User@E.EMAIL@MURENA.io"; + var host = "murena.io"; + var expected = "User@E.EMAIL"; + var actual = AccountUtils.retrieveUserId(host, userName); + assertEquals(expected, actual); + } + + @Test + public void test_retrieveUserId_with_leading_or_trailing_spaces() { + var userName = " user@domain.com "; + var host = " domain.com "; + var expected = "user"; + var actual = AccountUtils.retrieveUserId(host, userName); + assertEquals(expected, actual); + } +}