Skip to content

Commit

Permalink
Move kt file to kotlin sourceset
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Nov 30, 2023
1 parent 82de0bf commit e79a11a
Showing 1 changed file with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import com.pubnub.api.models.consumer.access_manager.v3.PNToken
import com.pubnub.api.vendor.Base64
import java.math.BigInteger
import java.nio.charset.StandardCharsets
import kotlin.collections.Map
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set
import co.nstant.`in`.cbor.model.Map as CborMap

internal class TokenParser {

Expand All @@ -27,7 +22,7 @@ internal class TokenParser {
val byteArray = Base64.decode(token.toByteArray(StandardCharsets.UTF_8), Base64.URL_SAFE)
val firstElement = CborDecoder(byteArray.inputStream()).decode().firstOrNull() ?: throw getException("Empty token")

val firstLevelMap = (firstElement as? CborMap)?.toJvmMap() ?: throw getException("First element is not a map")
val firstLevelMap = (firstElement as? co.nstant.`in`.cbor.model.Map)?.toJvmMap() ?: throw getException("First element is not a map")
val version = firstLevelMap[VERSION_KEY]?.toString()?.toInt() ?: throw getException("Couldn't parse version")
val timestamp = firstLevelMap[TIMESTAMP_KEY]?.toString()?.toLong() ?: throw getException("Couldn't parse timestamp")
val ttl = firstLevelMap[TTL_KEY]?.toString()?.toLong() ?: throw getException("Couldn't parse ttl")
Expand All @@ -50,7 +45,7 @@ internal class TokenParser {
}
}

private fun CborMap.toJvmMap(depth: Int = 0): MutableMap<String, Any> {
private fun co.nstant.`in`.cbor.model.Map.toJvmMap(depth: Int = 0): MutableMap<String, Any> {
if (depth > 3) {
throw getException("Token is too deep")
}
Expand All @@ -63,7 +58,7 @@ internal class TokenParser {
}

when (value) {
is CborMap -> result[keyString] = value.toJvmMap(depth + 1)
is co.nstant.`in`.cbor.model.Map -> result[keyString] = value.toJvmMap(depth + 1)
is ByteString -> result[keyString] = value.bytes
is List<*> -> result[keyString] = value.map { it.toString() }
is UnsignedInteger -> result[keyString] = value.value
Expand Down Expand Up @@ -107,4 +102,4 @@ internal class TokenParser {
private const val GROUPS_KEY = "grp"
private const val UUIDS_KEY = "uuid"
}
}
}

0 comments on commit e79a11a

Please sign in to comment.