https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Ali Kabiri

11/04/2020, 8:10 AM
Hi everyone 🙂 when using the Ktor client for authentication, is it possible to decode the JWT token payload in the common module (using pure Kotlin)*?*
m

mbonnin

11/04/2020, 8:26 AM
Looks like there's no mpp library yet so it's a good time to start one 🙂
e

Elka

11/04/2020, 9:02 AM
I had to write a expect/actual function that does base64 decoding and the rest was done in common
iOS
Copy code
// Based on the implementation at <https://github.com/auth0/JWTDecode.swift/>
actual fun decodeBase64(strEncoded: String): String {
    val base64 = strEncoded
            .replace("-", "+")
            .replace("_", "/")
    val padding: String by lazy {
        val length = base64.length
        val requiredLength = 4 * ceil(length / 4.0).roundToInt()
        val paddingLength = requiredLength - length
        "=".repeat(paddingLength)
    }
    return NSData.create(base64EncodedString = base64 + padding, options = NSDataBase64DecodingIgnoreUnknownCharacters)?.let {
        NSString.create(data = it, encoding = NSUTF8StringEncoding).toString()
    } ?: ""
}
Android implementation uses
android.util.Base64
👍 2
m

Mateusz Krawczuk

11/04/2020, 9:17 AM
e

Elka

11/04/2020, 9:54 AM
I remember that I used base 64 conversion in one of the KorLibs, but it didn’t work in some cases