From 4e6983862ff75768553f40049db5dc218980717b Mon Sep 17 00:00:00 2001 From: Julian Raufelder Date: Wed, 28 Apr 2021 17:22:09 +0200 Subject: [PATCH] Implement AuthStrategy for S3 --- .../CloudContentRepositoryFactories.java | 3 ++ .../presenter/AuthenticateCloudPresenter.kt | 35 +++++++++++++++++++ .../presenter/AuthenticateCloudPresenter.kt | 35 +++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/data/src/foss/java/org/cryptomator/data/cloud/CloudContentRepositoryFactories.java b/data/src/foss/java/org/cryptomator/data/cloud/CloudContentRepositoryFactories.java index f2bb16fa..4511d337 100644 --- a/data/src/foss/java/org/cryptomator/data/cloud/CloudContentRepositoryFactories.java +++ b/data/src/foss/java/org/cryptomator/data/cloud/CloudContentRepositoryFactories.java @@ -5,6 +5,7 @@ import org.cryptomator.data.cloud.dropbox.DropboxCloudContentRepositoryFactory; import org.cryptomator.data.cloud.local.LocalStorageContentRepositoryFactory; import org.cryptomator.data.cloud.onedrive.OnedriveCloudContentRepositoryFactory; import org.cryptomator.data.cloud.pcloud.PCloudContentRepositoryFactory; +import org.cryptomator.data.cloud.s3.S3CloudContentRepositoryFactory; import org.cryptomator.data.cloud.webdav.WebDavCloudContentRepositoryFactory; import org.cryptomator.data.repository.CloudContentRepositoryFactory; import org.jetbrains.annotations.NotNull; @@ -25,6 +26,7 @@ public class CloudContentRepositoryFactories implements Iterable { + if (!authenticationStarted) { + startAuthentication(intent.cloud()) + Toast.makeText( + context(), + String.format(getString(R.string.error_authentication_failed), intent.cloud().username()), + Toast.LENGTH_LONG).show() + } + } + else -> { + Timber.tag("AuthicateCloudPrester").e(intent.error()) + failAuthentication(intent.cloud().name()) + } + } + } + + private fun startAuthentication(cloud: CloudModel) { + authenticationStarted = true + startIntent(Intents.s3AddOrChangeIntent().withS3Cloud(cloud as S3CloudModel)) + } + } + private inner class LocalStorageAuthStrategy : AuthStrategy { private var authenticationStarted = false diff --git a/presentation/src/notFoss/java/org/cryptomator/presentation/presenter/AuthenticateCloudPresenter.kt b/presentation/src/notFoss/java/org/cryptomator/presentation/presenter/AuthenticateCloudPresenter.kt index 8b228d10..db32d2b2 100644 --- a/presentation/src/notFoss/java/org/cryptomator/presentation/presenter/AuthenticateCloudPresenter.kt +++ b/presentation/src/notFoss/java/org/cryptomator/presentation/presenter/AuthenticateCloudPresenter.kt @@ -40,10 +40,12 @@ import org.cryptomator.presentation.R import org.cryptomator.presentation.exception.ExceptionHandlers import org.cryptomator.presentation.exception.PermissionNotGrantedException import org.cryptomator.presentation.intent.AuthenticateCloudIntent +import org.cryptomator.presentation.intent.Intents import org.cryptomator.presentation.model.CloudModel import org.cryptomator.presentation.model.CloudTypeModel import org.cryptomator.presentation.model.ProgressModel import org.cryptomator.presentation.model.ProgressStateModel +import org.cryptomator.presentation.model.S3CloudModel import org.cryptomator.presentation.model.WebDavCloudModel import org.cryptomator.presentation.model.mappers.CloudModelMapper import org.cryptomator.presentation.ui.activity.view.AuthenticateCloudView @@ -76,6 +78,7 @@ class AuthenticateCloudPresenter @Inject constructor( // OnedriveAuthStrategy(), // PCloudAuthStrategy(), // WebDAVAuthStrategy(), // + S3AuthStrategy(), // LocalStorageAuthStrategy() // ) @@ -448,6 +451,38 @@ class AuthenticateCloudPresenter @Inject constructor( // finish() } + private inner class S3AuthStrategy : AuthStrategy { + + private var authenticationStarted = false + + override fun supports(cloud: CloudModel): Boolean { + return cloud.cloudType() == CloudTypeModel.S3 + } + + override fun resumed(intent: AuthenticateCloudIntent) { + when { + ExceptionUtil.contains(intent.error(), WrongCredentialsException::class.java) -> { + if (!authenticationStarted) { + startAuthentication(intent.cloud()) + Toast.makeText( + context(), + String.format(getString(R.string.error_authentication_failed), intent.cloud().username()), + Toast.LENGTH_LONG).show() + } + } + else -> { + Timber.tag("AuthicateCloudPrester").e(intent.error()) + failAuthentication(intent.cloud().name()) + } + } + } + + private fun startAuthentication(cloud: CloudModel) { + authenticationStarted = true + startIntent(Intents.s3AddOrChangeIntent().withS3Cloud(cloud as S3CloudModel)) + } + } + private inner class LocalStorageAuthStrategy : AuthStrategy { private var authenticationStarted = false