Mercurial > cgi-bin > hgweb.cgi > PassMan
annotate src/main/kotlin/name/blackcap/passman/Entry.kt @ 27:3a3067ba673b
Add idle-time detection to interactive mode, clean up imports.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sat, 27 Jul 2024 09:50:54 -0700 |
parents | a38a2a1036c3 |
children |
rev | line source |
---|---|
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
1 package name.blackcap.passman |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
2 |
5
ad997df1f560
Fix see() to be about as good as sccc.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
3 import java.util.* |
7 | 4 import kotlin.reflect.KProperty |
5 import kotlin.reflect.full.declaredMemberProperties | |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
6 |
8 | 7 class Entry(val name: String, val username: String, val password: CharArray, val notes: String?, |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 val created: Date? = null, val modified: Date? = null, val accessed: Date? = null) { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
9 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
10 companion object { |
7 | 11 private val FIELD_MAP = mutableMapOf<String, KProperty.Getter<*>>().apply { |
12 Entry::class.declaredMemberProperties.forEach{ | |
13 this[it.name] = it.getter | |
14 } | |
15 } | |
16 | |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 fun withPromptedPassword() = Entry( |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
18 name = _getName(), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
19 username = _getUsername(), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
20 password = _getPassword(), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 notes = _getNotes() |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
22 ) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
23 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
24 fun withGeneratedPassword(length: Int, allowSymbols: Boolean, verbose: Boolean): Entry { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
25 return Entry( |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
26 name = _getName(), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
27 username = _getUsername(), |
2
3c792ad36b3d
Can now update a password and read it back.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
28 password = _genPassword(length, allowSymbols, verbose), |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
29 notes = _getNotes() |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
30 ) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
31 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
32 |
12 | 33 fun fromDatabase(db: Database, name: String): Entry? { |
34 db.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { | |
35 it.setLong(1, db.makeKey(name)) | |
36 val results = it.executeQuery() | |
37 if (!results.next()) { | |
38 return null | |
39 } | |
40 return Entry( | |
41 name = results.getDecryptedString(1, db.encryption)!!, | |
42 username = results.getDecryptedString(2, db.encryption)!!, | |
43 password = results.getDecrypted(3, db.encryption)!!, | |
44 notes = results.getDecryptedString(4, db.encryption), | |
45 created = results.getDate(5), | |
46 modified = results.getDate(6), | |
47 accessed = results.getDate(7) | |
48 ) | |
49 } | |
50 } | |
51 | |
2
3c792ad36b3d
Can now update a password and read it back.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
52 private fun _genPassword(length: Int, allowSymbols: Boolean, verbose: Boolean): CharArray { |
3c792ad36b3d
Can now update a password and read it back.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
53 val generated = generate(length, allowSymbols) |
3c792ad36b3d
Can now update a password and read it back.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
54 if (verbose) { |
3c792ad36b3d
Can now update a password and read it back.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
55 printPassword(generated) |
3c792ad36b3d
Can now update a password and read it back.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
56 } |
3c792ad36b3d
Can now update a password and read it back.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
57 return generated |
3c792ad36b3d
Can now update a password and read it back.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
58 } |
3c792ad36b3d
Can now update a password and read it back.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
59 |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
60 private fun _getName() = mustReadLine("Name of site: ") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
61 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
62 private fun _getUsername() = mustReadLine("Username: ") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
63 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
64 private fun _getPassword() = mustGetPassword("Password: ", verify = true) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
65 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
66 private fun _getNotes() = readLine("Notes: ") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
67 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
68 |
12 | 69 fun insert(db: Database) { |
70 db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)") | |
71 .use { | |
72 it.setLong(1, db.makeKey(name)) | |
73 it.setEncryptedString(2, name, db.encryption) | |
74 it.setEncryptedString(3, username, db.encryption) | |
75 it.setEncrypted(4, password, db.encryption) | |
76 it.setEncryptedString(5, notes, db.encryption) | |
77 it.setLongOrNull(6, created?.time) | |
78 it.setLongOrNull(7, modified?.time) | |
79 it.setLongOrNull(8, accessed?.time) | |
80 it.executeUpdate() | |
81 } | |
82 } | |
83 | |
84 fun update(db: Database) { | |
85 db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, created = ?, modified = ?, accessed = ? where id = ?").use { | |
86 it.setEncryptedString(1, name, db.encryption) | |
87 it.setEncryptedString(2, username, db.encryption) | |
88 it.setEncrypted(3, password, db.encryption) | |
89 it.setEncryptedString(4, notes, db.encryption) | |
90 it.setLongOrNull(5, created?.time) | |
91 it.setLongOrNull(6, modified?.time) | |
92 it.setLongOrNull(7, accessed?.time) | |
93 it.setLong(8, db.makeKey(name)) | |
94 it.executeUpdate() | |
95 } | |
96 } | |
97 | |
11 | 98 val modifiedOrCreated get() = modified ?: created!! |
99 | |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
100 fun print(redactPassword: String? = null) { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
101 println("Name of site: $name") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
102 println("Username: $username") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
103 if (redactPassword == null) { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
104 printPassword(password) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
105 } else { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
106 println("Password: $redactPassword") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
107 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
108 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
109 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
110 fun printLong(redactPassword: String? = null) { |
9 | 111 this.print(redactPassword) |
8 | 112 println("Notes: ${notes ?: "(none)"}") |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
113 printDate("Created", created) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
114 printDate("Modified", modified) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
115 printDate("Accessed", accessed) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
116 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
117 |
7 | 118 fun getField(name: String): Any? { |
119 return FIELD_MAP[name]!!.call(this) | |
120 } | |
121 | |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
122 private fun printDate(tag: String, date: Date?) { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
123 kotlin.io.print("${tag}: ") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
124 if (date == null) { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
125 println("never") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
126 } else { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
127 println(ISO8601.format(date)) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
128 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
129 } |
8 | 130 } |