Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/generic request parameters #167

Merged
merged 11 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ tbd
- New Interface `RequestParameters`
- Remove RQES components from `AuthenticationRequestParameters`
- New class `CscAuthenticationRequestParameters` which now holds the RQES components
- New class `SignatureRequestParameters` and `SignatureRequestParametersFrom`
- New class `SignatureRequestParameters`
- Refactor `AuthenticationRequestParametersFrom` to generic sealed class `RequestParametersFrom`
- Refactor `AuthenticationRequestParser` to open class `RequestParser`
- New class `ExtendedRequestParser` used to also parse `SignatureRequestParametersFrom`


Release 5.2.0:
- SD-JWT: Validate confirmation claims correctly
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

/**
* Class that de-/serializes [JwsSigned] without respecting the generic type
*
* ONLY USE TO SERIALIZE!
* DO NOT USE TO DESERIALIZE!
*
* For more info on correct deserialization see [RequestParametersFromSerializer]
*/
object JwsSignedSerializer : KSerializer<JwsSigned<*>> {

override val descriptor: SerialDescriptor =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package at.asitplus.openid

import io.ktor.http.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable


@Serializable(with = RequestParametersFromSerializer::class)
sealed class RequestParametersFrom<S : RequestParameters> {

abstract val parameters: S

@Serializable
@SerialName("JwsSigned")
data class JwsSigned<T : RequestParameters>(
@Serializable(JwsSignedSerializer::class)
val jwsSigned: at.asitplus.signum.indispensable.josef.JwsSigned<T>,
override val parameters: T,
) : RequestParametersFrom<T>()

@Serializable
@SerialName("Uri")
data class Uri<T : RequestParameters>(
@Serializable(UrlSerializer::class)
val url: Url,
override val parameters: T,
) : RequestParametersFrom<T>()

@Serializable
@SerialName("Json")
data class Json<T : RequestParameters>(
val jsonString: String,
override val parameters: T,
) : RequestParametersFrom<T>()

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package at.asitplus.openid

import at.asitplus.signum.indispensable.josef.JwsSigned
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonEncoder
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonObject

/**
* In order to de-/serialize generic types we need a kind of factory approach
* Because we deal with a sealed class we can use an intermediary jsonSerializer,
* find the correct object and the specific type of the generic type and
* then finalize the serialization
*
* In order to de-/serialize JwsSigned which itself is again a generic class
* we use the fact that we can find the class of parameters before we need to know the
* generic class of JwsSigned. To serialize we use [JwsSignedSerializer].
*/
class RequestParametersFromSerializer<T : RequestParameters>(
private val parameterSerializer: KSerializer<T>,
) : KSerializer<RequestParametersFrom<T>> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("RequestParametersFromClass") {
element("Json", buildClassSerialDescriptor("Json") {
element<String>("jsonString")
element("parameters", parameterSerializer.descriptor)
nodh marked this conversation as resolved.
Show resolved Hide resolved
})
element("JwsSigned", buildClassSerialDescriptor("JwsSigned") {
element("jwsSigned", JwsSignedSerializer.descriptor)
element("parameters", parameterSerializer.descriptor)
})
element("Url", buildClassSerialDescriptor("Url") {
element("url", UrlSerializer.descriptor)
element("parameters", parameterSerializer.descriptor)
})
}

override fun deserialize(decoder: Decoder): RequestParametersFrom<T> {
require(decoder is JsonDecoder) // this class can be decoded only by Json

val element = decoder.decodeJsonElement()
return when {
"jsonString" in element.jsonObject -> RequestParametersFrom.Json(
nodh marked this conversation as resolved.
Show resolved Hide resolved
decoder.json.decodeFromJsonElement<String>(element.jsonObject["jsonString"]!!),
decoder.json.decodeFromJsonElement(parameterSerializer, element.jsonObject["parameters"]!!)
)

"jwsSigned" in element.jsonObject -> run {
val parameters =
decoder.json.decodeFromJsonElement(parameterSerializer, element.jsonObject["parameters"]!!)
val jwsSignedRaw = decoder.json.decodeFromJsonElement<String>(element.jsonObject["jwsSigned"]!!)
val jwsSignedFinal1 = JwsSigned.deserialize(jwsSignedRaw).getOrThrow()
nodh marked this conversation as resolved.
Show resolved Hide resolved
val jws = JwsSigned<T>(
jwsSignedFinal1.header,
parameters,
jwsSignedFinal1.signature,
jwsSignedFinal1.plainSignatureInput
)
RequestParametersFrom.JwsSigned(
jws,
nodh marked this conversation as resolved.
Show resolved Hide resolved
parameters,
)
}

"url" in element.jsonObject ->
RequestParametersFrom.Uri(
decoder.json.decodeFromJsonElement(UrlSerializer, element.jsonObject["url"]!!),
decoder.json.decodeFromJsonElement(parameterSerializer, element.jsonObject["parameters"]!!)
)

else -> throw NotImplementedError("Unknown RequestParametersFrom subclass. Input: $element")
}
}

override fun serialize(encoder: Encoder, value: RequestParametersFrom<T>) {
require(encoder is JsonEncoder) // this class can be decoded only by Json
val element = when (value) {
is RequestParametersFrom.Json -> buildJsonObject {
put("jsonString", encoder.json.encodeToJsonElement(value.jsonString))
put(
"parameters", encoder.json.encodeToJsonElement(
parameterSerializer,
value.parameters
)
)
}

is RequestParametersFrom.JwsSigned -> buildJsonObject {
put("jwsSigned", encoder.json.encodeToJsonElement(value.jwsSigned.serialize()))
put(
"parameters", encoder.json.encodeToJsonElement(
nodh marked this conversation as resolved.
Show resolved Hide resolved
parameterSerializer,
value.parameters
)
)
}

is RequestParametersFrom.Uri -> buildJsonObject {
put("url", encoder.json.encodeToJsonElement(UrlSerializer, value.url))
put(
"parameters", encoder.json.encodeToJsonElement(
parameterSerializer,
value.parameters
)
)
}
}
encoder.encodeJsonElement(element)
}
}

This file was deleted.

Loading
Loading