Retry without client authentication in case of errors

This commit is contained in:
Jonas Lochmann 2024-04-08 02:00:00 +02:00
parent 3d5efdc3c0
commit 5880ef2885
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36

View file

@ -139,7 +139,10 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
} }
} }
override suspend fun sendMailLoginCode(mail: String, locale: String, deviceAuthToken: String?): String = withDeviceVerification { client -> override suspend fun sendMailLoginCode(mail: String, locale: String, deviceAuthToken: String?): String =
sendMailLoginCode(mail, locale, deviceAuthToken, false)
private suspend fun sendMailLoginCode(mail: String, locale: String, deviceAuthToken: String?, skipDeviceVerification: Boolean): String = withDeviceVerification (enable = !skipDeviceVerification) { client ->
postJsonRequest( postJsonRequest(
"auth/send-mail-login-code-v2", "auth/send-mail-login-code-v2",
client = client client = client
@ -153,14 +156,16 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
try { try {
it.assertSuccess() it.assertSuccess()
} catch (ex: BadRequestHttpError) { } catch (ex: BadRequestHttpError) {
if (deviceAuthToken != null) { if (deviceAuthToken != null || !skipDeviceVerification) {
// retry without device auth token // retry without device auth token
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "sendMailLoginCode() try again without deviceAuthToken") Log.d(LOG_TAG, "sendMailLoginCode() try again without deviceAuthToken and device verification")
} }
return@use sendMailLoginCode(mail, locale, null) Threads.network.executeAndWait { it.close() }
return@use sendMailLoginCode(mail, locale, null, true)
} else { } else {
throw ex throw ex
} }
@ -685,8 +690,8 @@ class HttpServerApi(private val endpointWithoutSlashAtEnd: String): ServerApi {
).waitForResponse() ).waitForResponse()
} }
private suspend fun <T> withDeviceVerification(block: suspend (client: OkHttpClient) -> T): T { private suspend fun <T> withDeviceVerification(enable: Boolean = true, block: suspend (client: OkHttpClient) -> T): T {
if (VERSION.SDK_INT >= VERSION_CODES.N) { if (VERSION.SDK_INT >= VERSION_CODES.N && enable) {
val keyStoreName = "AndroidKeyStore" val keyStoreName = "AndroidKeyStore"
val keyStore = KeyStore.getInstance(keyStoreName).also { it.load(null) } val keyStore = KeyStore.getInstance(keyStoreName).also { it.load(null) }
val keyId = "temp-" + UUID.randomUUID().toString() val keyId = "temp-" + UUID.randomUUID().toString()