Skip to content

Commit 0e712f7

Browse files
committed
switch away from kotlinx.serialization
1 parent d39cd67 commit 0e712f7

File tree

3 files changed

+41
-43
lines changed

3 files changed

+41
-43
lines changed

ziti/src/integrationTest/kotlin/org/openziti/impl/LoadTests.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.openziti.impl
1818

19-
import kotlinx.serialization.json.Json
2019
import org.junit.jupiter.api.AfterEach
2120
import org.junit.jupiter.api.Assertions.assertEquals
2221
import org.junit.jupiter.api.BeforeEach
@@ -43,7 +42,11 @@ class LoadTests: BaseTest() {
4342

4443
@Test
4544
fun testLoadConfigByteArray() {
46-
val cfgBytes = Json.encodeToString(cfg).toByteArray(Charsets.UTF_8)
45+
46+
val cfgBytes = ByteArrayOutputStream().use {
47+
cfg.store(it)
48+
it.toByteArray()
49+
}
4750

4851
Ziti.init(cfgBytes, false)
4952

ziti/src/main/kotlin/org/openziti/IdentityConfig.kt

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
package org.openziti
1818

19-
import kotlinx.serialization.ExperimentalSerializationApi
20-
import kotlinx.serialization.SerialName
21-
import kotlinx.serialization.Serializable
22-
import kotlinx.serialization.json.Json
23-
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
24-
import kotlinx.serialization.json.decodeFromStream
19+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
20+
import com.fasterxml.jackson.annotation.JsonProperty
21+
import com.fasterxml.jackson.databind.ObjectMapper
22+
import com.fasterxml.jackson.module.kotlin.kotlinModule
2523
import org.openziti.identity.makeSSLContext
2624
import org.openziti.util.readCerts
2725
import org.openziti.util.readKey
@@ -35,24 +33,26 @@ import javax.net.ssl.SSLContext
3533
/**
3634
* Identity loaded from identity configuration JSON.
3735
*/
38-
@JsonIgnoreUnknownKeys
39-
@Serializable data class IdentityConfig (
36+
@JsonIgnoreProperties(ignoreUnknown = true)
37+
data class IdentityConfig (
4038
/**
4139
* Ziti controller address.
4240
*/
43-
@SerialName("ztAPI") val controller: String,
41+
@JsonProperty("ztAPI")
42+
val controller: String,
4443

4544
/**
4645
* List of ziti controller addresses.
4746
*/
48-
@SerialName("ztAPIs") val apiEndpoints: Collection<String>? = listOf(controller),
47+
@JsonProperty("ztAPIs")
48+
val apiEndpoints: Collection<String>? = listOf(controller),
4949

5050
/**
5151
* Identity credentials.
5252
*/
5353
val id: Id): Identity {
5454

55-
@Serializable data class Id(
55+
data class Id(
5656
/** Identity private key in PEM format */
5757
val key: String? = null,
5858
/** Identity X.509 certificate in PEM format */
@@ -65,7 +65,7 @@ import javax.net.ssl.SSLContext
6565
* Store identity configuration to the output stream.
6666
*/
6767
fun store(output: OutputStream) {
68-
output.write(json.encodeToString(this).encodeToByteArray())
68+
jsonMapper.writeValue(output, this)
6969
}
7070

7171
/**
@@ -96,15 +96,14 @@ import javax.net.ssl.SSLContext
9696

9797

9898
companion object {
99-
private val jsonBuilder = Json {
100-
ignoreUnknownKeys = true
101-
}
99+
val jsonMapper: ObjectMapper =
100+
ObjectMapper().registerModule(kotlinModule())
102101
/**
103102
* Load identity configuration from the input stream.
104103
*/
105-
@OptIn(ExperimentalSerializationApi::class)
106104
@JvmStatic
107-
fun load(input: InputStream): IdentityConfig = jsonBuilder.decodeFromStream(serializer(), input)
105+
fun load(input: InputStream): IdentityConfig =
106+
jsonMapper.readValue(input, IdentityConfig::class.java)
108107

109108
/**
110109
* Load identity configuration from the byte array.
@@ -130,9 +129,5 @@ import javax.net.ssl.SSLContext
130129

131130
return load(cfg.toByteArray())
132131
}
133-
134-
private val json = Json {
135-
prettyPrint = true
136-
}
137132
}
138133
}

ziti/src/main/kotlin/org/openziti/api/Authentication.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616

1717
package org.openziti.api
1818

19+
import com.fasterxml.jackson.databind.JsonNode
20+
import com.fasterxml.jackson.databind.ObjectMapper
21+
import com.fasterxml.jackson.module.kotlin.kotlinModule
1922
import kotlinx.coroutines.Dispatchers
2023
import kotlinx.coroutines.asExecutor
2124
import kotlinx.coroutines.future.await
22-
import kotlinx.serialization.json.Json
23-
import kotlinx.serialization.json.JsonObject
24-
import kotlinx.serialization.json.jsonObject
25-
import kotlinx.serialization.json.jsonPrimitive
2625
import org.openziti.edge.ApiClient
2726
import org.openziti.edge.api.AuthenticationApi
2827
import org.openziti.edge.api.CurrentApiSessionApi
@@ -128,6 +127,7 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
128127
val Encoder: Base64.Encoder = Base64.getUrlEncoder().withoutPadding()
129128
const val DISCOVERY = "/oidc/.well-known/openid-configuration"
130129
const val TOKEN_EXCHANGE_GRANT = "urn:ietf:params:oauth:grant-type:token-exchange"
130+
val json: ObjectMapper = ObjectMapper().registerModule(kotlinModule())
131131
}
132132

133133

@@ -136,7 +136,7 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
136136
.followRedirects(HttpClient.Redirect.NEVER)
137137
.executor(Dispatchers.IO.asExecutor())
138138
.build()
139-
lateinit var tokens: JsonObject
139+
lateinit var tokens: JsonNode
140140

141141
private val config by lazy {
142142
loadConfig()
@@ -166,7 +166,7 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
166166
.POST(HttpRequest.BodyPublishers.ofString(body))
167167
.build()
168168

169-
i{"sending auth request $req"}
169+
d{"sending auth request $req"}
170170
val resp = http.sendAsync(req, HttpResponse.BodyHandlers.ofString()).await()
171171

172172
if (resp.statusCode() / 100 != 3 && resp.headers().firstValue("Location").isEmpty) {
@@ -203,7 +203,7 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
203203
return query["code"]!! to query["state"]!!
204204
}
205205

206-
private suspend fun getTokens(ep: URI, code: String, codeVerifier: String): JsonObject {
206+
private suspend fun getTokens(ep: URI, code: String, codeVerifier: String): JsonNode {
207207
val body = formatForm(
208208
mapOf(
209209
"grant_type" to "authorization_code",
@@ -219,7 +219,7 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
219219
.POST(HttpRequest.BodyPublishers.ofString(body)).build()
220220

221221
val tokenResp = http.sendAsync(req, HttpResponse.BodyHandlers.ofString()).await()
222-
return Json.parseToJsonElement(tokenResp.body()).jsonObject
222+
return json.readTree(tokenResp.body())
223223
}
224224

225225
override suspend fun login(): ZitiAuthenticator.ZitiAccessToken {
@@ -230,9 +230,9 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
230230
val state = Encoder.encodeToString(Random.Default.nextBytes(30))
231231

232232

233-
val authEndpoint = config["authorization_endpoint"]?.jsonPrimitive?.content
233+
val authEndpoint = config["authorization_endpoint"]?.textValue()
234234
?: throw Exception("Missing authorization endpoint in OIDC config")
235-
val tokenEndpoint = config["token_endpoint"]?.jsonPrimitive?.content
235+
val tokenEndpoint = config["token_endpoint"]?.textValue()
236236
?: throw Exception("Missing token endpoint in OIDC config")
237237

238238
val loginURI = startAuth(authEndpoint, challenge, state)
@@ -244,14 +244,14 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
244244
tokens = getTokens(URI.create(tokenEndpoint), code, codeVerifier)
245245
d{ "OIDC tokens: $tokens" }
246246

247-
val accessToken = tokens["access_token"]?.jsonPrimitive?.content
247+
val accessToken = tokens["access_token"]?.textValue()
248248
?: throw Exception("Missing access token in OIDC response")
249-
val exp = OffsetDateTime.now().plusSeconds(tokens["expires_in"]?.jsonPrimitive?.content?.toLong() ?: 600)
249+
val exp = OffsetDateTime.now().plusSeconds(tokens["expires_in"]?.longValue() ?: 600)
250250
return ZitiAuthenticator.ZitiAccessToken(ZitiAuthenticator.TokenType.BEARER, accessToken, exp)
251251
}
252252

253253
override suspend fun refresh(): ZitiAuthenticator.ZitiAccessToken {
254-
val refreshToken = tokens.get("refresh_token")?.jsonPrimitive?.content
254+
val refreshToken = tokens.get("refresh_token")?.textValue()
255255

256256
if (refreshToken == null) return login()
257257

@@ -263,7 +263,7 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
263263
)
264264

265265
val req = HttpRequest.newBuilder()
266-
.uri(config["token_endpoint"]?.jsonPrimitive?.content?.let { URI.create(it) })
266+
.uri(config["token_endpoint"]?.textValue()?.let { URI.create(it) })
267267
.header("Accept", "application/x-www-form-urlencoded")
268268
.POST(HttpRequest.BodyPublishers.ofString(formatForm(form)))
269269
.build()
@@ -274,14 +274,14 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
274274
return login()
275275
}
276276

277-
tokens = Json.parseToJsonElement(resp.body()).jsonObject
278-
val accessToken = tokens["access_token"]?.jsonPrimitive?.content
277+
tokens = json.readTree(resp.body())
278+
val accessToken = tokens["access_token"]?.textValue()
279279
?: throw Exception("Missing access token in OIDC response")
280-
val exp = OffsetDateTime.now().plusSeconds(tokens["expires_in"]?.jsonPrimitive?.content?.toLong() ?: 600)
280+
val exp = OffsetDateTime.now().plusSeconds(tokens["expires_in"]?.longValue() ?: 600)
281281
return ZitiAuthenticator.ZitiAccessToken(ZitiAuthenticator.TokenType.BEARER, accessToken, exp)
282282
}
283283

284-
private fun loadConfig(): JsonObject {
284+
private fun loadConfig(): JsonNode {
285285
val url = URI.create(ep).resolve(DISCOVERY)
286286

287287
val request = HttpRequest.newBuilder(url)
@@ -293,7 +293,7 @@ class InternalOIDC(val ep: String, ssl: SSLContext): ZitiAuthenticator, Logged b
293293
throw Exception("Failed to get OIDC config: ${response.statusCode()}")
294294
}
295295

296-
i("OIDC config response: ${response.body()}")
297-
return Json.parseToJsonElement(response.body()).jsonObject
296+
v {"OIDC config response: ${response.body()}"}
297+
return ObjectMapper().readTree(response.body())
298298
}
299299
}

0 commit comments

Comments
 (0)