Skip to content

Commit

Permalink
Add reminder for mandatory client cert password
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Apr 23, 2024
1 parent 2d70b94 commit 11448f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
32 changes: 28 additions & 4 deletions app/src/main/kotlin/com/github/gotify/login/AdvancedDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.github.gotify.login

import android.content.Context
import android.view.LayoutInflater
import android.widget.Button
import android.widget.CompoundButton
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.doOnTextChanged
import com.github.gotify.R
import com.github.gotify.databinding.AdvancedSettingsDialogBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand All @@ -11,6 +14,8 @@ internal class AdvancedDialog(
private val context: Context,
private val layoutInflater: LayoutInflater
) {
private lateinit var dialog: AlertDialog
private lateinit var dialogDoneButton: Button
private lateinit var binding: AdvancedSettingsDialogBinding
private var onCheckedChangeListener: CompoundButton.OnCheckedChangeListener? = null
private lateinit var onClickSelectCaCertificate: Runnable
Expand Down Expand Up @@ -66,22 +71,27 @@ internal class AdvancedDialog(
} else {
showRemoveCaCertificate(caCertCN!!)
}
if (!clientCertPassword.isNullOrEmpty()) {
binding.clientCertPasswordEdittext.setText(clientCertPassword)
}
if (clientCertPath == null) {
showSelectClientCertificate()
} else {
showRemoveClientCertificate()
}
if (!clientCertPassword.isNullOrEmpty()) {
binding.clientCertPasswordEdittext.setText(clientCertPassword)
binding.clientCertPasswordEdittext.doOnTextChanged { _, _, _, _ ->
showPasswordMissing(binding.clientCertPasswordEdittext.text.toString().isEmpty())
}
MaterialAlertDialogBuilder(context)
dialog = MaterialAlertDialogBuilder(context)
.setView(binding.root)
.setTitle(R.string.advanced_settings)
.setPositiveButton(context.getString(R.string.done), null)
.setOnDismissListener {
onClose(binding.clientCertPasswordEdittext.text.toString())
}
.show()
.create()
dialog.show()
dialogDoneButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
return this
}

Expand All @@ -104,6 +114,7 @@ internal class AdvancedDialog(
binding.toggleClientCert.setText(R.string.select_client_certificate)
binding.toggleClientCert.setOnClickListener { onClickSelectClientCertificate.run() }
binding.selectedClientCert.setText(R.string.no_certificate_selected)
showPasswordMissing(false)
}

fun showRemoveClientCertificate() {
Expand All @@ -113,5 +124,18 @@ internal class AdvancedDialog(
onClickRemoveClientCertificate.run()
}
binding.selectedClientCert.setText(R.string.certificate_found)
showPasswordMissing(binding.clientCertPasswordEdittext.text.toString().isEmpty())
}

private fun showPasswordMissing(toggled: Boolean) {
if (::dialogDoneButton.isInitialized) {
dialogDoneButton.isEnabled = !toggled
}
val error = if (toggled) {
context.getString(R.string.client_cert_password_missing)
} else {
null
}
binding.clientCertPassword.error = error
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<string name="select_ca_file">Select a CA Certificate File</string>
<string name="select_client_file">Select a Client Certificate File</string>
<string name="client_cert_password">Certificate Password</string>
<string name="client_cert_password_missing">Password required</string>
<string name="please_install_file_browser">Please install a file browser</string>
<string name="select_ca_failed">Failed to read CA cert: %s</string>
<string name="select_client_failed">Failed to read client cert: %s</string>
Expand Down

0 comments on commit 11448f7

Please sign in to comment.