Skip to content

Commit

Permalink
chore: PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderPostma committed Jun 26, 2024
1 parent 1af39e0 commit 81322c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlin.js.JsExport

private val qpAllowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~".toSet()

/**
* URL encode a String.
* Converts characters not allowed in URL query parameters to their percent-encoded equivalents.
*
* input an input string
* @return URL encoded String
*/
@JsExport
fun urlEncodeValue(input: String): String {
return buildString {
input.forEach { char ->
Expand Down Expand Up @@ -71,10 +73,11 @@ fun <T> T.toUrlEncodedJsonValue(serializer: KSerializer<T>): String {
* Extension function to decode a URL encoded String.
* Converts percent-encoded characters back to their original form.
*
* input An URL encoded input string
* @return Decoded String
*/
fun String.fromUrlEncodedValue(): String {
val input = this
@JsExport
fun urlDecodeValue(input: String): String {
return buildString {
var i = 0
while (i < input.length) {
Expand All @@ -96,6 +99,16 @@ fun String.fromUrlEncodedValue(): String {
}
}

/**
* Extension function to decode a URL encoded String.
* Converts percent-encoded characters back to their original form.
*
* @return Decoded String
*/
fun String.fromUrlEncodedValue(): String {
return urlDecodeValue(this)
}

/**
* Extension function to decode a URL encoded JSON String to a JsonElement.
* Decodes the URL encoded String and parses it to a JsonElement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ data class TestData(

class JsonUrlEncoderTest {
private val originalJson =
"""{"grants":{"urn:ietf:params:oauth:grant-type:pre-authorized_code":{"pre-authorized_code":"a"}},"credential_configuration_ids":["Woonplaatsverklaring"],"credential_issuer":"https://agent.issuer.bd.demo.sphereon.com"}"""
"""{"grants":{"urn:ietf:params:oauth:grant-type:pre-authorized_code":{"pre-authorized_code":"a"}},"credential_configuration_ids":["DummyCredential"],"credential_issuer":"https://agent.issuer.dummy.com"}"""
private val encodedJson =
"%7B%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%22a%22%7D%7D%2C%22credential_configuration_ids%22%3A%5B%22Woonplaatsverklaring%22%5D%2C%22credential_issuer%22%3A%22https%3A%2F%2Fagent.issuer.bd.demo.sphereon.com%22%7D"
"%7B%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%22a%22%7D%7D%2C%22credential_configuration_ids%22%3A%5B%22DummyCredential%22%5D%2C%22credential_issuer%22%3A%22https%3A%2F%2Fagent.issuer.dummy.com%22%7D"
private val encodedJsonFromObject = // In objects, we can have colons in the field name
"%7B%22grants%22%3A%7B%22urn_ietf_params_oauth_grant_type_pre_authorized_code%22%3A%7B%22pre_authorized_code%22%3A%22a%22%7D%7D%2C%22credential_configuration_ids%22%3A%5B%22Woonplaatsverklaring%22%5D%2C%22credential_issuer%22%3A%22https%3A%2F%2Fagent.issuer.bd.demo.sphereon.com%22%7D"
"%7B%22grants%22%3A%7B%22urn_ietf_params_oauth_grant_type_pre_authorized_code%22%3A%7B%22pre_authorized_code%22%3A%22a%22%7D%7D%2C%22credential_configuration_ids%22%3A%5B%22DummyCredential%22%5D%2C%22credential_issuer%22%3A%22https%3A%2F%2Fagent.issuer.dummy.com%22%7D"

private val testData = TestData(
grants = Grants(PreAuthorizedCode("a")),
credential_configuration_ids = listOf("Woonplaatsverklaring"),
credential_issuer = "https://agent.issuer.bd.demo.sphereon.com"
credential_configuration_ids = listOf("DummyCredential"),
credential_issuer = "https://agent.issuer.dummy.com"
)

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.sphereon.oid.fed.common.mime

import com.sphereon.oid.fed.common.mime.appendUrlEncodedValue
import com.sphereon.oid.fed.common.mime.fromUrlEncodedJsonValuesToJsonElements
import com.sphereon.oid.fed.common.mime.fromUrlEncodedValues
import com.sphereon.oid.fed.common.mime.getUrlEncodedJsonValueToJsonElement
import com.sphereon.oid.fed.common.mime.getUrlEncodedValue
import io.ktor.http.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
Expand All @@ -14,9 +9,9 @@ import kotlin.test.assertFailsWith

class JsonUrlEncoderTestKtor {
private val originalJson =
"""{"grants":{"urn:ietf:params:oauth:grant-type:pre-authorized_code":{"pre-authorized_code":"a"}},"credential_configuration_ids":["Woonplaatsverklaring"],"credential_issuer":"https://agent.issuer.bd.demo.sphereon.com"}"""
"""{"grants":{"urn:ietf:params:oauth:grant-type:pre-authorized_code":{"pre-authorized_code":"a"}},"credential_configuration_ids":["DummyCredential"],"credential_issuer":"https://agent.issuer.dummy.com"}"""
private val encodedJson =
"%7B%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%22a%22%7D%7D%2C%22credential_configuration_ids%22%3A%5B%22Woonplaatsverklaring%22%5D%2C%22credential_issuer%22%3A%22https%3A%2F%2Fagent.issuer.bd.demo.sphereon.com%22%7D"
"%7B%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%22a%22%7D%7D%2C%22credential_configuration_ids%22%3A%5B%22DummyCredential%22%5D%2C%22credential_issuer%22%3A%22https%3A%2F%2Fagent.issuer.dummy.com%22%7D"


@Test
Expand Down

0 comments on commit 81322c7

Please sign in to comment.