1616
1717package 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
1922import kotlinx.coroutines.Dispatchers
2023import kotlinx.coroutines.asExecutor
2124import 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
2625import org.openziti.edge.ApiClient
2726import org.openziti.edge.api.AuthenticationApi
2827import 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