Skip to content

Commit d05d5c3

Browse files
committed
refactor(setup-encryption-dialog): download key
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent f8b2539 commit d05d5c3

2 files changed

Lines changed: 71 additions & 59 deletions

File tree

app/src/main/java/com/owncloud/android/ui/dialog/setupEncryption/SetupEncryptionDialogFragment.kt

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import com.owncloud.android.lib.resources.users.GetPublicKeyRemoteOperation
3434
import com.owncloud.android.lib.resources.users.GetServerPublicKeyRemoteOperation
3535
import com.owncloud.android.lib.resources.users.SendCSRRemoteOperation
3636
import com.owncloud.android.lib.resources.users.StorePrivateKeyRemoteOperation
37+
import com.owncloud.android.ui.dialog.setupEncryption.model.DownloadKeyResult
3738
import com.owncloud.android.utils.DisplayUtils
3839
import com.owncloud.android.utils.EncryptionUtils
3940
import com.owncloud.android.utils.crypto.CryptoHelper
@@ -46,9 +47,6 @@ import java.io.IOException
4647
import java.lang.ref.WeakReference
4748
import javax.inject.Inject
4849

49-
/*
50-
* Dialog to setup encryption
51-
*/
5250
class SetupEncryptionDialogFragment :
5351
DialogFragment(),
5452
Injectable {
@@ -170,10 +168,6 @@ class SetupEncryptionDialogFragment :
170168
}
171169

172170
val privateKey = (downloadKeyResult as DownloadKeyResult.Success).privateKey
173-
if (privateKey.isNullOrEmpty()) {
174-
Log_OC.e(TAG, "privateKey is null or empty")
175-
return@launch
176-
}
177171
val mnemonicUnchanged = binding.encryptionPasswordInput.text.toString().trim()
178172
val mnemonic =
179173
binding.encryptionPasswordInput.text.toString().replace("\\s".toRegex(), "")
@@ -275,30 +269,6 @@ class SetupEncryptionDialogFragment :
275269
super.onSaveInstanceState(outState)
276270
}
277271

278-
sealed class DownloadKeyResult(open val descriptionId: Int? = null) {
279-
data class CertificateVerificationFailed(
280-
override val descriptionId: Int = R.string.end_to_end_encryption_certificate_verification_failed
281-
) : DownloadKeyResult(descriptionId)
282-
283-
data class ServerPublicKeyUnavailable(
284-
override val descriptionId: Int = R.string.end_to_end_encryption_server_public_key_unavailable
285-
) : DownloadKeyResult(descriptionId)
286-
287-
data class ServerPrivateKeyUnavailable(
288-
override val descriptionId: Int = R.string.end_to_end_encryption_server_private_key_unavailable
289-
) : DownloadKeyResult(descriptionId)
290-
291-
data class CertificateUnavailable(
292-
override val descriptionId: Int = R.string.end_to_end_encryption_certificate_unavailable
293-
) : DownloadKeyResult(descriptionId)
294-
295-
data class UnexpectedError(
296-
override val descriptionId: Int = R.string.end_to_end_encryption_unexpected_error_occurred
297-
) : DownloadKeyResult(descriptionId)
298-
299-
data class Success(val privateKey: String?) : DownloadKeyResult()
300-
}
301-
302272
private suspend fun downloadKeys() {
303273
binding.encryptionStatus.setText(R.string.end_to_end_encryption_retrieving_keys)
304274
positiveButton?.visibility = View.INVISIBLE
@@ -314,7 +284,7 @@ class SetupEncryptionDialogFragment :
314284
// The certificate might not be available on the server yet.
315285
// Therefore, the user needs to generate a new passphrase first, send csr.
316286
return@withContext if (certificateResult.httpCode == HttpStatus.SC_NOT_FOUND) {
317-
DownloadKeyResult.Success(null)
287+
DownloadKeyResult.GeneratePassphraseSendCSR
318288
} else {
319289
DownloadKeyResult.CertificateUnavailable()
320290
}
@@ -346,15 +316,19 @@ class SetupEncryptionDialogFragment :
346316
Log_OC.d(TAG, "private key successful downloaded for " + user.accountName)
347317
keyResult = KEY_EXISTING_USED
348318
val privateKey = privateKeyResult.resultData?.getKey()
349-
DownloadKeyResult.Success(privateKey)
319+
if (privateKey == null) {
320+
DownloadKeyResult.ServerPrivateKeyUnavailable()
321+
} else {
322+
DownloadKeyResult.Success(privateKey)
323+
}
350324
} else {
351325
DownloadKeyResult.ServerPrivateKeyUnavailable()
352326
}
353327
}
354328

355329
downloadKeyResult?.let { result ->
356-
if (result is DownloadKeyResult.Success) {
357-
handlePrivateKey(result.privateKey)
330+
if (result is DownloadKeyResult.Success || result is DownloadKeyResult.GeneratePassphraseSendCSR) {
331+
handlePrivateKey(result)
358332
} else {
359333
val descriptionId = result.descriptionId ?: return
360334
val description = getString(descriptionId)
@@ -364,23 +338,28 @@ class SetupEncryptionDialogFragment :
364338
}
365339
}
366340

367-
private fun handlePrivateKey(privateKey: String?) {
368-
if (privateKey == null) {
369-
// first show info
370-
try {
371-
if (keyWords == null || keyWords!!.isEmpty()) {
372-
keyWords = EncryptionUtils.getRandomWords(NUMBER_OF_WORDS, context)
341+
private fun handlePrivateKey(result: DownloadKeyResult) {
342+
when (result) {
343+
is DownloadKeyResult.Success -> {
344+
binding.encryptionStatus.setText(R.string.end_to_end_encryption_enter_passphrase_to_access_files)
345+
binding.encryptionPasswordInputContainer.visibility = View.VISIBLE
346+
positiveButton?.visibility = View.VISIBLE
347+
}
348+
349+
is DownloadKeyResult.GeneratePassphraseSendCSR -> {
350+
try {
351+
if (keyWords == null || keyWords!!.isEmpty()) {
352+
keyWords = EncryptionUtils.getRandomWords(NUMBER_OF_WORDS, context)
353+
}
354+
showMnemonicInfo()
355+
} catch (_: IOException) {
356+
binding.encryptionStatus.setText(R.string.common_error)
373357
}
374-
showMnemonicInfo()
375-
} catch (e: IOException) {
376-
binding.encryptionStatus.setText(R.string.common_error)
377358
}
378-
} else if (privateKey.isNotEmpty()) {
379-
binding.encryptionStatus.setText(R.string.end_to_end_encryption_enter_passphrase_to_access_files)
380-
binding.encryptionPasswordInputContainer.visibility = View.VISIBLE
381-
positiveButton?.visibility = View.VISIBLE
382-
} else {
383-
Log_OC.e(TAG, "Got empty private key string")
359+
360+
else -> {
361+
Log_OC.e(TAG, "Got empty private key string")
362+
}
384363
}
385364
}
386365

@@ -552,15 +531,12 @@ class SetupEncryptionDialogFragment :
552531
private const val KEY_GENERATE = "KEY_GENERATE"
553532

554533
@JvmStatic
555-
fun newInstance(user: User?, filePath: String?): SetupEncryptionDialogFragment {
556-
val bundle = Bundle().apply {
557-
putParcelable(ARG_USER, user)
558-
putString(ARG_FILE_PATH, filePath)
559-
}
560-
561-
return SetupEncryptionDialogFragment().apply {
562-
arguments = bundle
534+
fun newInstance(user: User?, filePath: String?): SetupEncryptionDialogFragment =
535+
SetupEncryptionDialogFragment().apply {
536+
arguments = Bundle().apply {
537+
putParcelable(ARG_USER, user)
538+
putString(ARG_FILE_PATH, filePath)
539+
}
563540
}
564-
}
565541
}
566542
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.owncloud.android.ui.dialog.setupEncryption.model
9+
10+
import com.owncloud.android.R
11+
12+
sealed class DownloadKeyResult(open val descriptionId: Int? = null) {
13+
data class CertificateVerificationFailed(
14+
override val descriptionId: Int = R.string.end_to_end_encryption_certificate_verification_failed
15+
) : DownloadKeyResult(descriptionId)
16+
17+
data class ServerPublicKeyUnavailable(
18+
override val descriptionId: Int = R.string.end_to_end_encryption_server_public_key_unavailable
19+
) : DownloadKeyResult(descriptionId)
20+
21+
data class ServerPrivateKeyUnavailable(
22+
override val descriptionId: Int = R.string.end_to_end_encryption_server_private_key_unavailable
23+
) : DownloadKeyResult(descriptionId)
24+
25+
data class CertificateUnavailable(
26+
override val descriptionId: Int = R.string.end_to_end_encryption_certificate_unavailable
27+
) : DownloadKeyResult(descriptionId)
28+
29+
data class UnexpectedError(
30+
override val descriptionId: Int = R.string.end_to_end_encryption_unexpected_error_occurred
31+
) : DownloadKeyResult(descriptionId)
32+
33+
data object GeneratePassphraseSendCSR : DownloadKeyResult()
34+
35+
data class Success(val privateKey: String) : DownloadKeyResult()
36+
}

0 commit comments

Comments
 (0)