-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #22 - [publish] - Implement a DB secretResolver
- Refactor code - Create a common interface to implement diverse secret resolvers - Make DIDPeer a class
- Loading branch information
1 parent
8d5de7c
commit 6177166
Showing
12 changed files
with
256 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ permissions: | |
|
||
jobs: | ||
build: | ||
|
||
name: Compile and Test Code | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
|
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
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,3 +1,3 @@ | ||
version=2.0.2-SNAPSHOT | ||
version=2.0.3-SNAPSHOT | ||
|
||
kotlin.code.style=official |
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
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/rootsid/wal/library/didcom/model/DidComSecret.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,11 @@ | ||
package com.rootsid.wal.library.didcom.model | ||
|
||
import kotlinx.serialization.Contextual | ||
|
||
interface DidComSecret { | ||
val _id: String | ||
val secret: Map<String, @Contextual Any> | ||
} | ||
|
||
|
||
|
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/rootsid/wal/library/didcom/storage/DidComSecretStorage.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,15 @@ | ||
package com.rootsid.wal.library.didcom.storage | ||
|
||
import com.rootsid.wal.library.didcom.model.DidComSecret | ||
|
||
interface DidComSecretStorage { | ||
fun insert(kid: String, secretJson: Map<String, Any>): DidComSecret | ||
|
||
fun findById(kid: String): DidComSecret | ||
|
||
fun findIdsIn(kids: List<String>): Set<String> | ||
|
||
fun listIds(): List<String> | ||
|
||
fun list(): List<DidComSecret> | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/main/kotlin/com/rootsid/wal/library/didcom/storage/SecretResolverFileSystemStorage.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,66 @@ | ||
package com.rootsid.wal.library.didcom.storage | ||
|
||
//import com.rootsid.wal.library.didcom.model.createAnonymousObj | ||
import com.rootsid.wal.library.didcom.model.DidComSecret | ||
import kotlinx.serialization.Contextual | ||
import org.didcommx.didcomm.secret.Secret | ||
import org.didcommx.didcomm.secret.jwkToSecret | ||
import org.didcommx.didcomm.secret.secretToJwk | ||
import org.didcommx.didcomm.utils.fromJsonToList | ||
import org.didcommx.didcomm.utils.toJson | ||
import java.io.File | ||
import kotlin.collections.set | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.exists | ||
|
||
class SecretResolverFileSystemStorage(private val filePath: String = "secrets.json") : DidComSecretStorage { | ||
private val secrets: MutableMap<String, Secret> | ||
|
||
init { | ||
if (!Path(filePath).exists()) { | ||
secrets = mutableMapOf() | ||
save() | ||
} else { | ||
val secretsJson = File(filePath).readText() | ||
secrets = if (secretsJson.isNotEmpty()) { | ||
fromJsonToList(secretsJson).map { jwkToSecret(it) }.associate { it.kid to it }.toMutableMap() | ||
} else { | ||
mutableMapOf() | ||
} | ||
} | ||
} | ||
|
||
override fun insert(kid: String, secretJson: Map<String, Any>): DidComSecret { | ||
secrets[kid] = jwkToSecret(secretJson) | ||
save() | ||
|
||
return createAnonymousDidComSecret(kid, secretToJwk(secrets[kid]!!)) | ||
} | ||
|
||
private fun save() { | ||
val secretJson = toJson(secrets.values.map { secretToJwk(it) }) | ||
File(filePath).writeText(secretJson) | ||
} | ||
|
||
private fun createAnonymousDidComSecret(id: String, secret: Map<String, @Contextual Any>): DidComSecret = | ||
object : DidComSecret { | ||
override val _id: String | ||
get() = id | ||
override val secret: Map<String, Any> | ||
get() = secret | ||
} | ||
|
||
override fun findById(kid: String): DidComSecret { | ||
secrets[kid]?.let { | ||
return createAnonymousDidComSecret(kid, secretToJwk(it)) | ||
} | ||
|
||
throw RuntimeException("Secret '$kid' not found.") | ||
} | ||
|
||
override fun findIdsIn(kids: List<String>): Set<String> = kids.intersect(secrets.keys) | ||
|
||
override fun listIds(): List<String> = secrets.keys.toList() | ||
|
||
override fun list(): List<DidComSecret> = secrets.entries.map { createAnonymousDidComSecret(it.key, secretToJwk(it.value)) } | ||
} |
Oops, something went wrong.