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

Commit

Permalink
fix: use db internal ids for credentials reference
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplotean committed Nov 23, 2023
1 parent a1e314d commit 5f13402
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 34 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/id/walt/service/Credential.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package id.walt.service

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject

@Serializable
data class Credential(
val id: String,
val parsedCredential: JsonObject,
val rawCredential: String
)
4 changes: 2 additions & 2 deletions src/main/kotlin/id/walt/service/SSIKit2WalletService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) {
Base64.UrlSafe.decode(cred.split(".")[1]).decodeToString()
).jsonObject["vc"]!!.jsonObject
else throw IllegalArgumentException("Unknown credential format")
Credential(parsedCred, cred)
Credential(it.id.toString(), parsedCred, cred)
}.onFailure { it.printStackTrace() }.getOrNull()?.let { cred ->
Credential(JsonObject(cred.parsedCredential.toMutableMap().also {
Credential(it.id.toString(), JsonObject(cred.parsedCredential.toMutableMap().also {
it.putIfAbsent("id", JsonPrimitive(credentialId))
}), cred.rawCredential)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/id/walt/service/WalletKitWalletService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class WalletKitWalletService(accountId: UUID) : WalletService(accountId) {
/* Credentials */

override suspend fun listCredentials() = authenticatedJsonGet("/api/wallet/credentials/list")
.body<JsonObject>()["list"]!!.jsonArray.toList().map { Credential(it.jsonObject, it.toString()) }
.body<JsonObject>()["list"]!!.jsonArray.toList().map { Credential("null", it.jsonObject, it.toString()) }

override suspend fun listRawCredentials(): List<String> {
TODO("Not yet implemented")
Expand Down
42 changes: 23 additions & 19 deletions src/main/kotlin/id/walt/service/credentials/CredentialsService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import id.walt.db.repositories.AccountCredentialsRepository
import id.walt.db.repositories.CredentialsRepository
import id.walt.db.repositories.DbAccountCredentials
import id.walt.db.repositories.DbCredential
import id.walt.service.dids.DidUpdateDataObject
import org.jetbrains.exposed.sql.innerJoin
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import java.net.URLEncoder
import java.util.*

//TODO: replace DbCredential with a dto
object CredentialsService {
fun get(account: UUID, credentialId: String): DbCredential? =
list(account).singleOrNull { it.credentialId == credentialId }
fun get(account: UUID, id: String): DbCredential? =
list(account).singleOrNull { it.id == UUID.fromString(id) }

fun list(account: UUID): List<DbCredential> = join(account).let {
AccountCredentialsRepository.query(it) {
Expand All @@ -29,43 +27,49 @@ object CredentialsService {
}
}

fun add(account: UUID, credential: DbCredential): UUID =
getOrInsert(credential.credentialId, credential.document).let { cid ->
join(account, credential.credentialId).let {
AccountCredentialsRepository.query(it) {
it[AccountCredentials.id]
}
}.takeIf { it.isNotEmpty() }?.single()?.value ?: let {
AccountCredentialsRepository.insert(DbAccountCredentials(
fun add(account: UUID, credential: DbCredential): UUID = CredentialsRepository.insert(
DbCredential(
credentialId = credential.credentialId,
document = credential.document,
)
).let { cid ->
join(account, cid).let {
AccountCredentialsRepository.query(it) {
it[AccountCredentials.id]
}
}.takeIf { it.isNotEmpty() }?.single()?.value ?: let {
AccountCredentialsRepository.insert(
DbAccountCredentials(
account = account,
credential = cid,
))
}
)
)
}
}

fun delete(account: UUID, credentialId: String): Boolean = join(account, credentialId).let {
fun delete(account: UUID, id: String): Boolean = join(account, UUID.fromString(id)).let {
AccountCredentialsRepository.query(it) {
it[AccountCredentials.id]
}.singleOrNull()?.value
}?.let {
AccountCredentialsRepository.delete(it)
}?.let { it > 0 } ?: false

fun update(account: UUID, did: DidUpdateDataObject): Boolean {
fun update(account: UUID, credential: DbCredential): Boolean {
TODO()
}

private fun join(account: UUID, credentialId: String? = null) = Accounts.innerJoin(AccountCredentials,
private fun join(account: UUID, id: UUID? = null) = Accounts.innerJoin(AccountCredentials,
onColumn = { Accounts.id },
otherColumn = { AccountCredentials.account },
additionalConstraint = {
Accounts.id eq account
}).innerJoin(Credentials,
onColumn = { Credentials.id },
otherColumn = { AccountCredentials.credential },
additionalConstraint = credentialId?.let {
additionalConstraint = id?.let {
{
Credentials.credentialId eq credentialId
Credentials.id eq id
}
}).selectAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ fun Application.credentials() = walletRoute {
}
}
}) {
context.respond(getWalletService().listCredentials().map { it.parsedCredential })
// context.respond(getWalletService().listCredentials().map { it.parsedCredential })
context.respond(getWalletService().listCredentials().map { it })
}

put({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ import {ref} from "vue";
import {decodeBase64ToUtf8} from "~/composables/base64";

const route = useRoute();
const credentialId = route.params.credentialId;
const credentialId = route.params.id;

let showCredentialJson = ref(false);

Expand Down
20 changes: 10 additions & 10 deletions web/src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
<div class="bg-white p-6 rounded-2xl shadow-2xl h-full">
<div class="flex justify-end">
<div
:class="credential.expirationDate ? new Date(credential.expirationDate).getTime() > new Date().getTime() ? 'bg-cyan-50' : 'bg-red-50' : 'bg-cyan-50'"
:class="credential.parsedCredential.expirationDate ? new Date(credential.parsedCredential.expirationDate).getTime() > new Date().getTime() ? 'bg-cyan-50' : 'bg-red-50' : 'bg-cyan-50'"
class="rounded-lg px-3 mb-2">
<span
:class="credential.expirationDate ? new Date(credential.expirationDate).getTime() > new Date().getTime() ? 'text-cyan-900' : 'text-orange-900' : 'text-cyan-900'">{{
credential.expirationDate
? new Date(credential.expirationDate).getTime() >
:class="credential.parsedCredential.expirationDate ? new Date(credential.parsedCredential.expirationDate).getTime() > new Date().getTime() ? 'text-cyan-900' : 'text-orange-900' : 'text-cyan-900'">{{
credential.parsedCredential.expirationDate
? new Date(credential.parsedCredential.expirationDate).getTime() >
new Date().getTime()
? "Valid"
: "Expired"
Expand All @@ -51,19 +51,19 @@
</div>
<h2 class="text-2xl font-bold mb-2 text-gray-900 bold mb-8">
{{
credential.type[credential.type.length - 1].replace(
credential.parsedCredential.type[credential.parsedCredential.type.length - 1].replace(
/([a-z0-9])([A-Z])/g,
"$1 $2"
)
}}
</h2>
<div v-if="credential.issuer" class="flex items-center">
<img class="w-12" :src="credential.issuer?.image?.id
? credential.issuer?.image?.id
: credential.issuer?.image
<div v-if="credential.parsedCredential.issuer" class="flex items-center">
<img class="w-12" :src="credential.parsedCredential.issuer?.image?.id
? credential.parsedCredential.issuer?.image?.id
: credential.parsedCredential.issuer?.image
" />
<div class="text-natural-600 ml-2 w-32">
{{ credential.issuer?.name }}
{{ credential.parsedCredential.issuer?.name }}
</div>
</div>
</div>
Expand Down

0 comments on commit 5f13402

Please sign in to comment.