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

Commit

Permalink
Update for non 8-bit characters
Browse files Browse the repository at this point in the history
  • Loading branch information
waltkb committed Oct 25, 2023
1 parent 71af59a commit d2737d5
Show file tree
Hide file tree
Showing 4 changed files with 356 additions and 325 deletions.
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 @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions web/src/composables/base64.ts
Original file line number Diff line number Diff line change
@@ -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))
}
Loading

0 comments on commit d2737d5

Please sign in to comment.