From 2d15c4864fa0004975c7cd8788b1b78d189d60dd Mon Sep 17 00:00:00 2001 From: David Perez Date: Mon, 18 Nov 2024 16:13:55 -0600 Subject: [PATCH] Log JWT parsing errors (#4326) --- .../data/auth/repository/util/JwtTokenUtils.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/util/JwtTokenUtils.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/util/JwtTokenUtils.kt index 68a02363d..987390eb5 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/util/JwtTokenUtils.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/util/JwtTokenUtils.kt @@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.auth.repository.util import com.x8bit.bitwarden.data.auth.repository.model.JwtTokenDataJson import com.x8bit.bitwarden.data.platform.datasource.network.util.base64UrlDecodeOrNull import kotlinx.serialization.json.Json +import timber.log.Timber /** * Internal, generally basic [Json] instance for JWT parsing purposes. @@ -17,17 +18,24 @@ private val json: Json by lazy { /** * Parses a [JwtTokenDataJson] from the given [jwtToken], or `null` if this parsing is not possible. */ -@Suppress("MagicNumber") +@Suppress("MagicNumber", "TooGenericExceptionCaught") fun parseJwtTokenDataOrNull(jwtToken: String): JwtTokenDataJson? { val parts = jwtToken.split(".") - if (parts.size != 3) return null + if (parts.size != 3) { + Timber.e(IllegalArgumentException("Incorrect number of parts"), "Invalid JWT Token") + return null + } val dataJson = parts[1] - val decodedDataJson = dataJson.base64UrlDecodeOrNull() ?: return null + val decodedDataJson = dataJson.base64UrlDecodeOrNull() ?: run { + Timber.e(IllegalArgumentException("Unable to decode"), "Invalid JWT Token") + return null + } return try { json.decodeFromString(decodedDataJson) - } catch (_: Throwable) { + } catch (throwable: Throwable) { + Timber.e(throwable, "Failed to decode JwtTokenDataJson") null } }