#316 fix OAuth login in pCloud using Google
By switching from WebView to real browser
This commit is contained in:
parent
c4496ca148
commit
46aec2022a
@ -122,7 +122,6 @@ dependencies {
|
||||
implementation project(':util')
|
||||
implementation project(':domain')
|
||||
implementation project(':data')
|
||||
implementation project(':pcloud-sdk-android')
|
||||
|
||||
coreLibraryDesugaring dependencies.coreDesugaring
|
||||
|
||||
|
@ -265,76 +265,58 @@ class AuthenticateCloudPresenter @Inject constructor( //
|
||||
}
|
||||
|
||||
override fun resumed(intent: AuthenticateCloudIntent) {
|
||||
when {
|
||||
ExceptionUtil.contains(intent.error(), WrongCredentialsException::class.java) -> {
|
||||
if (!authenticationStarted) {
|
||||
startAuthentication()
|
||||
Toast.makeText(
|
||||
context(),
|
||||
String.format(getString(R.string.error_authentication_failed_re_authenticate), intent.cloud().username()),
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
Timber.tag("AuthicateCloudPrester").e(intent.error())
|
||||
failAuthentication(intent.cloud().name())
|
||||
}
|
||||
if (authenticationStarted) {
|
||||
finish()
|
||||
} else {
|
||||
startAuthentication(intent.cloud())
|
||||
Toast.makeText(
|
||||
context(),
|
||||
String.format(getString(R.string.error_authentication_failed_re_authenticate), intent.cloud().username()),
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun startAuthentication() {
|
||||
private fun startAuthentication(cloud: CloudModel) {
|
||||
authenticationStarted = true
|
||||
val authIntent: Intent = AuthorizationActivity.createIntent(
|
||||
context(),
|
||||
AuthorizationRequest.create()
|
||||
.setType(AuthorizationRequest.Type.TOKEN)
|
||||
.setClientId(BuildConfig.PCLOUD_CLIENT_ID)
|
||||
.setForceAccessApproval(true)
|
||||
.addPermission("manageshares")
|
||||
.build()
|
||||
)
|
||||
showProgress(ProgressModel(ProgressStateModel.AUTHENTICATION))
|
||||
view?.skipTransition()
|
||||
requestActivityResult(
|
||||
ActivityResultCallbacks.pCloudReAuthenticationFinished(), //
|
||||
authIntent
|
||||
ActivityResultCallbacks.pCloudReAuthenticationFinished(cloud), //
|
||||
Intents.cloudConnectionListIntent() //
|
||||
.withCloudType(CloudTypeModel.PCLOUD) //
|
||||
.withDialogTitle(context().getString(R.string.screen_update_pcloud_connections_title)) //
|
||||
.withFinishOnCloudItemClick(false) //
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Callback
|
||||
fun pCloudReAuthenticationFinished(activityResult: ActivityResult) {
|
||||
val authData: AuthorizationData = AuthorizationActivity.getResult(activityResult.intent())
|
||||
val result: AuthorizationResult = authData.result
|
||||
fun pCloudReAuthenticationFinished(activityResult: ActivityResult, cloud: CloudModel) {
|
||||
val code = activityResult.intent().extras?.getString(CloudConnectionListPresenter.PCLOUD_OAUTH_AUTH_CODE, "")
|
||||
val hostname = activityResult.intent().extras?.getString(CloudConnectionListPresenter.PCLOUD_HOSTNAME, "")
|
||||
|
||||
when (result) {
|
||||
AuthorizationResult.ACCESS_GRANTED -> {
|
||||
val accessToken: String = CredentialCryptor //
|
||||
.getInstance(context()) //
|
||||
.encrypt(authData.token)
|
||||
val pCloudSkeleton: PCloud = PCloud.aPCloud() //
|
||||
.withAccessToken(accessToken)
|
||||
.withUrl(authData.apiHost)
|
||||
.build();
|
||||
getUsernameUseCase //
|
||||
.withCloud(pCloudSkeleton) //
|
||||
.run(object : DefaultResultHandler<String>() {
|
||||
override fun onSuccess(username: String?) {
|
||||
prepareForSavingPCloud(PCloud.aCopyOf(pCloudSkeleton).withUsername(username).build())
|
||||
}
|
||||
})
|
||||
}
|
||||
AuthorizationResult.ACCESS_DENIED -> {
|
||||
Timber.tag("CloudConnListPresenter").e("Account access denied")
|
||||
view?.showMessage(String.format(getString(R.string.screen_authenticate_auth_authentication_failed), getString(R.string.cloud_names_pcloud)))
|
||||
}
|
||||
AuthorizationResult.AUTH_ERROR -> {
|
||||
Timber.tag("CloudConnListPresenter").e("""Account access grant error: ${authData.errorMessage}""".trimIndent())
|
||||
view?.showMessage(String.format(getString(R.string.screen_authenticate_auth_authentication_failed), getString(R.string.cloud_names_pcloud)))
|
||||
}
|
||||
AuthorizationResult.CANCELLED -> {
|
||||
Timber.tag("CloudConnListPresenter").i("Account access grant cancelled")
|
||||
view?.showMessage(String.format(getString(R.string.screen_authenticate_auth_authentication_failed), getString(R.string.cloud_names_pcloud)))
|
||||
}
|
||||
if (!code.isNullOrEmpty() && !hostname.isNullOrEmpty()) {
|
||||
Timber.tag("CloudConnectionListPresenter").i("PCloud OAuth code successfully retrieved")
|
||||
|
||||
val accessToken = CredentialCryptor //
|
||||
.getInstance(this.context()) //
|
||||
.encrypt(code)
|
||||
val pCloudSkeleton = PCloud.aPCloud() //
|
||||
.withAccessToken(accessToken)
|
||||
.withUrl(hostname)
|
||||
.build();
|
||||
getUsernameUseCase //
|
||||
.withCloud(pCloudSkeleton) //
|
||||
.run(object : DefaultResultHandler<String>() {
|
||||
override fun onSuccess(username: String) {
|
||||
Timber.tag("CloudConnectionListPresenter").i("PCloud Authentication successfully")
|
||||
prepareForSavingPCloud(PCloud.aCopyOf(pCloudSkeleton).withUsername(username).build())
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Timber.tag("CloudConnectionListPresenter").i("PCloud Authentication not successful")
|
||||
failAuthentication(cloud.name())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,6 +116,24 @@
|
||||
android:name=".ui.activity.AuthenticateCloudActivity"
|
||||
android:theme="@style/Theme.Transparent" />
|
||||
|
||||
<activity
|
||||
android:name=".ui.activity.AuthenticatePCloudActivity"
|
||||
android:configChanges="orientation|keyboard"
|
||||
android:launchMode="singleTask">
|
||||
|
||||
<intent-filter>
|
||||
<data
|
||||
android:host="redirect"
|
||||
android:scheme="pcloudoauth" />
|
||||
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
|
||||
<category android:name="android.intent.category.BROWSABLE" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
</intent-filter>
|
||||
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".ui.activity.ImagePreviewActivity"
|
||||
android:theme="@style/FullscreenTheme" />
|
||||
|
@ -5,6 +5,7 @@ import android.app.Activity;
|
||||
import org.cryptomator.domain.di.PerView;
|
||||
import org.cryptomator.presentation.di.module.ActivityModule;
|
||||
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.BiometricAuthSettingsActivity;
|
||||
import org.cryptomator.presentation.ui.activity.BrowseFilesActivity;
|
||||
@ -109,6 +110,8 @@ public interface ActivityComponent {
|
||||
|
||||
void inject(AuthenticateCloudActivity authenticateCloudActivity);
|
||||
|
||||
void inject(AuthenticatePCloudActivity authenticatePCloudActivity);
|
||||
|
||||
void inject(ImagePreviewActivity imagePreviewActivity);
|
||||
|
||||
void inject(ImagePreviewFragment imagePreviewFragment);
|
||||
|
@ -0,0 +1,8 @@
|
||||
package org.cryptomator.presentation.intent;
|
||||
|
||||
import org.cryptomator.generator.Intent;
|
||||
import org.cryptomator.presentation.ui.activity.AuthenticatePCloudActivity;
|
||||
|
||||
@Intent(AuthenticatePCloudActivity.class)
|
||||
public interface AuthenticatePCloudIntent {
|
||||
}
|
@ -6,10 +6,6 @@ import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.widget.Toast
|
||||
import androidx.annotation.RequiresApi
|
||||
import com.pcloud.sdk.AuthorizationActivity
|
||||
import com.pcloud.sdk.AuthorizationData
|
||||
import com.pcloud.sdk.AuthorizationRequest
|
||||
import com.pcloud.sdk.AuthorizationResult
|
||||
import org.cryptomator.domain.Cloud
|
||||
import org.cryptomator.domain.LocalStorageCloud
|
||||
import org.cryptomator.domain.PCloud
|
||||
@ -22,7 +18,6 @@ import org.cryptomator.domain.usecases.cloud.RemoveCloudUseCase
|
||||
import org.cryptomator.domain.usecases.vault.DeleteVaultUseCase
|
||||
import org.cryptomator.domain.usecases.vault.GetVaultListUseCase
|
||||
import org.cryptomator.generator.Callback
|
||||
import org.cryptomator.presentation.BuildConfig
|
||||
import org.cryptomator.presentation.R
|
||||
import org.cryptomator.presentation.exception.ExceptionHandlers
|
||||
import org.cryptomator.presentation.intent.Intents
|
||||
@ -33,6 +28,7 @@ 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.CloudConnectionListView
|
||||
import org.cryptomator.presentation.ui.dialog.PCloudCredentialsUpdatedDialog
|
||||
import org.cryptomator.presentation.workflow.ActivityResult
|
||||
import org.cryptomator.util.crypto.CredentialCryptor
|
||||
import java.util.*
|
||||
@ -136,18 +132,9 @@ class CloudConnectionListPresenter @Inject constructor( //
|
||||
Intents.webDavAddOrChangeIntent()
|
||||
)
|
||||
CloudTypeModel.PCLOUD -> {
|
||||
val authIntent: Intent = AuthorizationActivity.createIntent(
|
||||
this.context(),
|
||||
AuthorizationRequest.create()
|
||||
.setType(AuthorizationRequest.Type.TOKEN)
|
||||
.setClientId(BuildConfig.PCLOUD_CLIENT_ID)
|
||||
.setForceAccessApproval(true)
|
||||
.addPermission("manageshares")
|
||||
.build()
|
||||
)
|
||||
requestActivityResult(
|
||||
ActivityResultCallbacks.pCloudAuthenticationFinished(), //
|
||||
authIntent
|
||||
Intents.authenticatePCloudIntent()
|
||||
)
|
||||
}
|
||||
CloudTypeModel.S3 -> requestActivityResult(
|
||||
@ -210,38 +197,28 @@ class CloudConnectionListPresenter @Inject constructor( //
|
||||
|
||||
@Callback
|
||||
fun pCloudAuthenticationFinished(activityResult: ActivityResult) {
|
||||
val authData: AuthorizationData = AuthorizationActivity.getResult(activityResult.intent())
|
||||
val result: AuthorizationResult = authData.result
|
||||
val code = activityResult.intent().extras?.getString(PCLOUD_OAUTH_AUTH_CODE, "")
|
||||
val hostname = activityResult.intent().extras?.getString(PCLOUD_HOSTNAME, "")
|
||||
|
||||
when (result) {
|
||||
AuthorizationResult.ACCESS_GRANTED -> {
|
||||
val accessToken: String = CredentialCryptor //
|
||||
.getInstance(this.context()) //
|
||||
.encrypt(authData.token)
|
||||
val pCloudSkeleton: PCloud = PCloud.aPCloud() //
|
||||
.withAccessToken(accessToken)
|
||||
.withUrl(authData.apiHost)
|
||||
.build();
|
||||
getUsernameUseCase //
|
||||
.withCloud(pCloudSkeleton) //
|
||||
.run(object : DefaultResultHandler<String>() {
|
||||
override fun onSuccess(username: String?) {
|
||||
prepareForSavingPCloud(PCloud.aCopyOf(pCloudSkeleton).withUsername(username).build())
|
||||
}
|
||||
})
|
||||
}
|
||||
AuthorizationResult.ACCESS_DENIED -> {
|
||||
Timber.tag("CloudConnListPresenter").e("Account access denied")
|
||||
view?.showMessage(String.format(getString(R.string.screen_authenticate_auth_authentication_failed), getString(R.string.cloud_names_pcloud)))
|
||||
}
|
||||
AuthorizationResult.AUTH_ERROR -> {
|
||||
Timber.tag("CloudConnListPresenter").e("""Account access grant error: ${authData.errorMessage}""".trimIndent())
|
||||
view?.showMessage(String.format(getString(R.string.screen_authenticate_auth_authentication_failed), getString(R.string.cloud_names_pcloud)))
|
||||
}
|
||||
AuthorizationResult.CANCELLED -> {
|
||||
Timber.tag("CloudConnListPresenter").i("Account access grant cancelled")
|
||||
view?.showMessage(String.format(getString(R.string.screen_authenticate_auth_authentication_failed), getString(R.string.cloud_names_pcloud)))
|
||||
}
|
||||
if (!code.isNullOrEmpty() && !hostname.isNullOrEmpty()) {
|
||||
Timber.tag("CloudConnectionListPresenter").i("PCloud OAuth code successfully retrieved")
|
||||
|
||||
val accessToken = CredentialCryptor //
|
||||
.getInstance(this.context()) //
|
||||
.encrypt(code)
|
||||
val pCloudSkeleton = PCloud.aPCloud() //
|
||||
.withAccessToken(accessToken)
|
||||
.withUrl(hostname)
|
||||
.build();
|
||||
getUsernameUseCase //
|
||||
.withCloud(pCloudSkeleton) //
|
||||
.run(object : DefaultResultHandler<String>() {
|
||||
override fun onSuccess(username: String) {
|
||||
prepareForSavingPCloud(PCloud.aCopyOf(pCloudSkeleton).withUsername(username).build())
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Timber.tag("CloudConnectionListPresenter").i("PCloud Authentication not successful")
|
||||
}
|
||||
}
|
||||
|
||||
@ -253,13 +230,13 @@ class CloudConnectionListPresenter @Inject constructor( //
|
||||
clouds.firstOrNull {
|
||||
(it as PCloud).username() == cloud.username()
|
||||
}?.let {
|
||||
it as PCloud
|
||||
saveCloud(
|
||||
PCloud.aCopyOf(it) //
|
||||
PCloud.aCopyOf(it as PCloud) //
|
||||
.withUrl(cloud.url())
|
||||
.withAccessToken(cloud.accessToken())
|
||||
.build()
|
||||
)
|
||||
view?.showDialog(PCloudCredentialsUpdatedDialog.newInstance(it.username()))
|
||||
} ?: saveCloud(cloud)
|
||||
}
|
||||
})
|
||||
@ -284,12 +261,11 @@ class CloudConnectionListPresenter @Inject constructor( //
|
||||
LocalStorageCloud.aLocalStorage() //
|
||||
.withRootUri(rootTreeUriOfLocalStorage.toString()) //
|
||||
.build()
|
||||
) //
|
||||
.run(object : DefaultResultHandler<Void?>() {
|
||||
override fun onSuccess(void: Void?) {
|
||||
loadCloudList()
|
||||
}
|
||||
})
|
||||
).run(object : DefaultResultHandler<Void?>() {
|
||||
override fun onSuccess(void: Void?) {
|
||||
loadCloudList()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
||||
@ -327,6 +303,9 @@ class CloudConnectionListPresenter @Inject constructor( //
|
||||
companion object {
|
||||
|
||||
const val SELECTED_CLOUD = "selectedCloudConnection"
|
||||
const val PCLOUD_OAUTH_AUTH_CODE = "pCloudOAuthCode"
|
||||
const val PCLOUD_HOSTNAME = "pCloudHostname"
|
||||
|
||||
}
|
||||
|
||||
init {
|
||||
|
@ -0,0 +1,66 @@
|
||||
package org.cryptomator.presentation.ui.activity
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.widget.Toast
|
||||
import org.cryptomator.generator.Activity
|
||||
import org.cryptomator.presentation.BuildConfig
|
||||
import org.cryptomator.presentation.R
|
||||
import org.cryptomator.presentation.presenter.CloudConnectionListPresenter
|
||||
import java.util.TreeMap
|
||||
import timber.log.Timber
|
||||
|
||||
@Activity
|
||||
class AuthenticatePCloudActivity : BaseActivity() {
|
||||
|
||||
override fun setupView() {
|
||||
val uri = Uri.parse("https://my.pcloud.com/oauth2/authorize")
|
||||
.buildUpon()
|
||||
.appendQueryParameter("response_type", "token")
|
||||
.appendQueryParameter("client_id", BuildConfig.PCLOUD_CLIENT_ID)
|
||||
.appendQueryParameter("redirect_uri", "pcloudoauth://redirect")
|
||||
.build()
|
||||
|
||||
startActivityForResult(Intent(Intent.ACTION_VIEW, uri), 25)
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
intent.data?.let {
|
||||
if(it.host == "redirect" && it.scheme == "pcloudoauth") {
|
||||
val parameters = parseUrlFragmentParameters(it)
|
||||
val accessToken = parameters["access_token"]
|
||||
val hostname = parameters["hostname"]
|
||||
if (accessToken != null && hostname != null) {
|
||||
val result = Intent()
|
||||
result.putExtra(CloudConnectionListPresenter.PCLOUD_OAUTH_AUTH_CODE, accessToken)
|
||||
result.putExtra(CloudConnectionListPresenter.PCLOUD_HOSTNAME, hostname)
|
||||
setResult(android.app.Activity.RESULT_OK, result)
|
||||
finish()
|
||||
} else {
|
||||
Toast.makeText(this, R.string.error_authentication_failed, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
} else {
|
||||
Timber.tag("AuthenticatePCloudActivity").e("Tried to call activity using a different redirect scheme")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseUrlFragmentParameters(url: Uri): Map<String, String> {
|
||||
url.fragment?.let {
|
||||
val parameters: MutableMap<String, String> = TreeMap()
|
||||
val keyPairs = it.split("&".toRegex()).toTypedArray()
|
||||
keyPairs.forEach { keyPair ->
|
||||
val delimiterIndex = keyPair.indexOf('=')
|
||||
parameters[keyPair.substring(0, delimiterIndex)] = keyPair.substring(delimiterIndex + 1, keyPair.length)
|
||||
}
|
||||
return parameters
|
||||
}
|
||||
return emptyMap()
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@ import org.cryptomator.presentation.presenter.CloudConnectionListPresenter
|
||||
import org.cryptomator.presentation.ui.activity.view.CloudConnectionListView
|
||||
import org.cryptomator.presentation.ui.bottomsheet.CloudConnectionSettingsBottomSheet
|
||||
import org.cryptomator.presentation.ui.dialog.DeleteCloudConnectionWithVaultsDialog
|
||||
import org.cryptomator.presentation.ui.dialog.PCloudCredentialsUpdatedDialog
|
||||
import org.cryptomator.presentation.ui.fragment.CloudConnectionListFragment
|
||||
import java.util.ArrayList
|
||||
import javax.inject.Inject
|
||||
@ -20,7 +21,8 @@ import kotlinx.android.synthetic.main.toolbar_layout.toolbar
|
||||
class CloudConnectionListActivity : BaseActivity(),
|
||||
CloudConnectionListView,
|
||||
CloudConnectionSettingsBottomSheet.Callback,
|
||||
DeleteCloudConnectionWithVaultsDialog.Callback {
|
||||
DeleteCloudConnectionWithVaultsDialog.Callback,
|
||||
PCloudCredentialsUpdatedDialog.Callback {
|
||||
|
||||
@Inject
|
||||
lateinit var presenter: CloudConnectionListPresenter
|
||||
@ -74,4 +76,8 @@ class CloudConnectionListActivity : BaseActivity(),
|
||||
override fun onDeleteCloudConnectionAndVaults(cloudModel: CloudModel, vaultsOfCloud: ArrayList<Vault>) {
|
||||
presenter.onDeleteCloudConnectionAndVaults(cloudModel, vaultsOfCloud)
|
||||
}
|
||||
|
||||
override fun onNotifyForPCloudCredentialsUpdateFinished() {
|
||||
// nothing to do here
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,64 @@
|
||||
package org.cryptomator.presentation.ui.dialog
|
||||
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import org.cryptomator.generator.Dialog
|
||||
import org.cryptomator.presentation.R
|
||||
import org.cryptomator.presentation.util.ResourceHelper
|
||||
import kotlinx.android.synthetic.main.dialog_pcloud_credentials_updated.tv_pcloud_credentials_updated
|
||||
|
||||
|
||||
@Dialog(R.layout.dialog_pcloud_credentials_updated)
|
||||
class PCloudCredentialsUpdatedDialog : BaseDialog<PCloudCredentialsUpdatedDialog.Callback>() {
|
||||
|
||||
interface Callback {
|
||||
|
||||
fun onNotifyForPCloudCredentialsUpdateFinished()
|
||||
}
|
||||
|
||||
val someActivityResultLauncher = registerForActivityResult(StartActivityForResult()) {
|
||||
dismiss()
|
||||
callback?.onNotifyForPCloudCredentialsUpdateFinished()
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
val dialog = dialog as AlertDialog?
|
||||
dialog?.let {
|
||||
tv_pcloud_credentials_updated.setOnClickListener {
|
||||
someActivityResultLauncher.launch(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.pcloud.com")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override fun setupDialog(builder: AlertDialog.Builder): android.app.Dialog {
|
||||
val username = requireArguments().getString(ARG_PCLOUD_USERNAME)
|
||||
builder //
|
||||
.setTitle(String.format(ResourceHelper.getString(R.string.dialog_pcloud_credentials_updated_title), username)) //
|
||||
.setNeutralButton(getString(R.string.dialog_unable_to_share_positive_button)) { _: DialogInterface, _: Int ->
|
||||
callback?.onNotifyForPCloudCredentialsUpdateFinished()
|
||||
}
|
||||
return builder.create()
|
||||
}
|
||||
|
||||
public override fun setupView() {
|
||||
// empty
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val ARG_PCLOUD_USERNAME = "USERNAME"
|
||||
fun newInstance(username: String): PCloudCredentialsUpdatedDialog {
|
||||
val args = Bundle()
|
||||
args.putString(ARG_PCLOUD_USERNAME, username)
|
||||
val fragment = PCloudCredentialsUpdatedDialog()
|
||||
fragment.arguments = args
|
||||
return fragment
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:padding="@dimen/activity_vertical_margin">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_pcloud_credentials_updated"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/dialog_pcloud_credentials_updated" />
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</androidx.core.widget.NestedScrollView>
|
@ -290,6 +290,7 @@
|
||||
|
||||
<!-- ## screen: authenticate cloud -->
|
||||
<string name="screen_authenticate_auth_authentication_failed">%1$s could not be authenticated.</string>
|
||||
<string name="screen_update_pcloud_connections_title">Update pCloud credentials</string>
|
||||
|
||||
<!-- ## screen: empty dir file info -->
|
||||
<string name="screen_empty_dir_file_info_title">\'%1$s\' unreachable</string>
|
||||
@ -469,6 +470,9 @@
|
||||
|
||||
<string name="dialog_no_more_images_to_display">No more images to display…</string>
|
||||
|
||||
<string name="dialog_pcloud_credentials_updated_title">Credentials of \'%1$s\' updated</string>
|
||||
<string name="dialog_pcloud_credentials_updated">If you intended to add a new pCloud account, click on this url <a href="https://www.pcloud.com">www.pcloud.com</a>, log out from the current account and click again on the \'+\' in this app to create a new cloud connection.</string>
|
||||
|
||||
<string name="permission_snackbar_auth_local_vault">Cryptomator needs storage access to use local vaults</string>
|
||||
<string name="permission_snackbar_auth_auto_upload">Cryptomator needs storage access to use auto photo upload</string>
|
||||
|
||||
|
@ -3,15 +3,10 @@ package org.cryptomator.presentation.presenter
|
||||
import android.Manifest
|
||||
import android.accounts.AccountManager
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Intent
|
||||
import android.widget.Toast
|
||||
import com.dropbox.core.android.Auth
|
||||
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
|
||||
import com.google.api.services.drive.DriveScopes
|
||||
import com.pcloud.sdk.AuthorizationActivity
|
||||
import com.pcloud.sdk.AuthorizationData
|
||||
import com.pcloud.sdk.AuthorizationRequest
|
||||
import com.pcloud.sdk.AuthorizationResult
|
||||
import org.cryptomator.data.cloud.onedrive.OnedriveClientFactory
|
||||
import org.cryptomator.data.cloud.onedrive.graph.ClientException
|
||||
import org.cryptomator.data.cloud.onedrive.graph.ICallback
|
||||
@ -62,6 +57,7 @@ import java.security.cert.X509Certificate
|
||||
import javax.inject.Inject
|
||||
import timber.log.Timber
|
||||
|
||||
|
||||
@PerView
|
||||
class AuthenticateCloudPresenter @Inject constructor( //
|
||||
exceptionHandlers: ExceptionHandlers, //
|
||||
@ -310,76 +306,58 @@ class AuthenticateCloudPresenter @Inject constructor( //
|
||||
}
|
||||
|
||||
override fun resumed(intent: AuthenticateCloudIntent) {
|
||||
when {
|
||||
ExceptionUtil.contains(intent.error(), WrongCredentialsException::class.java) -> {
|
||||
if (!authenticationStarted) {
|
||||
startAuthentication()
|
||||
Toast.makeText(
|
||||
context(),
|
||||
String.format(getString(R.string.error_authentication_failed_re_authenticate), intent.cloud().username()),
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
Timber.tag("AuthicateCloudPrester").e(intent.error())
|
||||
failAuthentication(intent.cloud().name())
|
||||
}
|
||||
if (authenticationStarted) {
|
||||
finish()
|
||||
} else {
|
||||
startAuthentication(intent.cloud())
|
||||
Toast.makeText(
|
||||
context(),
|
||||
String.format(getString(R.string.error_authentication_failed_re_authenticate), intent.cloud().username()),
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun startAuthentication() {
|
||||
private fun startAuthentication(cloud: CloudModel) {
|
||||
authenticationStarted = true
|
||||
val authIntent: Intent = AuthorizationActivity.createIntent(
|
||||
context(),
|
||||
AuthorizationRequest.create()
|
||||
.setType(AuthorizationRequest.Type.TOKEN)
|
||||
.setClientId(BuildConfig.PCLOUD_CLIENT_ID)
|
||||
.setForceAccessApproval(true)
|
||||
.addPermission("manageshares")
|
||||
.build()
|
||||
)
|
||||
showProgress(ProgressModel(ProgressStateModel.AUTHENTICATION))
|
||||
view?.skipTransition()
|
||||
requestActivityResult(
|
||||
ActivityResultCallbacks.pCloudReAuthenticationFinished(), //
|
||||
authIntent
|
||||
ActivityResultCallbacks.pCloudReAuthenticationFinished(cloud), //
|
||||
Intents.cloudConnectionListIntent() //
|
||||
.withCloudType(CloudTypeModel.PCLOUD) //
|
||||
.withDialogTitle(context().getString(R.string.screen_update_pcloud_connections_title)) //
|
||||
.withFinishOnCloudItemClick(false) //
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Callback
|
||||
fun pCloudReAuthenticationFinished(activityResult: ActivityResult) {
|
||||
val authData: AuthorizationData = AuthorizationActivity.getResult(activityResult.intent())
|
||||
val result: AuthorizationResult = authData.result
|
||||
fun pCloudReAuthenticationFinished(activityResult: ActivityResult, cloud: CloudModel) {
|
||||
val code = activityResult.intent().extras?.getString(CloudConnectionListPresenter.PCLOUD_OAUTH_AUTH_CODE, "")
|
||||
val hostname = activityResult.intent().extras?.getString(CloudConnectionListPresenter.PCLOUD_HOSTNAME, "")
|
||||
|
||||
when (result) {
|
||||
AuthorizationResult.ACCESS_GRANTED -> {
|
||||
val accessToken: String = CredentialCryptor //
|
||||
.getInstance(context()) //
|
||||
.encrypt(authData.token)
|
||||
val pCloudSkeleton: PCloud = PCloud.aPCloud() //
|
||||
.withAccessToken(accessToken)
|
||||
.withUrl(authData.apiHost)
|
||||
.build();
|
||||
getUsernameUseCase //
|
||||
.withCloud(pCloudSkeleton) //
|
||||
.run(object : DefaultResultHandler<String>() {
|
||||
override fun onSuccess(username: String?) {
|
||||
prepareForSavingPCloud(PCloud.aCopyOf(pCloudSkeleton).withUsername(username).build())
|
||||
}
|
||||
})
|
||||
}
|
||||
AuthorizationResult.ACCESS_DENIED -> {
|
||||
Timber.tag("CloudConnListPresenter").e("Account access denied")
|
||||
view?.showMessage(String.format(getString(R.string.screen_authenticate_auth_authentication_failed), getString(R.string.cloud_names_pcloud)))
|
||||
}
|
||||
AuthorizationResult.AUTH_ERROR -> {
|
||||
Timber.tag("CloudConnListPresenter").e("""Account access grant error: ${authData.errorMessage}""".trimIndent())
|
||||
view?.showMessage(String.format(getString(R.string.screen_authenticate_auth_authentication_failed), getString(R.string.cloud_names_pcloud)))
|
||||
}
|
||||
AuthorizationResult.CANCELLED -> {
|
||||
Timber.tag("CloudConnListPresenter").i("Account access grant cancelled")
|
||||
view?.showMessage(String.format(getString(R.string.screen_authenticate_auth_authentication_failed), getString(R.string.cloud_names_pcloud)))
|
||||
}
|
||||
if (!code.isNullOrEmpty() && !hostname.isNullOrEmpty()) {
|
||||
Timber.tag("CloudConnectionListPresenter").i("PCloud OAuth code successfully retrieved")
|
||||
|
||||
val accessToken = CredentialCryptor //
|
||||
.getInstance(this.context()) //
|
||||
.encrypt(code)
|
||||
val pCloudSkeleton = PCloud.aPCloud() //
|
||||
.withAccessToken(accessToken)
|
||||
.withUrl(hostname)
|
||||
.build();
|
||||
getUsernameUseCase //
|
||||
.withCloud(pCloudSkeleton) //
|
||||
.run(object : DefaultResultHandler<String>() {
|
||||
override fun onSuccess(username: String) {
|
||||
Timber.tag("CloudConnectionListPresenter").i("PCloud Authentication successfully")
|
||||
prepareForSavingPCloud(PCloud.aCopyOf(pCloudSkeleton).withUsername(username).build())
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Timber.tag("CloudConnectionListPresenter").i("PCloud Authentication not successful")
|
||||
failAuthentication(cloud.name())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
include ':generator', ':presentation', ':generator-api', ':domain', ':data', ':util', ':subsampling-image-view', ':msa-auth-for-android', ':pcloud-sdk-java-root', ':pcloud-sdk-java', ':pcloud-sdk-android'
|
||||
include ':generator', ':presentation', ':generator-api', ':domain', ':data', ':util', ':subsampling-image-view', ':msa-auth-for-android', ':pcloud-sdk-java-root', ':pcloud-sdk-java'
|
||||
project(':subsampling-image-view').projectDir = file(new File(rootDir, 'subsampling-scale-image-view/library'))
|
||||
project(':pcloud-sdk-java-root').projectDir = file(new File(rootDir, 'pcloud-sdk-java'))
|
||||
project(':pcloud-sdk-java').projectDir = file(new File(rootDir, 'pcloud-sdk-java/java-core'))
|
||||
project(':pcloud-sdk-android').projectDir = file(new File(rootDir, 'pcloud-sdk-java/android'))
|
||||
|
Loading…
x
Reference in New Issue
Block a user