From d2737d580552c57beb547693541d49f61d5d0855 Mon Sep 17 00:00:00 2001 From: waltkb <68587968+waltkb@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:27:59 +0200 Subject: [PATCH] Update for non 8-bit characters --- .../id/walt/service/SSIKit2WalletService.kt | 4 +- .../service/oidc4vc/TestCredentialWallet.kt | 6 +- web/src/composables/base64.ts | 17 + web/src/pages/credentials/[credentialId].vue | 654 +++++++++--------- 4 files changed, 356 insertions(+), 325 deletions(-) create mode 100644 web/src/composables/base64.ts diff --git a/src/main/kotlin/id/walt/service/SSIKit2WalletService.kt b/src/main/kotlin/id/walt/service/SSIKit2WalletService.kt index 918d906..de42404 100644 --- a/src/main/kotlin/id/walt/service/SSIKit2WalletService.kt +++ b/src/main/kotlin/id/walt/service/SSIKit2WalletService.kt @@ -88,7 +88,7 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) { val cred = it.document val parsedCred = if (cred.startsWith("{")) Json.parseToJsonElement(cred).jsonObject else if (cred.startsWith("ey")) Json.parseToJsonElement( - Base64.decode(cred.split(".")[1]).decodeToString() + Base64.UrlSafe.decode(cred.split(".")[1]).decodeToString() ).jsonObject["vc"]!!.jsonObject else throw IllegalArgumentException("Unknown credential format") Credential(parsedCred, cred) @@ -313,7 +313,7 @@ class SSIKit2WalletService(accountId: UUID) : WalletService(accountId) { println(">>> $index. CREDENTIAL IS: $credential") val credentialId = Json.parseToJsonElement( - Base64.decode(credential.split(".")[1]).decodeToString() + Base64.UrlSafe.decode(credential.split(".")[1]).decodeToString() ).jsonObject["vc"]!!.jsonObject["id"]?.jsonPrimitive?.content ?: randomUUID() CredentialsService.add(accountId, DbCredential(credentialId = credentialId, document = credential)) diff --git a/src/main/kotlin/id/walt/service/oidc4vc/TestCredentialWallet.kt b/src/main/kotlin/id/walt/service/oidc4vc/TestCredentialWallet.kt index 960b7f6..646a615 100644 --- a/src/main/kotlin/id/walt/service/oidc4vc/TestCredentialWallet.kt +++ b/src/main/kotlin/id/walt/service/oidc4vc/TestCredentialWallet.kt @@ -1,6 +1,7 @@ package id.walt.service.oidc4vc import id.walt.crypto.keys.Key +import id.walt.crypto.utils.Base64Utils.base64UrlToBase64 import id.walt.crypto.utils.JsonUtils.toJsonElement import id.walt.crypto.utils.JwsUtils.decodeJws import id.walt.did.dids.DidService @@ -82,7 +83,7 @@ class TestCredentialWallet( override fun verifyTokenSignature(target: TokenTarget, token: String): Boolean { println("VERIFYING TOKEN: ($target) $token") val jwtHeader = runCatching { - Json.parseToJsonElement(Base64.decode(token.split(".")[0]).decodeToString()).jsonObject + Json.parseToJsonElement(Base64.UrlSafe.decode(token.split(".")[0]).decodeToString()).jsonObject }.getOrElse { throw IllegalArgumentException( "Could not verify token signature, as JWT header could not be coded for token: $token, cause attached.", @@ -162,8 +163,7 @@ class TestCredentialWallet( id = "submission 1", definitionId = presentationDefinition.id, descriptorMap = credentials.map { it.rawCredential }.mapIndexed { index, vcJwsStr -> - - val vcJws = vcJwsStr.decodeJws() + val vcJws = vcJwsStr.base64UrlToBase64().decodeJws() val type = vcJws.payload["vc"]?.jsonObject?.get("type")?.jsonArray?.last()?.jsonPrimitive?.contentOrNull ?: "VerifiableCredential" diff --git a/web/src/composables/base64.ts b/web/src/composables/base64.ts new file mode 100644 index 0000000..7074bb2 --- /dev/null +++ b/web/src/composables/base64.ts @@ -0,0 +1,17 @@ +function base64ToBytes(base64: string) { + const binString = atob(base64); + return Uint8Array.from(binString, (m) => m.codePointAt(0)); +} + +function bytesToBase64(bytes: Uint8Array) { + const binString = String.fromCodePoint(...bytes); + return btoa(binString); +} + +export function encodeUtf8ToBase64(utf8source: string): string { + return bytesToBase64(new TextEncoder().encode(utf8source)) +} + +export function decodeBase64ToUtf8(base64: string): string { + return new TextDecoder().decode(base64ToBytes(base64)) +} diff --git a/web/src/pages/credentials/[credentialId].vue b/web/src/pages/credentials/[credentialId].vue index b303295..67860f2 100644 --- a/web/src/pages/credentials/[credentialId].vue +++ b/web/src/pages/credentials/[credentialId].vue @@ -1,328 +1,333 @@ \ No newline at end of file +