From 132a8913aa9256342f11323992cc30e9c9715409 Mon Sep 17 00:00:00 2001 From: Infendro Date: Sun, 17 Aug 2025 15:40:26 +0200 Subject: [PATCH] refactor migration --- backend/gradle/libs.versions.toml | 2 +- .../com/infendro/account/model/Migration.kt | 102 +++++++++--------- .../kotlin/com/infendro/account/util/OTP.kt | 2 +- 3 files changed, 52 insertions(+), 54 deletions(-) diff --git a/backend/gradle/libs.versions.toml b/backend/gradle/libs.versions.toml index 449917f..fa9dd82 100644 --- a/backend/gradle/libs.versions.toml +++ b/backend/gradle/libs.versions.toml @@ -6,7 +6,7 @@ postgres = "42.7.5" koin = "4.0.3" random = "1.0.0" otp = "1.1.1" -kdf = "1.0.1" +kdf = "1.0.3" hash = "1.2.2" encoding = "1.1.0" logback = "1.5.18" diff --git a/backend/src/main/kotlin/com/infendro/account/model/Migration.kt b/backend/src/main/kotlin/com/infendro/account/model/Migration.kt index f443e70..df8b527 100644 --- a/backend/src/main/kotlin/com/infendro/account/model/Migration.kt +++ b/backend/src/main/kotlin/com/infendro/account/model/Migration.kt @@ -5,7 +5,6 @@ import com.infendro.account.model.repository.AccountRepository import com.infendro.account.model.repository.MigrationRepository import com.infendro.account.util.SecureHasher import io.ktor.server.application.* -import org.intellij.lang.annotations.Language import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.Transaction @@ -17,48 +16,48 @@ fun Application.migrate() { SchemaUtils.create(MigrationTable) } - migration( - "initial", - """ - CREATE TABLE access - ( - id BIGSERIAL - PRIMARY KEY, - token_hash TEXT - NOT NULL - ); - - CREATE TABLE account - ( - id BIGSERIAL - PRIMARY KEY, - username TEXT - UNIQUE - NOT NULL, - password_hash TEXT - NOT NULL, - password_salt TEXT - NOT NULL, - secret TEXT - NOT NULL, - role TEXT - NOT NULL - ); - - CREATE TABLE session - ( - id BIGSERIAL - PRIMARY KEY, - token_hash TEXT - NOT NULL, - account_id BIGINT - REFERENCES account (id) ON DELETE CASCADE - NOT NULL - ); - """.trimIndent() - ) - migration("owner") { - val accounts by inject() + val accounts by inject() + + migration("initial") { + exec( + """ + CREATE TABLE access + ( + id BIGSERIAL + PRIMARY KEY, + token_hash TEXT + NOT NULL + ); + + CREATE TABLE account + ( + id BIGSERIAL + PRIMARY KEY, + username TEXT + UNIQUE + NOT NULL, + password_hash TEXT + NOT NULL, + password_salt TEXT + NOT NULL, + secret TEXT + NOT NULL, + role TEXT + NOT NULL + ); + + CREATE TABLE session + ( + id BIGSERIAL + PRIMARY KEY, + token_hash TEXT + NOT NULL, + account_id BIGINT + REFERENCES account (id) ON DELETE CASCADE + NOT NULL + ); + """.trimIndent() + ) accounts.insert { val salt = SecureHasher.generateSalt() @@ -66,7 +65,7 @@ fun Application.migrate() { this.username = "infendro" this.passwordHash = SecureHasher.hash("password", salt) this.passwordSalt = salt - this.secret = "deadbeef" + this.secret = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA====" this.role = Role.OWNER } } @@ -83,17 +82,16 @@ private fun Application.migration( ?.run { return } transaction { - block() + try { + block() + log.info("""Migration "$name" successful""") + } catch (e: Exception) { + log.error("""Migration "$name" failed""") + throw e + } } migrations.insert { this.name = name } } - -private fun Application.migration( - name: String, - @Language("sql") sql: String, -) = migration(name) { - exec(sql) -} diff --git a/backend/src/main/kotlin/com/infendro/account/util/OTP.kt b/backend/src/main/kotlin/com/infendro/account/util/OTP.kt index 0d38f24..d6b8e88 100644 --- a/backend/src/main/kotlin/com/infendro/account/util/OTP.kt +++ b/backend/src/main/kotlin/com/infendro/account/util/OTP.kt @@ -24,6 +24,6 @@ object OTP { fun generateSecret(): String { val bytes = SecretGenerator.generate(totp.hotp.function) - return Base32.encode(bytes).toString() + return Base32.encode(bytes).decodeToString() } }