Skip to content

Commit

Permalink
credentials settings still not working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Jan 26, 2024
1 parent d7f6e91 commit ff357d0
Showing 1 changed file with 110 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import com.evolveum.midpoint.studio.impl.EncryptionService
import com.evolveum.midpoint.studio.impl.configuration.MidPointService
import com.evolveum.midpoint.studio.util.MidPointUtils
import com.evolveum.midpoint.studio.util.StudioLocalization.message
import com.intellij.openapi.observable.properties.AtomicBooleanProperty
import com.intellij.openapi.options.BoundSearchableConfigurable
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogPanel
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.ui.components.JBPasswordField
import com.intellij.ui.dsl.builder.COLUMNS_SHORT
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.columns
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.layout.ComponentPredicate
import com.intellij.ui.layout.ValidationInfoBuilder
import org.apache.commons.lang3.StringUtils
import javax.swing.JLabel

/**
Expand All @@ -22,94 +25,146 @@ import javax.swing.JLabel
class CredentialsConfigurable(val project: Project) :
BoundSearchableConfigurable(message("CredentialsConfigurable.title"), "") {

private var oldPassword: Cell<JBPasswordField>? = null
private val oldPassword = JBPasswordField()

private var newPassword: Cell<JBPasswordField>? = null
private val newPassword = JBPasswordField()

private var repeatNewPassword: Cell<JBPasswordField>? = null
private val repeatNewPassword = JBPasswordField()

private var text: Cell<JLabel>? = null
private val status = JLabel()

private var oldVisible = true
private val oldVisible = BooleanPropertyPredicate(true)

private var newVisible = true
private val newVisible = BooleanPropertyPredicate(true)

override fun apply() {
val oldPwd = oldPassword?.component?.password
val newPwd = newPassword?.component?.password

if (oldPwd != null && oldPwd.isNotEmpty() && newPwd != null && newPwd.isNotEmpty()) {
try {
EncryptionService.getInstance(project)
.changeMasterPassword(oldPwd.concatToString(), newPwd.concatToString())
} catch (ex: Exception) {
error("Couldn't change master password: ${ex.message}")
}
}
super.apply()
println(">>>> apply")
// val oldPwd = oldPassword.password
// val newPwd = newPassword.password
//
// if (oldPwd != null && oldPwd.isNotEmpty() && newPwd != null && newPwd.isNotEmpty()) {
// try {
// EncryptionService.getInstance(project)
// .changeMasterPassword(oldPwd.concatToString(), newPwd.concatToString())
// } catch (ex: Exception) {
// error("Couldn't change master password: ${ex.message}")
// }
// }
}

override fun isModified(): Boolean {
return super.isModified() || isModified(oldPassword) || isModified(newPassword) || isModified(repeatNewPassword)
return super.isModified()
|| isModified(oldPassword)
|| isModified(newPassword)
|| isModified(repeatNewPassword)
}

private fun isModified(cell: Cell<JBPasswordField>?): Boolean {
return cell?.component?.password?.isNotEmpty() ?: false
private fun isModified(component: JBPasswordField): Boolean {
return component.password.isNotEmpty()
}

override fun reset() {
super.reset()

oldPassword?.component?.text = null
newPassword?.component?.text = null
repeatNewPassword?.component?.text = null
oldPassword.text = null
newPassword.text = null
repeatNewPassword.text = null

val status = EncryptionService.getInstance(project).status

this.status.text = status.message

val oldVisible = status.status == EncryptionService.Status.OK
|| status.status == EncryptionService.Status.PASSWORD_NOT_SET
|| status.status == EncryptionService.Status.PASSWORD_INCORRECT
this.oldVisible.set(oldVisible)

val newVisible = status.status == EncryptionService.Status.OK
|| status.status == EncryptionService.Status.MISSING_FILE
this.newVisible.set(newVisible)
}

override fun createPanel(): DialogPanel {
return panel {
group(message("CredentialsConfigurable.credentials")) {
row("Old password:") {
oldPassword = cell(JBPasswordField())
cell(oldPassword)
.columns(COLUMNS_SHORT)
.visibleIf(ComponentPredicate.fromValue(oldVisible))
.validationOnInput { component ->
val password = component.password

if (password.isNotEmpty()) {
val projectId = MidPointService.get(project).settings.projectId
val currentPwd = MidPointUtils.getPassword(projectId)

if (password.concatToString() != currentPwd) {
error("Old password doesn't match one that is stored in keychain with id $projectId")
}
}

null
}
.visibleIf(oldVisible)
.validationOnApply { validateOldPassword(it) }
}
row("New password:") {
newPassword = cell(JBPasswordField())
cell(newPassword)
.columns(COLUMNS_SHORT)
.visibleIf(ComponentPredicate.fromValue(newVisible))
.visibleIf(newVisible)
}
row("Repeat new password:") {
repeatNewPassword = cell(JBPasswordField())
cell(repeatNewPassword)
.columns(COLUMNS_SHORT)
.visibleIf(ComponentPredicate.fromValue(newVisible))
.validationOnApply() { component ->
val password = newPassword?.component?.password
val repeatPassword = component.password

if (!repeatPassword.contentEquals(password)) {
error("New password and repeat password fields don't match.")
} else {
null
}
}
.visibleIf(newVisible)
.validationOnApply(::validatePasswords)
}
}
row {
text = label("Encryption service is correctly configured.")
cell(status)
}
}
}

private fun validateOldPassword(component: JBPasswordField): ValidationInfo? {
val password = component.password

if (password.isNotEmpty()) {
val projectId = MidPointService.get(project).settings.projectId
val currentPwd = MidPointUtils.getPassword(projectId)

if (StringUtils.isEmpty(currentPwd)) {
return ValidationInfoBuilder(component)
.error("There is no master password stored in keychain with id '$projectId'. Please set only the new password.")
}

if (password.concatToString() != currentPwd) {
return ValidationInfoBuilder(component)
.error("Old password doesn't match one that is stored in keychain with id '$projectId'.")
}
}

return null
}

private fun validatePasswords(
builder: ValidationInfoBuilder, password: JBPasswordField
): ValidationInfo? {
return builder.run {
val pwd = newPassword.password
val repeatPwd = repeatNewPassword.password

if (!pwd.contentEquals(repeatPwd)) {
return builder.error("New password and repeat password fields don't match.")
}

return null
}
}

class BooleanPropertyPredicate(value: Boolean) : ComponentPredicate() {

private val property: AtomicBooleanProperty = AtomicBooleanProperty(value)

override fun addListener(listener: (Boolean) -> Unit) {
}

override fun invoke(): Boolean {
return get()
}

fun set(value: Boolean) {
property.set(value)
}

fun get(): Boolean {
return property.get()
}
}
}

0 comments on commit ff357d0

Please sign in to comment.