diff --git a/app/build.gradle b/app/build.gradle index 224935d9fbe96cc5770a55bab51645845e02cad7..df6b5d3f5dc1f7cac33a74bdc8e760ca26dfaad6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,6 +45,10 @@ android { versionName "${versionMajor}.${versionMinor}.${versionPatch}" setProperty("archivesBaseName", "eDrive-$versionName") testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + + manifestPlaceholders = [ + 'appAuthRedirectScheme': applicationId, + ] } signingConfigs { @@ -94,7 +98,7 @@ android { } dependencies { - implementation 'foundation.e:Nextcloud-Android-Library:1.0.6-release' + implementation 'foundation.e:Nextcloud-Android-Library:1.0.7-u2.17-release' implementation "commons-httpclient:commons-httpclient:3.1@jar" implementation fileTree(include: ['*.jar'], dir: 'libs') api 'androidx.annotation:annotation:1.6.0' diff --git a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java index 641b3c47a498ca4060d997c4fa454ce6e856bc7e..b4e0ad42f96473af51164c758bc480559ed9cb63 100644 --- a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java +++ b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java @@ -37,6 +37,7 @@ import foundation.e.drive.databinding.ActivityAccountsBinding; import foundation.e.drive.utils.AccountUtils; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; +import foundation.e.drive.utils.AccessTokenProvider; import foundation.e.drive.widgets.EDriveWidget; import timber.log.Timber; @@ -78,6 +79,7 @@ public class AccountsActivity extends AppCompatActivity { final String totalQuota = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); String name = accountManager.getUserData(account, ACCOUNT_DATA_NAME); + final String token = AccessTokenProvider.getToken(accountManager, account); // For some reason if we cant get name use email as name if (name == null || name.isEmpty()) { @@ -139,7 +141,7 @@ public class AccountsActivity extends AppCompatActivity { binding.upgrade.setVisibility(View.VISIBLE); binding.upgrade.setOnClickListener(v -> { final Intent upgradeIntent = buildIntent(Intent.ACTION_VIEW, - String.format(EDriveWidget.WEBPAGE, email, + String.format(EDriveWidget.WEBPAGE, email, token, dataForWeb(totalQuota))); startActivity(upgradeIntent); }); diff --git a/app/src/main/java/foundation/e/drive/utils/AccessTokenProvider.kt b/app/src/main/java/foundation/e/drive/utils/AccessTokenProvider.kt new file mode 100644 index 0000000000000000000000000000000000000000..eecb67e48f758a77c9d93d5f2c47e45e3eba9666 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/AccessTokenProvider.kt @@ -0,0 +1,52 @@ +/* + * 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 foundation.e.drive.utils + +import android.accounts.Account +import android.accounts.AccountManager +import com.owncloud.android.lib.common.accounts.AccountUtils +import net.openid.appauth.AuthState +import org.json.JSONException +import timber.log.Timber + +object AccessTokenProvider { + + private const val PLACEHOLDER_TOKEN = "placeholder" + + @JvmStatic + fun getToken(accountManager: AccountManager, account: Account?): String { + getAuthState(accountManager, account)?.let { authState -> + try { + return AuthState.jsonDeserialize(authState).accessToken ?: PLACEHOLDER_TOKEN + } catch (e: JSONException) { + Timber.e(e) + } + } + + return PLACEHOLDER_TOKEN + } + + private fun getAuthState(accountManager: AccountManager, account: Account?): String? { + var authState: String? = null + + if (account != null) { + authState = accountManager.getUserData(account, AccountUtils.Constants.KEY_AUTH_STATE) + } + + return if (authState.isNullOrBlank()) null else authState + } +} diff --git a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java index 8c01a51e6755bfec3b59ca08f6ca467a603b829a..a839ee119e10ded258f970951362f730d3c332bf 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -42,6 +42,7 @@ import foundation.e.drive.R; import foundation.e.drive.utils.AccountUtils; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; +import foundation.e.drive.utils.AccessTokenProvider; import timber.log.Timber; /** @@ -51,7 +52,7 @@ import timber.log.Timber; public class EDriveWidget extends AppWidgetProvider { public static final String WEBPAGE = - "https://murena.com/ecloud-subscriptions/?username=%s&token=placeholder¤t-quota=%s&from=wp"; + "https://murena.com/ecloud-subscriptions/?username=%s&token=%s¤t-quota=%s&from=wp"; private static final String ADD_ACCOUNT_WEBPAGE = "https://murena.io/signup/e-email-invite"; private static final String ACCOUNT_PROVIDER_EELO = "e.foundation.webdav.eelo"; @@ -211,6 +212,7 @@ public class EDriveWidget extends AppWidgetProvider { final String totalQuota = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); String name = accountManager.getUserData(account, ACCOUNT_DATA_NAME); + final String token = AccessTokenProvider.getToken(accountManager, account); if (email == null || email.trim().isEmpty()) { noAccountView(context); @@ -297,7 +299,7 @@ public class EDriveWidget extends AppWidgetProvider { views.setOnClickPendingIntent(R.id.settings, pendingIntentSettings); final PendingIntent pendingIntentUpgrade = PendingIntent.getActivity(context, 0, - buildIntent(Intent.ACTION_VIEW, String.format(WEBPAGE, email, + buildIntent(Intent.ACTION_VIEW, String.format(WEBPAGE, email, token, dataForWeb(totalQuota))), PendingIntent.FLAG_IMMUTABLE); views.setOnClickPendingIntent(R.id.upgrade, pendingIntentUpgrade); } diff --git a/app/src/test/java/foundation/e/drive/TestUtils.java b/app/src/test/java/foundation/e/drive/TestUtils.java index 3ff562f96459d58ae8ffaeddbdc0d468fe159e21..08a9bb5d52a3dd59833c1c5ff5a53af51f85ecae 100644 --- a/app/src/test/java/foundation/e/drive/TestUtils.java +++ b/app/src/test/java/foundation/e/drive/TestUtils.java @@ -30,6 +30,8 @@ import androidx.work.Configuration; import androidx.work.testing.SynchronousExecutor; import androidx.work.testing.WorkManagerTestInitHelper; +import okhttp3.CookieJar; + public abstract class TestUtils { public static final String TEST_LOCAL_ROOT_FOLDER_PATH = "/tmp/eDrive/test/"; //THis is where test file and folder for synchronisatio will be stored public static final String TEST_REMOTE_ROOT_FOLDER_PATH ="/eDrive-test/"; @@ -64,7 +66,7 @@ public abstract class TestUtils { public static NextcloudClient getNcClient(Context context) { final Uri serverUri = Uri.parse(TEST_SERVER_URI); - return new NextcloudClient(serverUri, TEST_ACCOUNT_NAME, TEST_ACCOUNT_PASSWORD, context); + return new NextcloudClient(serverUri, TEST_ACCOUNT_NAME, TEST_ACCOUNT_PASSWORD, context, CookieJar.NO_COOKIES); } /** diff --git a/build.gradle b/build.gradle index 766ad4a879b844c738a26f8f4b79d94c98b3a85f..453138514166d91b76b2a9161eed6e8f8715bab1 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ plugins { id 'com.android.application' version '7.1.3' apply false id 'com.android.library' version '7.1.3' apply false - id 'org.jetbrains.kotlin.android' version '1.8.20-RC' apply false + id 'org.jetbrains.kotlin.android' version '1.8.22' apply false id 'io.gitlab.arturbosch.detekt' version '1.23.1' }