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

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

package com.pubnub.internal.endpoints.access

import com.pubnub.api.PubNubError
import com.pubnub.api.PubNubException
import com.pubnub.api.enums.PNOperationType
import com.pubnub.api.models.consumer.access_manager.v3.PNGrantTokenResult
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.v3.ChannelGrant
import com.pubnub.internal.models.consumer.access_manager.v3.ChannelGroupGrant
import com.pubnub.internal.models.consumer.access_manager.v3.UUIDGrant
import com.pubnub.internal.models.server.access_manager.v3.GrantTokenRequestBody
import com.pubnub.internal.models.server.access_manager.v3.GrantTokenResponse
import retrofit2.Call
import retrofit2.Response

class GrantTokenEndpoint(
    pubnub: PubNubCore,
    override val ttl: Int,
    private val meta: Any?,
    private val authorizedUUID: String?,
    private val channels: List,
    private val channelGroups: List,
    private val uuids: List,
) : EndpointCore(pubnub), GrantTokenInterface {
    override fun getAffectedChannels(): List = channels.map { it.id }

    override fun getAffectedChannelGroups(): List = channelGroups.map { it.id }

    override fun validateParams() {
        if (!pubnub.configuration.secretKey.isValid()) {
            throw PubNubException(PubNubError.SECRET_KEY_MISSING)
        }
        if (!configuration.subscribeKey.isValid()) {
            throw PubNubException(PubNubError.SUBSCRIBE_KEY_MISSING)
        }
        if ((channels + channelGroups + uuids).isEmpty()) {
            throw PubNubException(
                pubnubError = PubNubError.RESOURCES_MISSING,
                errorMessage = "At least one grant required",
            )
        }
    }

    override fun doWork(queryParams: HashMap): Call {
        val requestBody: GrantTokenRequestBody =
            GrantTokenRequestBody.of(
                ttl = ttl,
                channels = channels,
                groups = channelGroups,
                uuids = uuids,
                meta = meta,
                uuid = authorizedUUID,
            )
        return retrofitManager
            .accessManagerService
            .grantToken(configuration.subscribeKey, requestBody, queryParams)
    }

    override fun createResponse(input: Response): PNGrantTokenResult {
        return input.body()!!.data.token.let { PNGrantTokenResult(it) }
    }

    override fun operationType(): PNOperationType = PNOperationType.PNAccessManagerGrantToken

    override fun isAuthRequired(): Boolean = false

    override fun getEndpointGroupName(): RetryableEndpointGroup = RetryableEndpointGroup.ACCESS_MANAGER
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy