All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.onfido.api.client.token.sdk.InternalSDKToken.kt Maven / Gradle / Ivy

package com.onfido.api.client.token.sdk

import com.onfido.api.client.exception.InternalEnterpriseFeaturesNotAuthorizedException
import com.onfido.api.client.getJsonParserInstance
import com.onfido.api.client.token.InternalToken
import com.onfido.api.client.token.sdk.model.InternalSDKTokenPayload
import com.onfido.api.client.token.sdk.url.ApiUrlCreator
import com.onfido.api.client.token.sdk.url.ApiUrlCreatorImpl
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.jsonObject

public class InternalSDKToken @JvmOverloads constructor(
    tokenValue: String,
    appId: String?,
    urlCreator: ApiUrlCreator = ApiUrlCreatorImpl()
) : InternalToken(tokenValue) {
    private val urlCreator: ApiUrlCreator
    private var sdkTokenPayload: InternalSDKTokenPayload? = null

    @Transient
    private val jsonParser: Json = getJsonParserInstance()

    val enterpriseFeatures: Map
        get() {
            val json = InternalSDKTokenExtractor.decodePayload(tokenValue)
            val tokenMap = jsonParser
                .decodeFromString>(json)

            if (!tokenMap.containsKey(ENTERPRISE_FEATURES)) {
                throw InternalEnterpriseFeaturesNotAuthorizedException()
            }
            return tokenMap[ENTERPRISE_FEATURES]?.jsonObject?.toMap() ?: emptyMap()
        }
    val uuid: String?
        get() {
            val sdkTokenPayload = ensureSDKTokenPayload()
            return sdkTokenPayload?.uuid
        }

    val applicantId: ApplicantId
        get() {
            if (isDemoToken) {
                // Just return tokenValue which will be "demo" always
                return ApplicantId(tokenValue)
            }
            val sdkTokenPayload = ensureSDKTokenPayload()
            return ApplicantId(sdkTokenPayload?.applicantUuid ?: "")
        }

    val isStudioToken: Boolean
        get() = ensureSDKTokenPayload()?.isStudioToken ?: false

    val customerUserHash: String
        get() = ensureSDKTokenPayload()?.customerUserHash.orEmpty()

    init {
        this.appId = appId
        this.urlCreator = urlCreator
    }

    override fun buildUrl(): String {
        return urlCreator.createApiUrl(tokenValue)
    }

    @Synchronized
    private fun ensureSDKTokenPayload(): InternalSDKTokenPayload? {
        if (sdkTokenPayload == null) {
            sdkTokenPayload = InternalSDKTokenPayload.parseSDKTokenPayload(tokenValue)
        }
        return sdkTokenPayload
    }

    companion object {
        private const val ENTERPRISE_FEATURES = "enterprise_features"
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy