mirror of
https://github.com/bitwarden/android.git
synced 2024-11-21 08:55:48 +03:00
Log JWT parsing errors (#4326)
This commit is contained in:
parent
b183f7af42
commit
2d15c4864f
1 changed files with 12 additions and 4 deletions
|
@ -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<JwtTokenDataJson>(decodedDataJson)
|
||||
} catch (_: Throwable) {
|
||||
} catch (throwable: Throwable) {
|
||||
Timber.e(throwable, "Failed to decode JwtTokenDataJson")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue