Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
ktlint: Format
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Oct 29, 2023
1 parent 2588940 commit ae831c0
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 104 deletions.
39 changes: 23 additions & 16 deletions src/main/kotlin/dev/medzik/librepass/desktop/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*

object Config {
val workDir = File(getWorkDir("librepass"))

Expand All @@ -31,21 +32,27 @@ object Config {

private fun getWorkDir(name: String): String {
val osname = System.getProperty("os.name").lowercase(Locale.getDefault())
if (osname.startsWith("windows")) return Paths.get(System.getenv("APPDATA"), name)
.toString() else if (osname.contains("nux") || osname.contains("freebsd")) return Paths.get(
System.getProperty("user.home"),
".config",
name
).toString() else if (osname.contains("mac") || osname.contains("darwin")) return Paths.get(
System.getProperty("user.home"),
"Library",
"Application Support",
name
).toString()
if (osname.startsWith("windows"))
return Paths.get(System.getenv("APPDATA"), name)
.toString() else if (osname.contains("nux") || osname.contains("freebsd"))
return Paths.get(
System.getProperty("user.home"),
".config",
name
).toString() else if (osname.contains("mac") || osname.contains("darwin"))
return Paths.get(
System.getProperty("user.home"),
"Library",
"Application Support",
name
).toString()
return ""
}

fun writeObject(name: String, obj: Any): Any {
fun writeObject(
name: String,
obj: Any
): Any {
val file = File(workDir, "$name.json")
val serialized = Gson().toJson(obj)

Expand All @@ -60,7 +67,10 @@ object Config {
return Gson().fromJson(json, object : TypeToken<T>() {}.type)
}

inline fun <reified T : Any> overrideObject(name: String, obj: (T) -> T): T {
inline fun <reified T : Any> overrideObject(
name: String,
obj: (T) -> T
): T {
val readed = readObject<T>(name)
val ret = obj.invoke(readed)
writeObject(name, ret)
Expand All @@ -79,13 +89,10 @@ object Config {
data class Credentials(
val userId: UUID,
val email: String,

val apiUrl: String? = null,
val apiKey: String,
val publicKey: String,

val lastSync: Long? = null,

// argon2id parameters
val memory: Int,
val iterations: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package dev.medzik.librepass.desktop.gui

open class Controller {
open fun onStart() {}

open fun onStop() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class WelcomeController : Controller() {

@FXML
private fun initialize() {
fadeInTransition = FadeTransition(Duration.seconds(1.6), logo).apply {
fromValue = 0.0
toValue = 1.0
}
fadeInTransition =
FadeTransition(Duration.seconds(1.6), logo).apply {
fromValue = 0.0
toValue = 1.0
}

// reading theme
val theme = Config.readObject<Settings>("settings").theme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import javafx.scene.control.TextField
import java.util.concurrent.CompletableFuture

class LoginController : Controller() {

@FXML
private lateinit var email: TextField

Expand All @@ -34,50 +33,56 @@ class LoginController : Controller() {

@FXML
private fun initialize() {
val listener = ChangeListener<String> { _, _, _ ->
login.isDisable = !email.text.contains("@") || password.text.isEmpty()
}
val listener =
ChangeListener<String> { _, _, _ ->
login.isDisable = !email.text.contains("@") || password.text.isEmpty()
}
email.textProperty().addListener(listener)
password.textProperty().addListener(listener)
login.isDisable = true
}

@FXML
fun onBack() =
StateManager.setState(State.WELCOME)
fun onBack() = StateManager.setState(State.WELCOME)

@FXML
fun onLogin() {
submit(email.text, password.text)
}

private fun submit(email: String, password: String) = CompletableFuture.runAsync {
private fun submit(
email: String,
password: String
) = CompletableFuture.runAsync {
login.isDisable = true
try {
val preLogin = authClient.preLogin(email)

// authenticate user and get credentials
val loginCredentials = authClient.login(
email = email,
password = password
)

val credentials = Credentials(
userId = loginCredentials.userId,
email = email,
apiKey = loginCredentials.apiKey,
publicKey = Hex.encode(loginCredentials.publicKey),
// Argon2id parameters
memory = preLogin.memory,
iterations = preLogin.iterations,
parallelism = preLogin.parallelism
)
val loginCredentials =
authClient.login(
email = email,
password = password
)

val credentials =
Credentials(
userId = loginCredentials.userId,
email = email,
apiKey = loginCredentials.apiKey,
publicKey = Hex.encode(loginCredentials.publicKey),
// Argon2id parameters
memory = preLogin.memory,
iterations = preLogin.iterations,
parallelism = preLogin.parallelism
)
Config.writeObject("credentials", credentials)

val userSecrets = UserSecrets(
privateKey = Hex.encode(loginCredentials.privateKey),
secretKey = loginCredentials.secretKey
)
val userSecrets =
UserSecrets(
privateKey = Hex.encode(loginCredentials.privateKey),
secretKey = loginCredentials.secretKey
)
Config.writeObject("user_secrets", userSecrets)

// Perform GC to flush a lot of memory allocated by argon2 generation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import javafx.scene.control.TextField
import java.util.concurrent.CompletableFuture

class RegisterController {

@FXML
private lateinit var email: TextField

Expand All @@ -33,9 +32,10 @@ class RegisterController {

@FXML
private fun initialize() {
val listener = ChangeListener<String> { _, _, _ ->
register.isDisable = !email.text.contains("@") || password.text.isEmpty() || password.text != retypePassword.text
}
val listener =
ChangeListener<String> { _, _, _ ->
register.isDisable = !email.text.contains("@") || password.text.isEmpty() || password.text != retypePassword.text
}
email.textProperty().addListener(listener)
password.textProperty().addListener(listener)
retypePassword.textProperty().addListener(listener)
Expand All @@ -52,7 +52,11 @@ class RegisterController {
hint.clear()
}

private fun submit(email: String, password: String, passwordHint: String) {
private fun submit(
email: String,
password: String,
passwordHint: String
) {
CompletableFuture.runAsync {
authClient.register(email, password, passwordHint)
Utils.dialog("Registered!", "Registered!", Alert.AlertType.INFORMATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class CipherListItem : ListCell<Cipher>() {
}
}

override fun updateItem(cipher: Cipher?, empty: Boolean) {
override fun updateItem(
cipher: Cipher?,
empty: Boolean
) {
super.updateItem(cipher, empty)
if (empty) {
graphic = null
Expand Down Expand Up @@ -77,9 +80,10 @@ class CipherListItem : ListCell<Cipher>() {
private fun updateIcon(cipher: Cipher) {
val urls = cipher.loginData?.uris

icon.image = if (!urls.isNullOrEmpty())
getIcon(CipherClient.getFavicon(domain = urls[0]))
else
userIcon
icon.image =
if (!urls.isNullOrEmpty())
getIcon(CipherClient.getFavicon(domain = urls[0]))
else
userIcon
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ private const val BULLET = '\u2022'

// TODO: Split into components
class CipherView : AnchorPane() {

@FXML
private lateinit var name: Label

Expand Down Expand Up @@ -60,13 +59,18 @@ class CipherView : AnchorPane() {
passwordToggleShow.isSelected = false
}

private fun setPassword(text: String?, mask: Boolean) {
private fun setPassword(
text: String?,
mask: Boolean
) {
password.text = ""
if (!text.isNullOrEmpty()) {
if (mask) {
for (i in text)
password.text += BULLET
} else password.text = text
} else {
password.text = text
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ class DashboardController : Controller() {
cipherList.items = list
cipherList.setCellFactory { CipherListItem() }
cipherList.selectionModel.selectedItemProperty().addListener { _, _, cipher ->
rootBorderPane.center = if (cipher != null) {
cipherView.setCipher(cipher)
cipherView
} else emptyView
rootBorderPane.center =
if (cipher != null) {
cipherView.setCipher(cipher)
cipherView
} else {
emptyView
}
}

rootBorderPane.center = emptyView
Expand All @@ -69,28 +72,32 @@ class DashboardController : Controller() {
// get ciphers from local repository and update UI
// TODO: Add more database like storage
private fun updateLocalCiphers() {
val ciphers = if (Config.isObjectExists("ciphers"))
Config.readObject<List<StoreCipher>>("ciphers")
else {
val list = mutableListOf<StoreCipher>()
Config.writeObject("ciphers", list)
list
}
val ciphers =
if (Config.isObjectExists("ciphers")) {
Config.readObject<List<StoreCipher>>("ciphers")
} else {
val list = mutableListOf<StoreCipher>()
Config.writeObject("ciphers", list)
list
}

val decryptedCiphers = ciphers.map {
try {
Cipher(it.encryptedCipher, userSecrets.secretKey)
} catch (e: Exception) { // TODO
Cipher(
id = it.encryptedCipher.id,
owner = it.owner,
type = CipherType.Login,
loginData = CipherLoginData(
name = "Encryption error"
val decryptedCiphers =
ciphers.map {
try {
Cipher(it.encryptedCipher, userSecrets.secretKey)
} catch (e: Exception) {
// TODO
Cipher(
id = it.encryptedCipher.id,
owner = it.owner,
type = CipherType.Login,
loginData =
CipherLoginData(
name = "Encryption error"
)
)
)
}
}
}
Platform.runLater {
list.clear()
// sort ciphers by name and update UI
Expand Down Expand Up @@ -155,9 +162,9 @@ class DashboardController : Controller() {
}
resetStatus()
} catch (e: Exception) {
if (e is ClientException)
if (e is ClientException) {
setStatus(tr("status.error.network"))
else {
} else {
setStatus("Error: ${e.message}")
throw RuntimeException(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.medzik.librepass.desktop.locale
import java.util.*

object LangManager {

private lateinit var currentLocale: ResourceBundle

fun init(): ResourceBundle {
Expand Down
Loading

0 comments on commit ae831c0

Please sign in to comment.