From 1ca4b3b78a784cfd4c56ebc1db133b956475b318 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Wed, 25 Aug 2021 15:50:39 +0200 Subject: [PATCH 1/3] Add expired account error code For synapse instances which have activated and configured the email account validity module, an error code (ORG_MATRIX_EXPIRED_ACCOUNT) is triggered for any request authenticated by the user's access token which is expired. This change only add the error code in the matrix SDK but does not handle it for now in the client side. More documentation can be found in the dedicated Synapse plugin module repository: https://github.com/matrix-org/synapse-email-account-validity --- .../org/matrix/android/sdk/api/failure/Extensions.kt | 4 +++- .../matrix/android/sdk/api/failure/GlobalError.kt | 1 + .../matrix/android/sdk/api/failure/MatrixError.kt | 3 +++ .../sdk/internal/network/RetrofitExtensions.kt | 12 +++++++----- .../vector/app/core/platform/VectorBaseActivity.kt | 1 + 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt index 0ba61e5890e..3149a0218bc 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/Extensions.kt @@ -29,7 +29,9 @@ fun Throwable.is401() = fun Throwable.isTokenError() = this is Failure.ServerError - && (error.code == MatrixError.M_UNKNOWN_TOKEN || error.code == MatrixError.M_MISSING_TOKEN) + && (error.code == MatrixError.M_UNKNOWN_TOKEN + || error.code == MatrixError.M_MISSING_TOKEN + || error.code == MatrixError.ORG_MATRIX_EXPIRED_ACCOUNT) fun Throwable.shouldBeRetried(): Boolean { return this is Failure.NetworkConnection diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/GlobalError.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/GlobalError.kt index ef770ea1d2e..50c84da02b8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/GlobalError.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/GlobalError.kt @@ -23,4 +23,5 @@ sealed class GlobalError { data class InvalidToken(val softLogout: Boolean) : GlobalError() data class ConsentNotGivenError(val consentUri: String) : GlobalError() data class CertificateError(val fingerprint: Fingerprint) : GlobalError() + object ExpiredAccount : GlobalError() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixError.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixError.kt index 73b0fe0a7c0..f2da2b9ad24 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixError.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixError.kt @@ -189,5 +189,8 @@ data class MatrixError( // Possible value for "limit_type" const val LIMIT_TYPE_MAU = "monthly_active_user" + + /** The user account has expired. It has to be renewed by clicking on an email or by sending a renewal token. */ + const val ORG_MATRIX_EXPIRED_ACCOUNT = "ORG_MATRIX_EXPIRED_ACCOUNT" } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/RetrofitExtensions.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/RetrofitExtensions.kt index 71ba71b9150..8a031025279 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/RetrofitExtensions.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/RetrofitExtensions.kt @@ -19,13 +19,13 @@ package org.matrix.android.sdk.internal.network import com.squareup.moshi.JsonEncodingException +import kotlinx.coroutines.suspendCancellableCoroutine +import okhttp3.ResponseBody +import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.GlobalError import org.matrix.android.sdk.api.failure.MatrixError import org.matrix.android.sdk.internal.di.MoshiProvider -import kotlinx.coroutines.suspendCancellableCoroutine -import okhttp3.ResponseBody -import org.matrix.android.sdk.api.extensions.orFalse import retrofit2.HttpException import retrofit2.Response import timber.log.Timber @@ -86,16 +86,18 @@ private fun toFailure(errorBody: ResponseBody?, httpCode: Int, globalErrorReceiv val matrixError = matrixErrorAdapter.fromJson(errorBodyStr) if (matrixError != null) { + // Also send following errors to the globalErrorReceiver, for a global management when { matrixError.code == MatrixError.M_CONSENT_NOT_GIVEN && !matrixError.consentUri.isNullOrBlank() -> { - // Also send this error to the globalErrorReceiver, for a global management globalErrorReceiver?.handleGlobalError(GlobalError.ConsentNotGivenError(matrixError.consentUri)) } httpCode == HttpURLConnection.HTTP_UNAUTHORIZED /* 401 */ && matrixError.code == MatrixError.M_UNKNOWN_TOKEN -> { - // Also send this error to the globalErrorReceiver, for a global management globalErrorReceiver?.handleGlobalError(GlobalError.InvalidToken(matrixError.isSoftLogout.orFalse())) } + matrixError.code == MatrixError.ORG_MATRIX_EXPIRED_ACCOUNT -> { + globalErrorReceiver?.handleGlobalError(GlobalError.ExpiredAccount) + } } return Failure.ServerError(matrixError, httpCode) diff --git a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt index 61abbd445b3..dc195208657 100644 --- a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt +++ b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt @@ -267,6 +267,7 @@ abstract class VectorBaseActivity : AppCompatActivity(), HasSc activeSessionHolder.getActiveSession().sessionParams.homeServerHost ?: "") is GlobalError.CertificateError -> handleCertificateError(globalError) + GlobalError.ExpiredAccount -> Unit // TODO Handle account expiration }.exhaustive } From cb3ce2708fec72f7def60b12df1f7a55ceb0c9a2 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Wed, 25 Aug 2021 16:21:19 +0200 Subject: [PATCH 2/3] Add towncrier --- changelog.d/3900.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/3900.feature diff --git a/changelog.d/3900.feature b/changelog.d/3900.feature new file mode 100644 index 00000000000..765f51f054d --- /dev/null +++ b/changelog.d/3900.feature @@ -0,0 +1 @@ +Add expired account error code in the matrix SDK \ No newline at end of file From 7714cc41f7cbf64ad2193f6f9d94d18b7ecbfbe0 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Fri, 27 Aug 2021 14:33:12 +0200 Subject: [PATCH 3/3] Attach documentation reference to expired account error --- .../java/org/matrix/android/sdk/api/failure/MatrixError.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixError.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixError.kt index f2da2b9ad24..e0230e6dff5 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixError.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/failure/MatrixError.kt @@ -190,7 +190,11 @@ data class MatrixError( // Possible value for "limit_type" const val LIMIT_TYPE_MAU = "monthly_active_user" - /** The user account has expired. It has to be renewed by clicking on an email or by sending a renewal token. */ + /** + * The user account has expired. It has to be renewed by clicking on an email or by sending a renewal token. + * + * More documentation can be found in the dedicated Synapse plugin module repository: https://github.com/matrix-org/synapse-email-account-validity + */ const val ORG_MATRIX_EXPIRED_ACCOUNT = "ORG_MATRIX_EXPIRED_ACCOUNT" } }