Skip to content
Browse files

done

  • Loading branch information...
1 parent 5cce529 commit f3dad2c16bf86ce0925dd7572ff20f8ff645ef95 @Frostman committed May 1, 2012
Showing with 121 additions and 87 deletions.
  1. +55 −45 problem_1/src/auth/Auth.kt
  2. +11 −8 problem_1/src/ui/EditableTable.kt
  3. +55 −34 problem_1/src/ui/GUI.kt
View
100 problem_1/src/auth/Auth.kt
@@ -10,16 +10,16 @@ import java.util.List
import java.util.Map
import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.binary.StringUtils
+import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
import ui.et.Column
import ui.et.StringValue
import ui.et.Value
-import java.text.NumberFormat
class AuthDb(val dbPath : String) {
// login -> user
public val users : Map<String, User> = LinkedHashMap<String, User>()
- public val confs : Map<String, String> = LinkedHashMap<String, String>()
+ public val infos : List<Info> = LinkedList<Info>()
fun load(val encrypted : Boolean = false) {
if(encrypted) {
@@ -29,19 +29,21 @@ class AuthDb(val dbPath : String) {
users.clear()
for (line in FileUtils.readLines(File(dbPath))) {
val split = line.sure().split(" ").sure()
- if(split.size == 5) {
+ if(split.size == 4) {
val login = split[0].sure().fromB64()
val password = split[1].sure().fromB64()
- val role = if (Integer.parseInt(split[2]) == 0) Role.ADMIN else Role.USER
- val lastModified = Long.parseLong(split[3]!!)
- val strange = split[4]!!.fromB64()
-
- users.put(login, User(login, password, role, lastModified, strange))
- } else if(split.size == 2) {
- val key = split[0]!!.fromB64()
- val value = split[1]!!.fromB64()
-
- confs.put(key, value)
+ val role = if (Integer.parseInt(split[2]) == 0) Role.ADMIN else if (Integer.parseInt(split[2]) == 1) Role.EDITOR else Role.USER
+ val lastModified = Long.parseLong(split[3])
+
+ users.put(login, User(login, password, role, lastModified))
+ } else if(split.size == 5) {
+ val name = split[0].fromB64()
+ val middleName = split[1].fromB64()
+ val lastName = split[2].fromB64()
+ val phone = split[3].fromB64()
+ val address = split[4].fromB64()
+
+ infos.add(Info(name, middleName, lastName, phone, address))
}
}
@@ -53,7 +55,7 @@ class AuthDb(val dbPath : String) {
fun save(val encrypt : Boolean = false) {
val list = ArrayList<Any?>()
list.addAll(users.b64Values())
- list.addAll(confs.b64Values2())
+ list.addAll(infos.b64Values2())
FileUtils.writeLines(File(dbPath), list)
if (encrypt) {
encryptFile(ui.key, File(dbPath))
@@ -63,56 +65,68 @@ class AuthDb(val dbPath : String) {
enum class Role {
ADMIN
+ EDITOR
USER
}
-class User(val login : String, val password : String, val role : Role, val lastModified : Long, val passwordStrange : String) {
+class User(val login : String, val password : String, val role : Role, val lastModified : Long) {
//todo think about password hashing
- fun toString() = "${login} ${password} ${if (role == Role.ADMIN) 0 else 1} $lastModified strange=${passwordStrange}"
+ fun toString() = "${login} ${password} ${if (role == Role.ADMIN) 0 else if (role == Role.EDITOR) 1 else 2} $lastModified"
- fun asB64String() = "${login.toB64()} ${password.toB64()} ${if (role == Role.ADMIN) 0 else 1} $lastModified ${passwordStrange.toB64()}"
+ fun asB64String() = "${login.toB64()} ${password.toB64()} ${if (role == Role.ADMIN) 0 else if (role == Role.EDITOR) 1 else 2} $lastModified"
fun asColumns() : List<Value> {
val columns = ArrayList<Value>()
columns.add(StringValue(login))
columns.add(StringValue(password))
- columns.add(StringValue(if (role == Role.ADMIN) "admin" else "user"))
+ columns.add(StringValue(if (role == Role.ADMIN) "admin" else if (role == Role.EDITOR) "editor" else "user"))
columns.add(StringValue(lastModified.toString()))
- columns.add(StringValue(passwordStrange))
return columns
}
class object {
public val columns : List<Column> = arrayList(Column("login"), object : Column("password", true){
- public override fun onChanged(val value : String) : List<#(Int, Any?)>? = arrayList(#(3, StringValue(System.currentTimeMillis().toString())), #(4, StringValue(calcPasswordStrange(value))))
- }, Column("role"), Column("lastModified"), Column("Strange", false));
+ public override fun onChanged(val value : String) : List<#(Int, Any?)>? = arrayList(#(1, StringValue(value.sha())))
+ }, Column("role"), Column("lastModified"));
public fun fromColumns(val it : List<Value>) : User = User(
(it[0] as StringValue).str,
(it[1] as StringValue).str,
- if(it[2] is StringValue && (it[2] as StringValue).str.equals("admin")) Role.ADMIN else Role.USER,
- (it[3] as StringValue).str.toLong(),
- (it[4] as StringValue).str
+ if(it[2] is StringValue && (it[2] as StringValue).str.equals("admin")) Role.ADMIN else
+ (if(it[2] is StringValue && (it[2] as StringValue).str.equals("editor")) Role.EDITOR else Role.USER),
+ (it[3] as StringValue).str.toLong()
)
+ }
+}
- public fun calcPasswordStrange(val password : String) : String {
- val db = AuthDb("database")
- db.load(true)
+class Info (val name : String, val middleName : String, val lastName : String, val phone : String, val address : String) {
+ fun asB64String() = "${name.toB64()} ${middleName.toB64()} ${lastName.toB64()} ${phone.toB64()} ${address.toB64()}"
- val bruteForceSpeed = db.confs.get("password bruteforce speed")!!.toDouble()
- val maxLifeTime = db.confs.get("max password life time")!!.toDouble()
- val alphabetSize = db.confs.get("alphabet size")!!.toDouble()
- val passwordSpace = Math.pow(alphabetSize, password.length().toDouble())
- val value = Math.min(bruteForceSpeed * maxLifeTime / passwordSpace, 100.toDouble())
- val format = NumberFormat.getInstance()!!
- format.setMaximumFractionDigits(4)
+ fun asColumns() : List<Value> = arrayList(
+ StringValue(name),
+ StringValue(middleName),
+ StringValue(lastName),
+ StringValue(phone),
+ StringValue(address)
+ )
+
+ class object {
+ public val editableColumns : List<Column> = arrayList(Column("name"), Column("middle name"), Column("last name"), Column("phone"), Column("address"))
+
+ public val columns : List<Column> = arrayList(Column("name", false), Column("middle name", false), Column("last name", false), Column("phone", false), Column("address", false))
+
+ public fun fromColumns(val it : List<Value>) : Info = Info(
+ (it[0] as StringValue).str,
+ (it[1] as StringValue).str,
+ (it[2] as StringValue).str,
+ (it[3] as StringValue).str,
+ (it[4] as StringValue).str
+ )
- return "${format.format(100 - value)}%"
- }
}
}
@@ -126,11 +140,11 @@ fun Map<String, User>.b64Values() : List<String> {
return result
}
-fun Map<String, String>.b64Values2() : List<String> {
+fun List<Info>.b64Values2() : List<String> {
val result = LinkedList<String>()
- for (pair in this.entrySet()) {
- result.add(pair.getKey().toB64() + " " + pair.getValue().toB64())
+ for (entry in this) {
+ result.add(entry.asB64String())
}
return result
@@ -143,7 +157,7 @@ fun String.fromB64() = StringUtils.newStringUtf8(Base64.decodeBase64(this.getByt
fun checkCredentials(val login : String, val password : String, val key : String = "test key", val dbPath : String = "database") : Boolean {
val db = AuthDb(dbPath)
db.load(true)
- val result = db.users.get(login)?.password.equals(password)
+ val result = db.users.get(login)?.password.equals(password.sha())
if (db.users.size == 0) {
return true
@@ -159,8 +173,4 @@ fun getUser(val login : String, val dbPath : String = "database") : User? {
return db.users.get(login)
}
-fun main(args : Array<String>) {
- val db = AuthDb("database")
- db.users.put("admin", User("admin", "admin", Role.ADMIN, 0, "0%"))
- db.save(true)
-}
+fun String.sha() = DigestUtils.sha256Hex(this)!!
View
19 problem_1/src/ui/EditableTable.kt
@@ -40,22 +40,25 @@ class EditableTable(val columns : List<Column>) {
public override fun getColumnName(column : Int) = columns[column].name
public override fun setValueAt(newValue : Any?, row : Int, column : Int) {
+ var changed : List<#(Int, Any?)>?;
if(data[row][column] is StringValue && !(data[row][column] as StringValue).str.equals(newValue)) {
- val changed = columns[column].onChanged(if(newValue is StringValue) newValue.str else newValue as String)
+ changed = columns[column].onChanged(if(newValue is StringValue) newValue.str else newValue as String)
+
+ data[row][column] = if (newValue is String) StringValue(newValue) else newValue as Value
+ fireTableCellUpdated(row, column)
+
if(changed != null) {
for(change in changed) {
- setValueAt(change._2, row, change._1)
+ data[row][change._1] = if (change._2 is String) StringValue(change._2 as String) else change._2 as Value
+ fireTableCellUpdated(row, change._1)
}
}
+ } else {
+ data[row][column] = if (newValue is String) StringValue(newValue) else newValue as Value
+ fireTableCellUpdated(row, column)
}
- data[row][column] = if (newValue is String) StringValue(newValue) else newValue as Value
- fireTableCellUpdated(row, column)
}
})
- // var idx = 0
- // for (column in columns) {
- // table.getColumnModel()!!.getColumn(idx++)!!.setCellEditor(column.editor)
- // }
}
fun <T> getObjects(transform : List<Value>.() -> T) = data.map{it.transform()}
View
89 problem_1/src/ui/GUI.kt
@@ -1,6 +1,7 @@
package ui
import auth.AuthDb
+import auth.Info
import auth.Role
import auth.User
import auth.checkCredentials
@@ -14,34 +15,29 @@ import java.awt.event.FocusListener
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.io.File
-import java.util.ArrayList
-import java.util.Date
-import java.util.List
-import java.util.Map
import javax.swing.JButton
-import javax.swing.JCheckBox
import javax.swing.JDialog
import javax.swing.JFileChooser
import javax.swing.JFrame
import javax.swing.JLabel
-import javax.swing.JOptionPane
import javax.swing.JPasswordField
import javax.swing.JTextField
import javax.swing.WindowConstants
import javax.swing.text.JTextComponent
-import ui.et.Column
+import org.apache.commons.codec.digest.DigestUtils
import ui.et.EditableTable
-import ui.et.StringValue
-import ui.et.Value
public val key : String = "test key"
+
+fun String.sha() = DigestUtils.sha256Hex(this)!!
+
fun main(args : Array<String>) {
val dbPath = "database"
if(!File(dbPath).exists()) {
val db = AuthDb("database")
- db.users.put("admin", User("admin", "admin", Role.ADMIN, 0, "0%"))
+ db.users.put("admin", User("admin", "admin".sha(), Role.ADMIN, 0))
db.save()
encryptFile(key, File("database"))
@@ -176,7 +172,7 @@ fun showVariantsWindow(val parent : Frame? = null, val username : String) {
loginField.setEditable(false)
dialog.getContentPane()?.add(loginField)
- val roleField = JTextField("role: " + if (user.role == Role.ADMIN) "admin" else "user")
+ val roleField = JTextField("role: " + if (user.role == Role.ADMIN) "admin" else if (user.role == Role.EDITOR) "editor" else "user")
roleField.setBounds(60, 44, 200, 24)
roleField.setEditable(false)
dialog.getContentPane()?.add(roleField)
@@ -188,12 +184,9 @@ fun showVariantsWindow(val parent : Frame? = null, val username : String) {
}
dialog.getContentPane()?.add(usersButton)
- val confButton = JButton("Config")
- confButton.setBounds(60, 108, 80, 25)
- if(user.role != Role.ADMIN) {
- confButton.setEnabled(false)
- }
- dialog.getContentPane()?.add(confButton)
+ val infoButton = JButton("Info")
+ infoButton.setBounds(60, 108, 80, 25)
+ dialog.getContentPane()?.add(infoButton)
val exitButton = JButton("Exit")
exitButton.setBounds(60, 138, 80, 25)
@@ -217,11 +210,9 @@ fun showVariantsWindow(val parent : Frame? = null, val username : String) {
}
}
- confButton -> {
- if(user.role == Role.ADMIN) {
- showConfWindow(username)
- dialog.dispose()
- }
+ infoButton -> {
+ showInfoWindow(username)
+ dialog.dispose()
}
exitButton -> {
@@ -254,7 +245,7 @@ fun showVariantsWindow(val parent : Frame? = null, val username : String) {
}
usersButton.addMouseListener(clickHandler)
- confButton.addMouseListener(clickHandler)
+ infoButton.addMouseListener(clickHandler)
exitButton.addMouseListener(clickHandler)
encryptButton.addMouseListener(clickHandler)
decryptButton.addMouseListener(clickHandler)
@@ -310,7 +301,7 @@ fun showUsersListWindow(val username : String) {
}
addButton -> {
- table.data.add(User("NAME", "PASSWORD", Role.USER, System.currentTimeMillis(), "0%").asColumns())
+ table.data.add(User("NAME", "PASSWORD", Role.USER, System.currentTimeMillis()).asColumns())
table.data = table.data
}
@@ -331,35 +322,54 @@ fun showUsersListWindow(val username : String) {
frame.setVisible(true)
}
-fun showConfWindow(val username : String) {
+fun showInfoWindow(val username : String) {
+ val user = auth.getUser(username)!!
+
val frame = JFrame("Configuration")
frame.setLayout(null)
frame.setBounds(100, 100, 600, 650)
frame.setResizable(false)
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
- val table = EditableTable(confColumns)
+ val table = EditableTable(if (user.role == Role.USER) Info.columns else Info.editableColumns )
frame.add(table.getScrollPane(10, 10, 580, 560))
val db = AuthDb("database")
db.load(true)
- table.data = db.confs.entrySet().map{it.asColumns()}
+ table.data = db.infos.map{it.asColumns()}
val saveButton = JButton("Save")
saveButton.setBounds(10, 590, 100, 30)
+ if (user.role == Role.USER) {
+ saveButton.setEnabled(false)
+ }
frame.add(saveButton)
val closeButton = JButton("Menu")
closeButton.setBounds(150, 590, 100, 30)
frame.add(closeButton)
+ val addButton = JButton("Add")
+ addButton.setBounds(350, 590, 100, 30)
+ if (user.role == Role.USER) {
+ addButton.setEnabled(false)
+ }
+ frame.add(addButton)
+
+ val remButton = JButton("Remove")
+ remButton.setBounds(490, 590, 100, 30)
+ if (user.role == Role.USER) {
+ remButton.setEnabled(false)
+ }
+ frame.add(remButton)
+
val clickHandler = object : MouseAdapter() {
public override fun mouseClicked(e : MouseEvent?) {
when (e?.getSource()) {
saveButton -> {
- db.confs.clear()
- for (pair in table.getObjects{this}) {
- db.confs.put((pair[0] as StringValue).str, (pair[1] as StringValue).str)
+ db.infos.clear()
+ for (entry in table.getObjects{this}) {
+ db.infos.add(Info.fromColumns(entry))
}
db.save(true)
}
@@ -370,17 +380,28 @@ fun showConfWindow(val username : String) {
showVariantsWindow(null, username)
}
+ addButton -> {
+ if(user.role != Role.USER) {
+ table.data.add(Info("NAME", "MIDDLE", "LAST", "PHONE", "ADDRESS").asColumns())
+ table.data = table.data
+ }
+ }
+
+ remButton -> {
+ if(user.role != Role.USER) {
+ table.removeSelected()
+ }
+ }
+
else -> println("unknown click source")
}
}
}
saveButton.addMouseListener(clickHandler)
closeButton.addMouseListener(clickHandler)
+ addButton.addMouseListener(clickHandler)
+ remButton.addMouseListener(clickHandler)
frame.setVisible(true)
}
-
-fun Map.Entry<String, String>.asColumns() : List<Value> = arrayList(StringValue(this.getKey()), StringValue(this.getValue()))
-
-val confColumns = arrayList(Column("Key", false), Column("Value"))

0 comments on commit f3dad2c

Please sign in to comment.
Something went wrong with that request. Please try again.