view src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt @ 21:ea65ab890f66

More work to support interactive feature.
author David Barts <n5jrn@me.com>
date Tue, 02 Jul 2024 11:27:39 -0700
parents 302d224bbd57
children
line wrap: on
line source

package name.blackcap.passman

import org.apache.commons.cli.*
import java.sql.Types
import java.util.*
import kotlin.properties.Delegates

class UpdateSubcommand(): Subcommand() {
    private lateinit var commandLine: CommandLine
    private lateinit var db: Database
    private lateinit var nameIn: String
    private var id by Delegates.notNull<Long>()
    private var length by Delegates.notNull<Int>()
    private val fields = StringBuilder()
    private val fieldValues = mutableListOf<Any?>()

    private companion object {
        const val GENERATE = "generate"
        const val HELP = "help"
        const val LENGTH = "length"
        const val SYMBOLS = "symbols"
        const val VERBOSE = "verbose"
        const val NULL_SPECIFIED = "."
        val NULLABLE_FIELDS = setOf<String>("notes")
        val SENSITIVE_FIELDS = setOf<String>("password")
    }

    override fun run(args: Array<String>) {
        parseArguments(args)
        if (commandLine.hasOption(HELP)) {
            return
        }
        checkDatabase()
        try {
            update()
        } finally {
            cleanUp()
        }
    }

    private fun parseArguments(args: Array<String>) {
        val options = Options().apply {
            addOption("g", GENERATE, false, "Use password generator.")
            addOption("h", HELP, false, "Print this help message.")
            addOption("l", LENGTH, true, "Length of generated password (default $DEFAULT_GENERATED_LENGTH).")
            addOption("s", SYMBOLS, false, "Use symbol characters in generated password.")
            addOption("v", VERBOSE, false, "Print the generated password.")
        }
        try {
            commandLine = DefaultParser().parse(options, args)
        } catch (e: ParseException) {
            throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
        }
        if (commandLine.hasOption(HELP)) {
            HelpFormatter().printHelp("$SHORTNAME update [options] name", options)
            return
        }
        checkArguments()
        db = Database.default
        nameIn = commandLine.args[0]
        id = db.makeKey(nameIn)
        val rawLength = commandLine.getOptionValue(LENGTH)
        length = try {
            rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH
        } catch (e: NumberFormatException) {
            -1
        }
        if (length < MIN_GENERATED_LENGTH) {
            throw SubcommandException(message = "${see(rawLength)} - invalid length")
        }
    }

    private fun checkArguments(): Unit {
        var bad = false
        if (!commandLine.hasOption(GENERATE)) {
            for (option in listOf<String>(LENGTH, SYMBOLS, VERBOSE)) {
                if (commandLine.hasOption(option)) {
                    error("--$option requires --$GENERATE")
                    bad = true
                }
            }
        }
        if (bad) {
            throw SubcommandException(status = 2)
        }
        if (commandLine.args.isEmpty()) {
            throw SubcommandException(message = "expecting site name", status = 2)
        }
        if (commandLine.args.size > 1) {
            throw SubcommandException(message = "unexpected trailing arguments", status = 2)
        }
    }

    private fun checkDatabase(): Unit {
        db.connection.prepareStatement("select count(*) from passwords where id = ?").use {
            it.setLong(1, id)
            val result = it.executeQuery()
            result.next()
            val count = result.getInt(1)
            if (count < 1) {
                throw SubcommandException(message = "no record matches " + see(nameIn))
            }
        }
    }

    private fun update(): Unit {
        updateOne("username")
        if (commandLine.hasOption(GENERATE)) {
            generatePassword()
        } else {
            updateOne("password")
        }
        updateOne("notes")
        if (fieldValues.isEmpty()) {
            error("no values changed")
            return
        }

        db.connection.prepareStatement("update passwords set modified = ?, $fields where id = ?").use { stmt ->
            stmt.setLong(1, System.currentTimeMillis())
            fieldValues.indices.forEach { fieldIndex ->
                val fieldValue = fieldValues[fieldIndex]
                val columnIndex = fieldIndex + 2
                when (fieldValue) {
                    is String -> stmt.setEncryptedString(columnIndex, fieldValue, db.encryption)
                    is CharArray -> stmt.setEncrypted(columnIndex, fieldValue, db.encryption)
                    null -> stmt.setNull(columnIndex, Types.BLOB)
                    else -> throw RuntimeException("this shouldn't happen")
                }
            }
            stmt.setLong(fieldValues.size + 2, id)
            stmt.execute()
        }
    }

    private fun cleanUp(): Unit {
        fieldValues.forEach {
            if (it is CharArray) {
                it.clear()
            }
        }
    }

    private fun updateOne(name: String): Unit {
        val prompt = name.replaceFirstChar { it.uppercase(Locale.getDefault()) } + ": "
        val value: Any? = if (name in SENSITIVE_FIELDS) {
            try {
                getPassword(prompt)
            } catch (e: ConsoleException) {
                throw SubcommandException(message = e.message, cause = e)
            }
        } else {
            val rawValue = readLine(prompt)
            if (name in NULLABLE_FIELDS && rawValue == NULL_SPECIFIED) {
                null
            } else {
                rawValue
            }
        }

        val noChange = when (value) {
            is String -> value.isEmpty()
            is CharArray -> value.isEmpty()
            else -> false
        }
        if (noChange) {
            return
        }

        addOne(name, value)
    }

    private fun addOne(name: String, value: Any?) {
        if (fields.isNotEmpty()) {
            fields.append(", ")
        }
        fields.append(name)
        fields.append(" = ?")
        fieldValues.add(value)
    }

    private fun generatePassword(): Unit {
        val newPassword = generate(length, commandLine.hasOption(SYMBOLS))
        if (commandLine.hasOption(VERBOSE)) {
            printPassword(newPassword)
        }
        addOne("password", newPassword)
    }

}