Update to openpgp-ktx 1.0.0 (#585)

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
This commit is contained in:
Harsh Shandilya 2019-11-29 15:55:16 +05:30 committed by GitHub
parent fdbbb467b2
commit 0a01911d87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 173 additions and 164 deletions

View file

@ -567,12 +567,12 @@ class AutofillService : AccessibilityService() {
} }
private inner class OnBoundListener : OpenPgpServiceConnection.OnBound { private inner class OnBoundListener : OpenPgpServiceConnection.OnBound {
override fun onBound(service: IOpenPgpService2?) { override fun onBound(service: IOpenPgpService2) {
decryptAndVerify() decryptAndVerify()
} }
override fun onError(e: Exception?) { override fun onError(e: Exception) {
e?.printStackTrace() e.printStackTrace()
} }
} }

View file

@ -51,6 +51,9 @@ import kotlinx.android.synthetic.main.encrypt_layout.crypto_password_category
import kotlinx.android.synthetic.main.encrypt_layout.crypto_password_edit import kotlinx.android.synthetic.main.encrypt_layout.crypto_password_edit
import kotlinx.android.synthetic.main.encrypt_layout.crypto_password_file_edit import kotlinx.android.synthetic.main.encrypt_layout.crypto_password_file_edit
import kotlinx.android.synthetic.main.encrypt_layout.generate_password import kotlinx.android.synthetic.main.encrypt_layout.generate_password
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import me.msfjarvis.openpgpktx.OpenPgpError import me.msfjarvis.openpgpktx.OpenPgpError
import me.msfjarvis.openpgpktx.util.OpenPgpApi import me.msfjarvis.openpgpktx.util.OpenPgpApi
import me.msfjarvis.openpgpktx.util.OpenPgpApi.Companion.ACTION_DECRYPT_VERIFY import me.msfjarvis.openpgpktx.util.OpenPgpApi.Companion.ACTION_DECRYPT_VERIFY
@ -243,128 +246,129 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
val iStream = FileUtils.openInputStream(File(fullPath)) val iStream = FileUtils.openInputStream(File(fullPath))
val oStream = ByteArrayOutputStream() val oStream = ByteArrayOutputStream()
api?.executeApiAsync(data, iStream, oStream, object : OpenPgpApi.IOpenPgpCallback { GlobalScope.launch(IO) {
override fun onReturn(result: Intent?) { api?.executeApiAsync(data, iStream, oStream, object : OpenPgpApi.IOpenPgpCallback {
when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { override fun onReturn(result: Intent?) {
RESULT_CODE_SUCCESS -> { when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) {
try { RESULT_CODE_SUCCESS -> {
val showPassword = settings.getBoolean("show_password", true) try {
val showExtraContent = settings.getBoolean("show_extra_content", true) val showPassword = settings.getBoolean("show_password", true)
val showExtraContent = settings.getBoolean("show_extra_content", true)
crypto_container_decrypt.visibility = View.VISIBLE crypto_container_decrypt.visibility = View.VISIBLE
val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf") val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf")
val entry = PasswordEntry(oStream) val entry = PasswordEntry(oStream)
passwordEntry = entry passwordEntry = entry
if (intent.getStringExtra("OPERATION") == "EDIT") { if (intent.getStringExtra("OPERATION") == "EDIT") {
editPassword() editPassword()
return return
} }
if (entry.password.isEmpty()) { if (entry.password.isEmpty()) {
crypto_password_show.visibility = View.GONE crypto_password_show.visibility = View.GONE
crypto_password_show_label.visibility = View.GONE crypto_password_show_label.visibility = View.GONE
} else { } else {
crypto_password_show.visibility = View.VISIBLE crypto_password_show.visibility = View.VISIBLE
crypto_password_show_label.visibility = View.VISIBLE crypto_password_show_label.visibility = View.VISIBLE
crypto_password_show.typeface = monoTypeface
crypto_password_show.text = entry.password
}
crypto_password_show.typeface = monoTypeface crypto_password_show.typeface = monoTypeface
crypto_password_show.text = entry.password crypto_password_show.text = entry.password
}
crypto_password_show.typeface = monoTypeface
crypto_password_show.text = entry.password
crypto_password_toggle_show.visibility = if (showPassword) View.GONE else View.VISIBLE crypto_password_toggle_show.visibility = if (showPassword) View.GONE else View.VISIBLE
crypto_password_show.transformationMethod = if (showPassword) { crypto_password_show.transformationMethod = if (showPassword) {
null null
} else { } else {
HoldToShowPasswordTransformation( HoldToShowPasswordTransformation(
crypto_password_toggle_show, crypto_password_toggle_show,
Runnable { crypto_password_show.text = entry.password } Runnable { crypto_password_show.text = entry.password }
) )
} }
if (entry.hasExtraContent()) { if (entry.hasExtraContent()) {
crypto_extra_show.typeface = monoTypeface crypto_extra_show.typeface = monoTypeface
crypto_extra_show.text = entry.extraContent crypto_extra_show.text = entry.extraContent
if (showExtraContent) { if (showExtraContent) {
crypto_extra_show_layout.visibility = View.VISIBLE crypto_extra_show_layout.visibility = View.VISIBLE
crypto_extra_toggle_show.visibility = View.GONE crypto_extra_toggle_show.visibility = View.GONE
crypto_extra_show.transformationMethod = null crypto_extra_show.transformationMethod = null
} else { } else {
crypto_extra_show_layout.visibility = View.GONE crypto_extra_show_layout.visibility = View.GONE
crypto_extra_toggle_show.visibility = View.VISIBLE crypto_extra_toggle_show.visibility = View.VISIBLE
crypto_extra_toggle_show.setOnCheckedChangeListener { _, _ -> crypto_extra_toggle_show.setOnCheckedChangeListener { _, _ ->
crypto_extra_show.text = entry.extraContent crypto_extra_show.text = entry.extraContent
} }
crypto_extra_show.transformationMethod = object : PasswordTransformationMethod() { crypto_extra_show.transformationMethod = object : PasswordTransformationMethod() {
override fun getTransformation(source: CharSequence, view: View): CharSequence { override fun getTransformation(source: CharSequence, view: View): CharSequence {
return if (crypto_extra_toggle_show.isChecked) source else super.getTransformation(source, view) return if (crypto_extra_toggle_show.isChecked) source else super.getTransformation(source, view)
}
} }
} }
}
if (entry.hasUsername()) { if (entry.hasUsername()) {
crypto_username_show.visibility = View.VISIBLE crypto_username_show.visibility = View.VISIBLE
crypto_username_show_label.visibility = View.VISIBLE crypto_username_show_label.visibility = View.VISIBLE
crypto_copy_username.visibility = View.VISIBLE crypto_copy_username.visibility = View.VISIBLE
crypto_copy_username.setOnClickListener { copyUsernameToClipBoard(entry.username!!) } crypto_copy_username.setOnClickListener { copyUsernameToClipBoard(entry.username!!) }
crypto_username_show.typeface = monoTypeface crypto_username_show.typeface = monoTypeface
crypto_username_show.text = entry.username crypto_username_show.text = entry.username
} else { } else {
crypto_username_show.visibility = View.GONE crypto_username_show.visibility = View.GONE
crypto_username_show_label.visibility = View.GONE crypto_username_show_label.visibility = View.GONE
crypto_copy_username.visibility = View.GONE crypto_copy_username.visibility = View.GONE
}
}
if (entry.hasTotp() || entry.hasHotp()) {
crypto_extra_show_layout.visibility = View.VISIBLE
crypto_extra_show.typeface = monoTypeface
crypto_extra_show.text = entry.extraContent
crypto_otp_show.visibility = View.VISIBLE
crypto_otp_show_label.visibility = View.VISIBLE
crypto_copy_otp.visibility = View.VISIBLE
if (entry.hasTotp()) {
crypto_copy_otp.setOnClickListener {
copyOtpToClipBoard(
Otp.calculateCode(
entry.totpSecret,
Date().time / (1000 * entry.totpPeriod),
entry.totpAlgorithm,
entry.digits)
)
} }
crypto_otp_show.text = }
Otp.calculateCode(
if (entry.hasTotp() || entry.hasHotp()) {
crypto_extra_show_layout.visibility = View.VISIBLE
crypto_extra_show.typeface = monoTypeface
crypto_extra_show.text = entry.extraContent
crypto_otp_show.visibility = View.VISIBLE
crypto_otp_show_label.visibility = View.VISIBLE
crypto_copy_otp.visibility = View.VISIBLE
if (entry.hasTotp()) {
crypto_copy_otp.setOnClickListener {
copyOtpToClipBoard(
Otp.calculateCode(
entry.totpSecret, entry.totpSecret,
Date().time / (1000 * entry.totpPeriod), Date().time / (1000 * entry.totpPeriod),
entry.totpAlgorithm, entry.totpAlgorithm,
entry.digits) entry.digits)
} else { )
// we only want to calculate and show HOTP if the user requests it }
crypto_copy_otp.setOnClickListener { crypto_otp_show.text =
if (settings.getBoolean("hotp_remember_check", false)) { Otp.calculateCode(
if (settings.getBoolean("hotp_remember_choice", false)) { entry.totpSecret,
calculateAndCommitHotp(entry) Date().time / (1000 * entry.totpPeriod),
entry.totpAlgorithm,
entry.digits)
} else {
// we only want to calculate and show HOTP if the user requests it
crypto_copy_otp.setOnClickListener {
if (settings.getBoolean("hotp_remember_check", false)) {
if (settings.getBoolean("hotp_remember_choice", false)) {
calculateAndCommitHotp(entry)
} else {
calculateHotp(entry)
}
} else { } else {
calculateHotp(entry) // show a dialog asking permission to update the HOTP counter in the entry
} val checkInflater = LayoutInflater.from(this@PgpActivity)
} else { val checkLayout = checkInflater.inflate(R.layout.otp_confirm_layout, null)
// show a dialog asking permission to update the HOTP counter in the entry val rememberCheck: CheckBox =
val checkInflater = LayoutInflater.from(this@PgpActivity)
val checkLayout = checkInflater.inflate(R.layout.otp_confirm_layout, null)
val rememberCheck: CheckBox =
checkLayout.findViewById(R.id.hotp_remember_checkbox) checkLayout.findViewById(R.id.hotp_remember_checkbox)
val dialogBuilder = MaterialAlertDialogBuilder(this@PgpActivity) val dialogBuilder = MaterialAlertDialogBuilder(this@PgpActivity)
dialogBuilder.setView(checkLayout) dialogBuilder.setView(checkLayout)
dialogBuilder.setMessage(R.string.dialog_update_body) dialogBuilder.setMessage(R.string.dialog_update_body)
.setCancelable(false) .setCancelable(false)
.setPositiveButton(R.string.dialog_update_positive) { _, _ -> .setPositiveButton(R.string.dialog_update_positive) { _, _ ->
run { run {
@ -386,32 +390,33 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
editor.apply() editor.apply()
} }
} }
val updateDialog = dialogBuilder.create() val updateDialog = dialogBuilder.create()
updateDialog.setTitle(R.string.dialog_update_title) updateDialog.setTitle(R.string.dialog_update_title)
updateDialog.show() updateDialog.show()
}
} }
crypto_otp_show.setText(R.string.hotp_pending)
} }
crypto_otp_show.setText(R.string.hotp_pending) crypto_otp_show.typeface = monoTypeface
} else {
crypto_otp_show.visibility = View.GONE
crypto_otp_show_label.visibility = View.GONE
crypto_copy_otp.visibility = View.GONE
} }
crypto_otp_show.typeface = monoTypeface
} else {
crypto_otp_show.visibility = View.GONE
crypto_otp_show_label.visibility = View.GONE
crypto_copy_otp.visibility = View.GONE
}
if (settings.getBoolean("copy_on_decrypt", true)) { if (settings.getBoolean("copy_on_decrypt", true)) {
copyPasswordToClipBoard() copyPasswordToClipBoard()
}
} catch (e: Exception) {
Log.e(TAG, "An Exception occurred", e)
} }
} catch (e: Exception) {
Log.e(TAG, "An Exception occurred", e)
} }
RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_DECRYPT)
RESULT_CODE_ERROR -> handleError(result)
} }
RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_DECRYPT)
RESULT_CODE_ERROR -> handleError(result)
} }
} })
}) }
} }
/** /**
@ -453,36 +458,38 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
val path = if (intent.getBooleanExtra("fromDecrypt", false)) fullPath else "$fullPath/$editName.gpg" val path = if (intent.getBooleanExtra("fromDecrypt", false)) fullPath else "$fullPath/$editName.gpg"
api?.executeApiAsync(data, iStream, oStream, object : OpenPgpApi.IOpenPgpCallback { GlobalScope.launch(IO) {
override fun onReturn(result: Intent?) { api?.executeApiAsync(data, iStream, oStream, object : OpenPgpApi.IOpenPgpCallback {
when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { override fun onReturn(result: Intent?) {
RESULT_CODE_SUCCESS -> { when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) {
try { RESULT_CODE_SUCCESS -> {
// TODO This might fail, we should check that the write is successful try {
val outputStream = FileUtils.openOutputStream(File(path)) // TODO This might fail, we should check that the write is successful
outputStream.write(oStream.toByteArray()) val outputStream = FileUtils.openOutputStream(File(path))
outputStream.close() outputStream.write(oStream.toByteArray())
outputStream.close()
val returnIntent = Intent() val returnIntent = Intent()
returnIntent.putExtra("CREATED_FILE", path) returnIntent.putExtra("CREATED_FILE", path)
returnIntent.putExtra("NAME", editName) returnIntent.putExtra("NAME", editName)
returnIntent.putExtra("LONG_NAME", getLongName(fullPath, repoPath, editName!!)) returnIntent.putExtra("LONG_NAME", getLongName(fullPath, repoPath, editName!!))
// if coming from decrypt screen->edit button // if coming from decrypt screen->edit button
if (intent.getBooleanExtra("fromDecrypt", false)) { if (intent.getBooleanExtra("fromDecrypt", false)) {
returnIntent.putExtra("OPERATION", "EDIT") returnIntent.putExtra("OPERATION", "EDIT")
returnIntent.putExtra("needCommit", true) returnIntent.putExtra("needCommit", true)
}
setResult(RESULT_OK, returnIntent)
finish()
} catch (e: Exception) {
Log.e(TAG, "An Exception occurred", e)
} }
setResult(RESULT_OK, returnIntent)
finish()
} catch (e: Exception) {
Log.e(TAG, "An Exception occurred", e)
} }
RESULT_CODE_ERROR -> handleError(result)
} }
RESULT_CODE_ERROR -> handleError(result)
} }
} })
}) }
} }
/** /**
@ -559,39 +566,41 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
private fun getKeyIds(receivedIntent: Intent? = null) { private fun getKeyIds(receivedIntent: Intent? = null) {
val data = receivedIntent ?: Intent() val data = receivedIntent ?: Intent()
data.action = OpenPgpApi.ACTION_GET_KEY_IDS data.action = OpenPgpApi.ACTION_GET_KEY_IDS
api?.executeApiAsync(data, null, null, object : OpenPgpApi.IOpenPgpCallback { GlobalScope.launch(IO) {
override fun onReturn(result: Intent?) { api?.executeApiAsync(data, null, null, object : OpenPgpApi.IOpenPgpCallback {
when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) { override fun onReturn(result: Intent?) {
RESULT_CODE_SUCCESS -> { when (result?.getIntExtra(RESULT_CODE, RESULT_CODE_ERROR)) {
try { RESULT_CODE_SUCCESS -> {
val ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS) try {
val ids = result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS)
?: LongArray(0) ?: LongArray(0)
val keys = ids.map { it.toString() }.toSet() val keys = ids.map { it.toString() }.toSet()
// use Long // use Long
settings.edit().putStringSet("openpgp_key_ids_set", keys).apply() settings.edit().putStringSet("openpgp_key_ids_set", keys).apply()
showSnackbar("PGP keys selected") showSnackbar("PGP keys selected")
setResult(RESULT_OK) setResult(RESULT_OK)
finish() finish()
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "An Exception occurred", e) Log.e(TAG, "An Exception occurred", e)
}
} }
RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_KEY_ID)
RESULT_CODE_ERROR -> handleError(result)
} }
RESULT_CODE_USER_INTERACTION_REQUIRED -> handleUserInteractionRequest(result, REQUEST_KEY_ID)
RESULT_CODE_ERROR -> handleError(result)
} }
} })
}) }
} }
override fun onError(e: Exception?) {} override fun onError(e: Exception) {}
/** /**
* The action to take when the PGP service is bound * The action to take when the PGP service is bound
*/ */
override fun onBound(service: IOpenPgpService2?) { override fun onBound(service: IOpenPgpService2) {
initOpenPgpApi() initOpenPgpApi()
when (operation) { when (operation) {
"EDIT", "DECRYPT" -> decryptAndVerify() "EDIT", "DECRYPT" -> decryptAndVerify()

View file

@ -40,7 +40,7 @@ ext.deps = [
commons_codec: 'commons-codec:commons-codec:1.13', commons_codec: 'commons-codec:commons-codec:1.13',
jsch: 'com.jcraft:jsch:0.1.55', jsch: 'com.jcraft:jsch:0.1.55',
jgit: 'org.eclipse.jgit:org.eclipse.jgit:3.7.1.201504261725-r', jgit: 'org.eclipse.jgit:org.eclipse.jgit:3.7.1.201504261725-r',
openpgp_ktx: 'com.github.android-password-store:openpgp-ktx:0.1.0', openpgp_ktx: 'com.github.android-password-store:openpgp-ktx:1.0.0',
ssh_auth: 'org.sufficientlysecure:sshauthentication-api:1.0' ssh_auth: 'org.sufficientlysecure:sshauthentication-api:1.0'
], ],