Skip to content

Commit

Permalink
Refresh stale authentication token during auto upload
Browse files Browse the repository at this point in the history
Fixes #495
  • Loading branch information
SailReal committed Jun 5, 2023
1 parent 1fd714f commit 0c7804c
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 2 deletions.
5 changes: 4 additions & 1 deletion presentation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<uses-permission
android:name="android.permission.GET_ACCOUNTS"
Expand Down Expand Up @@ -92,6 +92,9 @@
android:exported="false"
android:theme="@style/TransparentPopUp" />

<activity
android:name=".ui.activity.AutoUploadRefreshTokenActivity"
android:exported="false" />
<!-- Settings -->
<activity
android:name=".ui.activity.AutoUploadChooseVaultActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.cryptomator.presentation.ui.activity.AuthenticateCloudActivity;
import org.cryptomator.presentation.ui.activity.AuthenticatePCloudActivity;
import org.cryptomator.presentation.ui.activity.AutoUploadChooseVaultActivity;
import org.cryptomator.presentation.ui.activity.AutoUploadRefreshTokenActivity;
import org.cryptomator.presentation.ui.activity.BiometricAuthSettingsActivity;
import org.cryptomator.presentation.ui.activity.BrowseFilesActivity;
import org.cryptomator.presentation.ui.activity.ChooseCloudServiceActivity;
Expand Down Expand Up @@ -112,6 +113,8 @@ public interface ActivityComponent {

void inject(AutoUploadChooseVaultFragment autoUploadChooseVaultFragment);

void inject(AutoUploadRefreshTokenActivity autoUploadRefreshTokenActivity);

void inject(LicenseCheckActivity licenseCheckActivity);

void inject(UnlockVaultActivity unlockVaultActivity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.cryptomator.presentation.presenter

import org.cryptomator.domain.Cloud
import org.cryptomator.domain.di.PerView
import org.cryptomator.domain.exception.authentication.AuthenticationException
import org.cryptomator.generator.Callback
import org.cryptomator.presentation.CryptomatorApp
import org.cryptomator.presentation.exception.ExceptionHandlers
import org.cryptomator.presentation.ui.activity.view.AutoUploadRefreshTokenView
import org.cryptomator.presentation.workflow.ActivityResult
import org.cryptomator.presentation.workflow.AuthenticationExceptionHandler
import javax.inject.Inject

@PerView
class AutoUploadRefreshTokenPresenter @Inject constructor(
exceptionHandlers: ExceptionHandlers, //
private val authenticationExceptionHandler: AuthenticationExceptionHandler, //
) : Presenter<AutoUploadRefreshTokenView>(exceptionHandlers) {

fun refreshCloudToken(authenticationException: AuthenticationException) {
authenticationExceptionHandler.handleAuthenticationException( //
this@AutoUploadRefreshTokenPresenter, //
authenticationException, //
ActivityResultCallbacks.onAutoUploadCloudAuthenticated(authenticationException.cloud)
)
}

@Callback(dispatchResultOkOnly = false)
fun onAutoUploadCloudAuthenticated(result: ActivityResult, cloud: Cloud) {
if (result.isResultOk) {
val cryptomatorApp = activity().application as CryptomatorApp
cryptomatorApp.startAutoUpload(cloud)
}
finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import android.content.Intent.ACTION_MAIN
import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import androidx.core.app.NotificationCompat
import org.cryptomator.domain.exception.authentication.AuthenticationException
import org.cryptomator.presentation.R
import org.cryptomator.presentation.service.AutoUploadService.cancelAutoUploadIntent
import org.cryptomator.presentation.ui.activity.AutoUploadRefreshTokenActivity
import org.cryptomator.presentation.ui.activity.AutoUploadRefreshTokenActivity.Companion.AUTHENTICATION_EXCEPTION_ARG
import org.cryptomator.presentation.ui.activity.VaultListActivity
import org.cryptomator.presentation.util.ResourceHelper.Companion.getColor
import org.cryptomator.presentation.util.ResourceHelper.Companion.getString
Expand Down Expand Up @@ -107,6 +110,23 @@ class AutoUploadNotification(private val context: Context, private val amountOfP
showErrorWithMessage(context.getString(R.string.notification_auto_upload_permission_not_granted))
}

fun showWrongCredentialNotification(authenticationException: AuthenticationException) {
val startTheActivity = Intent(context, AutoUploadRefreshTokenActivity::class.java)
startTheActivity.action = ACTION_MAIN
startTheActivity.flags = FLAG_ACTIVITY_CLEAR_TASK or FLAG_ACTIVITY_NEW_TASK
startTheActivity.putExtra(AUTHENTICATION_EXCEPTION_ARG, authenticationException)
val intent = PendingIntent.getActivity(context, 0, startTheActivity, FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE)
builder
.setContentIntent(intent)
.setContentTitle(context.getString(R.string.notification_auto_upload_failed_title))
.setContentText(context.getString(R.string.notification_auto_upload_failed_due_to_authentication_problem))
.setProgress(0, 0, false)
.setAutoCancel(true)
.setOngoing(false)
.clearActions()
show()
}

private fun showErrorWithMessage(message: String) {
builder
.setContentIntent(startTheActivity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.cryptomator.domain.exception.FileRemovedDuringUploadException;
import org.cryptomator.domain.exception.MissingCryptorException;
import org.cryptomator.domain.exception.NoSuchCloudFileException;
import org.cryptomator.domain.exception.authentication.AuthenticationException;
import org.cryptomator.domain.exception.authentication.WrongCredentialsException;
import org.cryptomator.domain.repository.CloudContentRepository;
import org.cryptomator.domain.usecases.ProgressAware;
import org.cryptomator.domain.usecases.cloud.CancelAwareDataSource;
Expand Down Expand Up @@ -107,7 +109,7 @@ private void startBackgroundImageUpload(Cloud cloud) {
}

upload(progress -> updateNotification(progress.asPercentage()));
} catch (FatalBackendException | BackendException | MissingCryptorException e) {
} catch (FatalBackendException | BackendException | MissingCryptorException | AuthenticationException e) {
if (e instanceof NoSuchCloudFileException) {
notification.showFolderMissing();
} else if (e instanceof MissingCryptorException) {
Expand All @@ -116,6 +118,8 @@ private void startBackgroundImageUpload(Cloud cloud) {
Timber.tag("AutoUploadService").i("Upload canceled by user");
} else if (wrappedStoragePermissionException(e)) {
notification.showPermissionNotGrantedNotification();
} else if (e instanceof AuthenticationException) {
notification.showWrongCredentialNotification((WrongCredentialsException) e);
} else {
notification.showGeneralErrorDuringUpload();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.cryptomator.presentation.ui.activity

import android.os.Bundle
import org.cryptomator.domain.exception.authentication.AuthenticationException
import org.cryptomator.generator.Activity
import org.cryptomator.presentation.R
import org.cryptomator.presentation.presenter.AutoUploadRefreshTokenPresenter
import org.cryptomator.presentation.ui.activity.view.AutoUploadRefreshTokenView
import javax.inject.Inject
import timber.log.Timber

@Activity(layout = R.layout.activity_empty)
class AutoUploadRefreshTokenActivity : BaseActivity(), AutoUploadRefreshTokenView {

@Inject
lateinit var presenter: AutoUploadRefreshTokenPresenter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val cloud = intent.getSerializableExtra(AUTHENTICATION_EXCEPTION_ARG) as? AuthenticationException
cloud?.let {
presenter.refreshCloudToken(it)
} ?: run {
Timber.tag("AutoUploadRefreshTokenActivity").e("WrongCredentialsException not provided")
finish()
}
}

companion object {
const val AUTHENTICATION_EXCEPTION_ARG = "authenticationException"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.cryptomator.presentation.ui.activity.view

interface AutoUploadRefreshTokenView : View {

}
1 change: 1 addition & 0 deletions presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@
<string name="notification_auto_upload_failed_due_to_vault_locked">Vault locked during upload, please reopen vault to continue</string>
<string name="notification_auto_upload_failed_due_to_vault_not_found">Vault specified for auto upload doesn\'t exist anymore.</string>
<string name="notification_auto_upload_permission_not_granted" translatable="false">@string/permission_snackbar_auth_auto_upload</string>
<string name="notification_auto_upload_failed_due_to_authentication_problem">Tap to refresh authentication.</string>

<string name="notification_cancel_open_writable_file" translatable="false">@string/dialog_button_cancel</string>
<string name="notification_open_writable_file_title">Open writable file</string>
Expand Down

0 comments on commit 0c7804c

Please sign in to comment.