-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Trust Chain Validation refactoring - Enabled JS in local-kms m…
…odule,
- Loading branch information
Zoe Maas
committed
Sep 26, 2024
1 parent
7d2ce75
commit cceb968
Showing
4 changed files
with
116 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
modules/local-kms/src/commonMain/sqldelight/com/sphereon/oid/fed/kms/local/models/1.sqm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
CREATE TABLE Keys ( | ||
id TEXT PRIMARY KEY, | ||
key TEXT NOT NULL, | ||
deleted_at TIMESTAMP | ||
deleted_at BIGINT | ||
); |
75 changes: 75 additions & 0 deletions
75
...ocal-kms/src/jsMain/kotlin/com/sphereon/oid/fed/kms/local/database/LocalKmsDatabase.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.sphereon.oid.fed.kms.local.database | ||
|
||
import app.cash.sqldelight.db.QueryResult | ||
import app.cash.sqldelight.db.SqlCursor | ||
import app.cash.sqldelight.db.SqlDriver | ||
import com.sphereon.oid.fed.kms.local.Constants | ||
import com.sphereon.oid.fed.kms.local.Database | ||
import com.sphereon.oid.fed.kms.local.models.Keys | ||
|
||
|
||
actual class LocalKmsDatabase { | ||
|
||
private var database: Database | ||
|
||
init { | ||
val driver = getDriver() | ||
runMigrations(driver) | ||
|
||
database = Database(driver) | ||
} | ||
|
||
private fun getDriver(): SqlDriver { | ||
return PlatformSqlDriver().createPostgresDriver( | ||
System.getenv(Constants.LOCAL_KMS_DATASOURCE_URL), | ||
System.getenv(Constants.LOCAL_KMS_DATASOURCE_USER), | ||
System.getenv(Constants.LOCAL_KMS_DATASOURCE_PASSWORD) | ||
) | ||
} | ||
|
||
private fun runMigrations(driver: SqlDriver) { | ||
setupSchemaVersioningTable(driver) | ||
|
||
val currentVersion = getCurrentDatabaseVersion(driver) | ||
val newVersion = Database.Schema.version | ||
|
||
if (currentVersion < newVersion) { | ||
Database.Schema.migrate(driver, currentVersion, newVersion) | ||
updateDatabaseVersion(driver, newVersion) | ||
} | ||
} | ||
|
||
private fun setupSchemaVersioningTable(driver: SqlDriver) { | ||
driver.execute(null, "CREATE TABLE IF NOT EXISTS schema_version (version INTEGER NOT NULL)", 0) | ||
} | ||
|
||
private fun getCurrentDatabaseVersion(driver: SqlDriver): Long { | ||
val versionQuery = "SELECT version FROM schema_version ORDER BY version DESC LIMIT 1" | ||
|
||
val version = driver.executeQuery(null, versionQuery, parameters = 0, mapper = { cursor: SqlCursor -> | ||
QueryResult.Value(if (cursor.next().value) cursor.getLong(0) else null) | ||
}) | ||
|
||
return version.value ?: 0 | ||
} | ||
|
||
private fun updateDatabaseVersion(driver: SqlDriver, newVersion: Long) { | ||
val updateQuery = "INSERT INTO schema_version (version) VALUES (?)" | ||
driver.execute(null, updateQuery, 1) { | ||
bindLong(0, newVersion) | ||
} | ||
} | ||
|
||
actual fun getKey(keyId: String): Keys { | ||
return database.keysQueries.findById(keyId).executeAsOneOrNull() | ||
?: throw KeyNotFoundException("$keyId not found") | ||
} | ||
|
||
actual fun insertKey(keyId: String, key: String) { | ||
database.keysQueries.create(keyId, key).executeAsOneOrNull() | ||
} | ||
|
||
actual fun deleteKey(keyId: String) { | ||
database.keysQueries.delete(keyId) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../local-kms/src/jsMain/kotlin/com/sphereon/oid/fed/kms/local/database/PlatformSqlDriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.sphereon.oid.fed.kms.local.database | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import com.sphereon.oid.fed.kms.local.Constants | ||
|
||
actual class PlatformSqlDriver { | ||
actual fun createPostgresDriver(url: String, username: String, password: String): SqlDriver { | ||
throw UnsupportedOperationException(Constants.POSTGRESQL_IS_NOT_SUPPORTED_IN_JS) as Throwable | ||
} | ||
|
||
actual fun createSqliteDriver(path: String): SqlDriver { | ||
throw UnsupportedOperationException(Constants.SQLITE_IS_NOT_SUPPORTED_IN_JS) | ||
} | ||
} |