-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
260 additions
and
194 deletions.
There are no files selected for viewing
109 changes: 89 additions & 20 deletions
109
openid-data-classes/src/commonMain/kotlin/at/asitplus/openid/AuthenticationRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,115 @@ | ||
package at.asitplus.openid | ||
|
||
|
||
import at.asitplus.catching | ||
import io.ktor.http.* | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.builtins.serializer | ||
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 | ||
|
||
class RequestParametersFromClassSerializer<T : RequestParameters>( | ||
private val parameterSerializer: KSerializer<T>, | ||
) : KSerializer<RequestParametersFromClass<T>> { | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("RequestParametersFromClass") { | ||
element("JwsSigned", parameterSerializer.descriptor) | ||
element("Json", buildClassSerialDescriptor("Json") { | ||
element<String>("jsonString") | ||
element("parameters", parameterSerializer.descriptor) | ||
}) | ||
element("Uri", parameterSerializer.descriptor) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): RequestParametersFromClass<T> { | ||
// Decoder -> JsonDecoder | ||
require(decoder is JsonDecoder) // this class can be decoded only by Json | ||
// JsonDecoder -> JsonElement | ||
val element = decoder.decodeJsonElement() | ||
return when { | ||
"jsonString" in element.jsonObject -> RequestParametersFromClass.Json( | ||
decoder.json.decodeFromJsonElement<String>(element.jsonObject["jsonString"]!!), | ||
decoder.json.decodeFromJsonElement(parameterSerializer, element.jsonObject["parameters"]!!) | ||
) | ||
|
||
@Serializable | ||
sealed class AuthenticationRequestParametersFrom : RequestParametersFrom { | ||
else -> TODO() | ||
} | ||
} | ||
|
||
fun serialize(): String = odcJsonSerializer.encodeToString(this) | ||
override fun serialize(encoder: Encoder, value: RequestParametersFromClass<T>) { | ||
// Encoder -> JsonEncoder | ||
require(encoder is JsonEncoder) // This class can be encoded only by Json | ||
// value -> JsonElement | ||
val element = when (value) { | ||
is RequestParametersFromClass.Json -> buildJsonObject { | ||
put("jsonString", encoder.json.encodeToJsonElement(value.jsonString)) | ||
put( | ||
"parameters", encoder.json.encodeToJsonElement( | ||
parameterSerializer, | ||
value.parameters | ||
) | ||
) | ||
} | ||
|
||
companion object { | ||
fun deserialize(input: String) = | ||
catching { odcJsonSerializer.decodeFromString<AuthenticationRequestParametersFrom>(input) } | ||
else -> TODO() | ||
} | ||
// JsonElement -> JsonEncoder | ||
encoder.encodeJsonElement(element) | ||
} | ||
} | ||
|
||
|
||
@Serializable(with = RequestParametersFromClassSerializer::class) | ||
sealed class RequestParametersFromClass<S : RequestParameters> { | ||
|
||
abstract override val parameters: AuthenticationRequestParameters | ||
abstract val parameters: S | ||
|
||
@Serializable | ||
@SerialName("JwsSigned") | ||
data class JwsSigned( | ||
data class JwsSigned<T : RequestParameters>( | ||
@Serializable(JwsSignedSerializer::class) | ||
val jwsSigned: at.asitplus.signum.indispensable.josef.JwsSigned<ByteArray>, | ||
override val parameters: AuthenticationRequestParameters, | ||
) : AuthenticationRequestParametersFrom() | ||
val jwsSigned: at.asitplus.signum.indispensable.josef.JwsSigned<T>, | ||
override val parameters: T, | ||
) : RequestParametersFromClass<T>() { | ||
|
||
} | ||
|
||
@Serializable | ||
@SerialName("Uri") | ||
data class Uri( | ||
data class Uri<T : RequestParameters>( | ||
@Serializable(UrlSerializer::class) | ||
val url: Url, | ||
override val parameters: AuthenticationRequestParameters, | ||
) : AuthenticationRequestParametersFrom() | ||
override val parameters: T, | ||
) : RequestParametersFromClass<T>() { | ||
|
||
// override inline fun serialize(json: kotlinx.serialization.json.Json): String = json.encodeToString(this) | ||
} | ||
|
||
@Serializable | ||
@SerialName("Json") | ||
data class Json( | ||
data class Json<T : RequestParameters>( | ||
val jsonString: String, | ||
override val parameters: AuthenticationRequestParameters, | ||
) : AuthenticationRequestParametersFrom() | ||
override val parameters: T, | ||
) : RequestParametersFromClass<T>() { | ||
|
||
// override fun serialize(json: kotlinx.serialization.json.Json): String = json.encodeToString(this) | ||
} | ||
|
||
// companion object { | ||
// inline fun <P : RequestParameters> deserialize( | ||
// it: String, | ||
// json: kotlinx.serialization.json.Json = kotlinx.serialization.json.Json, | ||
// ): KmmResult<RequestParametersFromClass<P>> = | ||
// catching { json.decodeFromString<RequestParametersFromClass<P>>(it) } | ||
// } | ||
} | ||
|
||
|
8 changes: 4 additions & 4 deletions
8
openid-data-classes/src/commonMain/kotlin/at/asitplus/openid/RequestParameterFrom.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package at.asitplus.openid | ||
|
||
interface RequestParametersFrom { | ||
val parameters: RequestParameters | ||
} | ||
// | ||
//interface RequestParametersFrom <T:RequestParameters>{ | ||
// val parameters: T | ||
//} |
90 changes: 44 additions & 46 deletions
90
rqes-data-classes/src/commonMain/kotlin/at/asitplus/rqes/SignatureRequestParameterFrom.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,45 @@ | ||
package at.asitplus.rqes | ||
|
||
import at.asitplus.catching | ||
import at.asitplus.openid.JwsSignedSerializer | ||
import at.asitplus.openid.RequestParametersFrom | ||
import at.asitplus.openid.UrlSerializer | ||
import io.ktor.http.* | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToString | ||
|
||
|
||
@Serializable | ||
sealed class SignatureRequestParametersFrom : RequestParametersFrom { | ||
fun serialize(): String = rdcJsonSerializer.encodeToString(this) | ||
|
||
companion object { | ||
fun deserialize(input: String) = | ||
catching { rdcJsonSerializer.decodeFromString<SignatureRequestParameters>(input) } | ||
} | ||
|
||
abstract override val parameters: SignatureRequestParameters | ||
|
||
@Serializable | ||
@SerialName("JwsSigned") | ||
data class JwsSigned( | ||
@Serializable(JwsSignedSerializer::class) | ||
val jwsSigned: at.asitplus.signum.indispensable.josef.JwsSigned<ByteArray>, | ||
override val parameters: SignatureRequestParameters, | ||
) : SignatureRequestParametersFrom() | ||
|
||
@Serializable | ||
@SerialName("Uri") | ||
data class Uri( | ||
@Serializable(UrlSerializer::class) | ||
val url: Url, | ||
override val parameters: SignatureRequestParameters, | ||
) : SignatureRequestParametersFrom() | ||
|
||
@Serializable | ||
@SerialName("Json") | ||
data class Json( | ||
val jsonString: String, | ||
override val parameters: SignatureRequestParameters, | ||
) : SignatureRequestParametersFrom() | ||
|
||
} | ||
// | ||
//import at.asitplus.catching | ||
//import at.asitplus.openid.JwsSignedSerializer | ||
//import at.asitplus.openid.RequestParametersFrom | ||
//import at.asitplus.openid.UrlSerializer | ||
//import io.ktor.http.* | ||
//import kotlinx.serialization.SerialName | ||
//import kotlinx.serialization.Serializable | ||
//import kotlinx.serialization.encodeToString | ||
// | ||
// | ||
//@Serializable | ||
//sealed class SignatureRequestParametersFrom : RequestParametersFrom<SignatureRequestParameters> { | ||
// fun serialize(): String = rdcJsonSerializer.encodeToString(this) | ||
// | ||
// companion object { | ||
// fun deserialize(input: String) = | ||
// catching { rdcJsonSerializer.decodeFromString<SignatureRequestParameters>(input) } | ||
// } | ||
// | ||
// @Serializable | ||
// @SerialName("JwsSigned") | ||
// data class JwsSigned( | ||
// @Serializable(JwsSignedSerializer::class) | ||
// val jwsSigned: at.asitplus.signum.indispensable.josef.JwsSigned<SignatureRequestParameters>, | ||
// override val parameters: SignatureRequestParameters, | ||
// ) : SignatureRequestParametersFrom() | ||
// | ||
// @Serializable | ||
// @SerialName("Uri") | ||
// data class Uri( | ||
// @Serializable(UrlSerializer::class) | ||
// val url: Url, | ||
// override val parameters: SignatureRequestParameters, | ||
// ) : SignatureRequestParametersFrom() | ||
// | ||
// @Serializable | ||
// @SerialName("Json") | ||
// data class Json( | ||
// val jsonString: String, | ||
// override val parameters: SignatureRequestParameters, | ||
// ) : SignatureRequestParametersFrom() | ||
// | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.