pubnub

Unable to use the PubNub access manager for client authorization


I was trying to use the access manager for client authorization. Unfortunately, I'm unable to use it.

I have already tried this procedure:

Note: Used C# SDK for Server side & js SDK for Client-side

  1. I turned on the access manager plugin from my admin panel.
  2. On the server side I created the client using the below code
    PNConfiguration pnConfiguration = new PNConfiguration(new UserId("AdminUserId"))
    {
        SubscribeKey = _pubNubSubscribeKey,
        PublishKey = _pubNubPublishKey,
        SecretKey = _pubNubSecretKey
    };

    _pubNubClient = new Pubnub(pnConfiguration);
  1. Using this client I tried to create an access token.
    PNResult<PNAccessManagerTokenResult> grantTokenResponse = await _pubNubClient.GrantToken()
        //.AuthorizedUserId("my-authorized-userId")
        .AuthorizedUuid("my-authorized-userId")
        .TTL(60)
        .Resources(new PNTokenResources()
        {
        Spaces = new Dictionary<string, PNTokenAuthValues>() {
                    { "messenger", new PNTokenAuthValues() { Read = true, Write = true } }
        })
        .ExecuteAsync();
  1. Using "AuthorizedUserId" I'm not getting authorized-userId inside the generated token. but using obsolete "AuthorizedUuid" I'm getting this. I checked it by parsing the generated access token.

  2. After that I passed it to the client side & tried to set this token. procedure was:

    let pubNubOptions = {
        publishKey: _pubNubPublishKey,
        subscribeKey: _pubNubSubscribeKey,
        userId: "my-authorized-userId"
        //uuid: "my-authorized-userId",
        //authKey: "returned token"
    };
    
    let pubnub = new PubNub(pubNubOptions);
    pubnub.setToken("returned token");
  1. After this I subscribed my client to the "messenger" channel.
    pubnub.subscribe({
        channels: ['messenger'],
        //spaces: ['messenger'],
    });

Note: It is returning the "403 status code" access is forbidden.

  1. I was unable to publish or receive any messages with access forbidden 403 status code.

So, how can I use Access manager for client authorization?


Solution

  • Grant to the Channel

    Spaces are an up-and-coming feature of PubNub and will become the recommended way to use PubNub (over channels and channel groups). But channels and channel groups aren't going anywhere.

    Please refer to the docs for granting permissions on the channel.

    PNResult<PNAccessManagerTokenResult> grantTokenResponse = await pubnub.GrantToken()
        .TTL(15)
        .AuthorizedUuid("my-authorized-uuid")
        .Resources(new PNTokenResources() 
        {
            Channels = new Dictionary<string, PNTokenAuthValues>() {
                { "my-channel", new PNTokenAuthValues() { Read = true } } }
        }) 
        .ExecuteAsync();