Hi everyone :slightly_smiling_face: when using the...
# multiplatform
a
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
Looks like there's no mpp library yet so it's a good time to start one 🙂
e
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
e
I remember that I used base 64 conversion in one of the KorLibs, but it didn’t work in some cases