|
| 1 | +/* |
| 2 | + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: GPL-3.0-only |
| 4 | + */ |
| 5 | +package dev.msfjarvis.aps.ui.autofill |
| 6 | + |
| 7 | +import android.app.PendingIntent |
| 8 | +import android.content.Context |
| 9 | +import android.content.Intent |
| 10 | +import android.content.IntentSender |
| 11 | +import android.os.Build |
| 12 | +import android.os.Bundle |
| 13 | +import android.view.autofill.AutofillManager |
| 14 | +import androidx.annotation.RequiresApi |
| 15 | +import androidx.appcompat.app.AppCompatActivity |
| 16 | +import androidx.lifecycle.lifecycleScope |
| 17 | +import com.github.ajalt.timberkt.d |
| 18 | +import com.github.ajalt.timberkt.e |
| 19 | +import com.github.androidpasswordstore.autofillparser.AutofillAction |
| 20 | +import com.github.androidpasswordstore.autofillparser.Credentials |
| 21 | +import com.github.michaelbull.result.getOrElse |
| 22 | +import com.github.michaelbull.result.onFailure |
| 23 | +import com.github.michaelbull.result.onSuccess |
| 24 | +import com.github.michaelbull.result.runCatching |
| 25 | +import dagger.hilt.android.AndroidEntryPoint |
| 26 | +import dev.msfjarvis.aps.injection.crypto.CryptoSet |
| 27 | +import dev.msfjarvis.aps.injection.password.PasswordEntryFactory |
| 28 | +import dev.msfjarvis.aps.ui.crypto.GopenpgpDecryptActivity |
| 29 | +import dev.msfjarvis.aps.util.autofill.AutofillPreferences |
| 30 | +import dev.msfjarvis.aps.util.autofill.AutofillResponseBuilder |
| 31 | +import dev.msfjarvis.aps.util.autofill.DirectoryStructure |
| 32 | +import java.io.File |
| 33 | +import javax.inject.Inject |
| 34 | +import kotlinx.coroutines.Dispatchers |
| 35 | +import kotlinx.coroutines.launch |
| 36 | +import kotlinx.coroutines.withContext |
| 37 | + |
| 38 | +@RequiresApi(Build.VERSION_CODES.O) |
| 39 | +@AndroidEntryPoint |
| 40 | +class GopenpgpAutofillDecryptActivity : AppCompatActivity() { |
| 41 | + |
| 42 | + companion object { |
| 43 | + |
| 44 | + private const val EXTRA_FILE_PATH = "dev.msfjarvis.aps.autofill.oreo.EXTRA_FILE_PATH" |
| 45 | + private const val EXTRA_SEARCH_ACTION = "dev.msfjarvis.aps.autofill.oreo.EXTRA_SEARCH_ACTION" |
| 46 | + |
| 47 | + private var decryptFileRequestCode = 1 |
| 48 | + |
| 49 | + fun makeDecryptFileIntent(file: File, forwardedExtras: Bundle, context: Context): Intent { |
| 50 | + return Intent(context, GopenpgpAutofillDecryptActivity::class.java).apply { |
| 51 | + putExtras(forwardedExtras) |
| 52 | + putExtra(EXTRA_SEARCH_ACTION, true) |
| 53 | + putExtra(EXTRA_FILE_PATH, file.absolutePath) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fun makeDecryptFileIntentSender(file: File, context: Context): IntentSender { |
| 58 | + val intent = |
| 59 | + Intent(context, GopenpgpAutofillDecryptActivity::class.java).apply { |
| 60 | + putExtra(EXTRA_SEARCH_ACTION, false) |
| 61 | + putExtra(EXTRA_FILE_PATH, file.absolutePath) |
| 62 | + } |
| 63 | + return PendingIntent.getActivity( |
| 64 | + context, |
| 65 | + decryptFileRequestCode++, |
| 66 | + intent, |
| 67 | + PendingIntent.FLAG_CANCEL_CURRENT |
| 68 | + ) |
| 69 | + .intentSender |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Inject lateinit var passwordEntryFactory: PasswordEntryFactory |
| 74 | + @Inject lateinit var cryptos: CryptoSet |
| 75 | + |
| 76 | + private lateinit var directoryStructure: DirectoryStructure |
| 77 | + |
| 78 | + override fun onStart() { |
| 79 | + super.onStart() |
| 80 | + val filePath = |
| 81 | + intent?.getStringExtra(EXTRA_FILE_PATH) |
| 82 | + ?: run { |
| 83 | + e { "GopenpgpAutofillDecryptActivity started without EXTRA_FILE_PATH" } |
| 84 | + finish() |
| 85 | + return |
| 86 | + } |
| 87 | + val clientState = |
| 88 | + intent?.getBundleExtra(AutofillManager.EXTRA_CLIENT_STATE) |
| 89 | + ?: run { |
| 90 | + e { "GopenpgpAutofillDecryptActivity started without EXTRA_CLIENT_STATE" } |
| 91 | + finish() |
| 92 | + return |
| 93 | + } |
| 94 | + val isSearchAction = intent?.getBooleanExtra(EXTRA_SEARCH_ACTION, true)!! |
| 95 | + val action = if (isSearchAction) AutofillAction.Search else AutofillAction.Match |
| 96 | + directoryStructure = AutofillPreferences.directoryStructure(this) |
| 97 | + d { action.toString() } |
| 98 | + lifecycleScope.launch { |
| 99 | + val credentials = decryptCredential(File(filePath)) |
| 100 | + if (credentials == null) { |
| 101 | + setResult(RESULT_CANCELED) |
| 102 | + } else { |
| 103 | + val fillInDataset = |
| 104 | + AutofillResponseBuilder.makeFillInDataset( |
| 105 | + this@GopenpgpAutofillDecryptActivity, |
| 106 | + credentials, |
| 107 | + clientState, |
| 108 | + action |
| 109 | + ) |
| 110 | + withContext(Dispatchers.Main) { |
| 111 | + setResult( |
| 112 | + RESULT_OK, |
| 113 | + Intent().apply { putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, fillInDataset) } |
| 114 | + ) |
| 115 | + } |
| 116 | + } |
| 117 | + withContext(Dispatchers.Main) { finish() } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private suspend fun decryptCredential(file: File): Credentials? { |
| 122 | + runCatching { file.inputStream() } |
| 123 | + .onFailure { e -> |
| 124 | + e(e) { "File to decrypt not found" } |
| 125 | + return null |
| 126 | + } |
| 127 | + .onSuccess { encryptedInput -> |
| 128 | + runCatching { |
| 129 | + val crypto = cryptos.first { it.canHandle(file.absolutePath) } |
| 130 | + withContext(Dispatchers.IO) { |
| 131 | + crypto.decrypt( |
| 132 | + GopenpgpDecryptActivity.PRIV_KEY, |
| 133 | + GopenpgpDecryptActivity.PASS.toByteArray(charset = Charsets.UTF_8), |
| 134 | + encryptedInput.readBytes() |
| 135 | + ) |
| 136 | + } |
| 137 | + } |
| 138 | + .onFailure { e -> |
| 139 | + e(e) { "Decryption with Gopenpgp failed" } |
| 140 | + return null |
| 141 | + } |
| 142 | + .onSuccess { result -> |
| 143 | + return runCatching { |
| 144 | + val entry = passwordEntryFactory.create(lifecycleScope, result) |
| 145 | + AutofillPreferences.credentialsFromStoreEntry(this, file, entry, directoryStructure) |
| 146 | + } |
| 147 | + .getOrElse { e -> |
| 148 | + e(e) { "Failed to parse password entry" } |
| 149 | + return null |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + return null |
| 154 | + } |
| 155 | +} |
0 commit comments