From 1c8a3c3ea5b4ace335e27b2172af55e15925ec32 Mon Sep 17 00:00:00 2001 From: lalwani Date: Thu, 14 Mar 2024 15:24:34 -0700 Subject: [PATCH] addressed comments --- .../uber/sdk2/auth/api/request/SsoConfig.kt | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/authentication/src/main/kotlin/com/uber/sdk2/auth/api/request/SsoConfig.kt b/authentication/src/main/kotlin/com/uber/sdk2/auth/api/request/SsoConfig.kt index 2920c7bd..ee193aca 100644 --- a/authentication/src/main/kotlin/com/uber/sdk2/auth/api/request/SsoConfig.kt +++ b/authentication/src/main/kotlin/com/uber/sdk2/auth/api/request/SsoConfig.kt @@ -38,19 +38,20 @@ object SsoConfigProvider { val resourceId = resources.getIdentifier(SSO_CONFIG_FILE, "raw", context.packageName) val configSource: BufferedSource = Okio.buffer(Okio.source(resources.openRawResource(resourceId))) - val configData = Buffer() - try { - configSource.readAll(configData) - val configJson = JSONObject(configData.readString(Charset.forName("UTF-8"))) - val clientId = getRequiredConfigString(configJson, CLIENT_ID_PARAM) - val scope = getConfigString(configJson, SCOPE_PARAM) - val redirectUri = getRequiredConfigString(configJson, REDIRECT_PARAM) - configSource.close() - return SsoConfig(clientId, redirectUri, scope) - } catch (ex: IOException) { - throw AuthException.ClientError("Failed to read configuration: " + ex.message) - } catch (ex: JSONException) { - throw AuthException.ClientError("Failed to read configuration: " + ex.message) + configSource.use { + val configData = Buffer() + try { + configSource.readAll(configData) + val configJson = JSONObject(configData.readString(Charset.forName("UTF-8"))) + val clientId = getRequiredConfigString(configJson, CLIENT_ID_PARAM) + val scope = getConfigString(configJson, SCOPE_PARAM) + val redirectUri = getRequiredConfigString(configJson, REDIRECT_PARAM) + return SsoConfig(clientId, redirectUri, scope) + } catch (ex: IOException) { + throw AuthException.ClientError("Failed to read configuration: " + ex.message) + } catch (ex: JSONException) { + throw AuthException.ClientError("Failed to read configuration: " + ex.message) + } } } @@ -63,11 +64,8 @@ object SsoConfigProvider { } private fun getConfigString(configJson: JSONObject, propName: String): String? { - var value: String = configJson.optString(propName) ?: return null - value = value.trim { it <= ' ' } - return if (TextUtils.isEmpty(value)) { - null - } else value + val value: String = configJson.optString(propName) ?: return null + return value.trim { it <= ' ' }.takeIf { it.isNotEmpty() } } private const val SSO_CONFIG_FILE = "sso_config"