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

com.pubnub.internal.endpoints.access.GrantEndpoint.kt Maven / Gradle / Ivy

package com.pubnub.internal.endpoints.access

import com.google.gson.JsonElement
import com.pubnub.api.PubNubError
import com.pubnub.api.PubNubException
import com.pubnub.api.enums.PNOperationType
import com.pubnub.api.retry.RetryableEndpointGroup
import com.pubnub.api.v2.BasePNConfiguration.Companion.isValid
import com.pubnub.internal.EndpointCore
import com.pubnub.internal.PubNubCore
import com.pubnub.internal.models.consumer.access_manager.PNAccessManagerGrantResult
import com.pubnub.internal.models.consumer.access_manager.PNAccessManagerKeyData
import com.pubnub.internal.models.server.Envelope
import com.pubnub.internal.models.server.access_manager.AccessManagerGrantPayload
import com.pubnub.internal.toCsv
import retrofit2.Call
import retrofit2.Response

/**
 * @see [PubNubCore.grant]
 */
open class GrantEndpoint(
    pubnub: PubNubCore,
    override val read: Boolean = false,
    override val write: Boolean = false,
    override val manage: Boolean = false,
    override val delete: Boolean = false,
    override val get: Boolean = false,
    override val update: Boolean = false,
    override val join: Boolean = false,
    override val ttl: Int = -1,
    override val authKeys: List = emptyList(),
    override val channels: List = emptyList(),
    override val channelGroups: List = emptyList(),
    override val uuids: List = emptyList(),
) : EndpointCore, PNAccessManagerGrantResult>(pubnub), GrantInterface {
    override fun validateParams() {
        super.validateParams()
        if (!pubnub.configuration.secretKey.isValid()) {
            throw PubNubException(PubNubError.SECRET_KEY_MISSING)
        }
    }

    override fun getAffectedChannels() = channels

    override fun getAffectedChannelGroups() = channelGroups

    override fun doWork(queryParams: HashMap): Call> {
        addQueryParams(queryParams)

        return retrofitManager.accessManagerService
            .grant(
                subKey = configuration.subscribeKey,
                options = queryParams,
            )
    }

    override fun createResponse(input: Response>): PNAccessManagerGrantResult {
        val data = input.body()!!.payload!!

        val constructedChannels = mutableMapOf?>()
        val constructedGroups = mutableMapOf?>()

        // we have a case of a singular channel.
        data.channel?.let {
            constructedChannels[it] = data.authKeys!!
        }

        if (channelGroups.size == 1) {
            constructedGroups[pubnub.mapper.elementToString(data.channelGroups)!!] = data.authKeys!!
        } else if (channelGroups.size > 1) {
            val it = pubnub.mapper.getObjectIterator(data.channelGroups!!)
            while (it.hasNext()) {
                val (k, v) = it.next()
                constructedGroups[k] = createKeyMap(v)
            }
        }

        data.channels?.forEach {
            constructedChannels[it.key] = data.channels[it.key]!!.authKeys
        }

        val constructedUuids = mutableMapOf?>()
        data.uuids?.forEach {
            constructedUuids[it.key] = data.uuids[it.key]!!.authKeys
        }

        return PNAccessManagerGrantResult(
            level = data.level!!,
            ttl = data.ttl,
            subscribeKey = data.subscribeKey!!,
            channels = constructedChannels,
            channelGroups = constructedGroups,
            uuids = constructedUuids,
        )
    }

    override fun operationType() = PNOperationType.PNAccessManagerGrant

    override fun isAuthRequired() = false

    override fun getEndpointGroupName(): RetryableEndpointGroup = RetryableEndpointGroup.ACCESS_MANAGER

    private fun createKeyMap(input: JsonElement): Map {
        val result: MutableMap =
            HashMap()
        val it: Iterator> =
            pubnub.mapper.getObjectIterator(input, "auths")
        while (it.hasNext()) {
            val keyMap = it.next()
            val pnAccessManagerKeyData = PNAccessManagerKeyData()
            pnAccessManagerKeyData.manageEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "m"))
            pnAccessManagerKeyData.writeEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "w"))
            pnAccessManagerKeyData.readEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "r"))
            pnAccessManagerKeyData.deleteEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "d"))
            pnAccessManagerKeyData.getEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "g"))
            pnAccessManagerKeyData.updateEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "u"))
            pnAccessManagerKeyData.joinEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "j"))
            result[keyMap.key] = pnAccessManagerKeyData
        }
        return result
    }

    private fun addQueryParams(queryParams: MutableMap) {
        channels.run {
            if (isNotEmpty()) {
                queryParams["channel"] = toCsv()
            }
        }
        channelGroups.run {
            if (isNotEmpty()) {
                queryParams["channel-group"] = toCsv()
            }
        }
        authKeys.run {
            if (isNotEmpty()) {
                queryParams["auth"] = toCsv()
            }
        }
        uuids.run {
            if (isNotEmpty()) {
                queryParams["target-uuid"] = toCsv()
            }
        }

        if (ttl >= -1) {
            queryParams["ttl"] = ttl.toString()
        }

        queryParams["r"] =
            if (read) {
                "1"
            } else {
                "0"
            }
        queryParams["w"] =
            if (write) {
                "1"
            } else {
                "0"
            }
        queryParams["m"] =
            if (manage) {
                "1"
            } else {
                "0"
            }
        queryParams["d"] =
            if (delete) {
                "1"
            } else {
                "0"
            }
        queryParams["g"] =
            if (get) {
                "1"
            } else {
                "0"
            }
        queryParams["u"] =
            if (update) {
                "1"
            } else {
                "0"
            }
        queryParams["j"] =
            if (join) {
                "1"
            } else {
                "0"
            }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy