On this page

Access Manager v3 API for Windows C++ SDK

Access Manager allows you to enforce security controls for client access to resources within the PubNub Platform. With Access Manager v3, your servers (that use a PubNub instance configured with a secret key) can grant their clients tokens with embedded permissions that provide access to individual PubNub resources:

  • For a limited period of time.
  • Through resource lists or patterns (regular expressions).
  • In a single API request, even if permission levels differ (read to channel1 and write to channel2).

You can add the authorized_uuid parameter to the grant request to restrict the token usage to only one client with a given UUID. Only this authorized_uuid can use the token to make API requests for the specified resources and permissions.

For more information about Access Manager v3, refer to Manage Permissions with Access Manager v3.

Grant token

Requires Access Manager add-on

This method requires that the Access Manager add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Requires Secret Key authentication

Granting permissions to resources should be done by administrators whose SDK instance has been initialized with a Secret Key (available on the Admin Portal on your app's keyset).

The grant_token() method generates a time-limited authorization token with an embedded access control list. The token defines time to live (ttl_minutes), authorized_uuid, and a set of permissions giving access to one or more resources:

  • channels
  • groups
  • uuids (other users' object metadata, such as their names or avatars)

Only this authorized_uuid will be able to use the token with the defined permissions. The authorized client will send the token to PubNub with each request until the token's ttl_minutes expires. Any unauthorized request or a request made with an invalid token will return a 403 with a respective error message.

The grant request allows your server to securely grant your clients access to the resources within the PubNub Platform. There is a limited set of operations the clients can perform on every resource:

ResourcePermissions
channels
read, write, get, manage, update, join, delete
groups
read, manage
uuids
get, update, delete

For permissions and API operations mapping, refer to Manage Permissions with Access Manager v3.

Method(s)

1enum pubnub_res pubnub_grant_token(pubnub_t* pb, char const* perm_obj)
* required
ParameterDescription
pb *
Type: pubnub_t*
Pointer to the PubNub context. Can't be NULL.
perm_obj *
Type: const char*
Pointer to string with the permissions.
Required key/value mappings

For a successful grant request, you must specify permissions for at least one uuid, channel, or group, either as a resource list or as a pattern (RegEx).

Sample code

1struct pam_permission ch_perm = { ch_perm.read=true };
2int perm_my_channel = pubnub_get_grant_bit_mask_value(ch_perm);
3int ttl_minutes = 15; // Max value for ttl_minutes is 43,200 minutes (30 days)
4char perm_obj[2000];
5char* author_uuid = "my_authorized_uuid";
6sprintf(perm_obj,"{\"ttl\":%d, \"uuid\":\"%s\", \"permissions\":{\"resources\":{\"channels\":{ \"my_channel\":%d }, \"groups\":{}, \"users\":{ }, \"spaces\":{}}, \"patterns\":{\"channels\":{}, \"groups\":{}, \"users\":{}, \"spaces\":{}},\"meta\":{}}}", ttl_minutes, author_uuid, perm_my_channel);
7pubnub::futres futgres = gb.grant_token(perm_obj);
8res = futgres.await();
9std::string tkn = "";
10if (PNR_OK == res) {
11 tkn = gb.get_grant_token();
12 std::cout << "Grant Token done! token = " << tkn << std::endl;
13}

Returns

1p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI

Other examples

Grant an authorized client different levels of access to various resources in a single call

The code below grants my-authorized-uuid:

  • Read access to channel-a, channel-group-b, and get to uuid-c.
  • Read/write access to channel-b, channel-c, channel-d, and get/update to uuid-d.
1struct pam_permission cha_perm = {cha_perm.read=true };
2struct pam_permission cgb_perm = {cgb_perm.read=true };
3struct pam_permission uidc_perm = {uidc_perm.get=true };
4
5struct pam_permission chb_perm = {chb_perm.read=true, chb_perm.write=true };
6struct pam_permission chc_perm = {chc_perm.read=true, chc_perm.write=true };
7struct pam_permission chd_perm = {chd_perm.read=true, chd_perm.write=true };
8struct pam_permission uidd_perm = {uidd_perm.get=true, uidd_perm.update=true };
9
10int perm_cha = pubnub_get_grant_bit_mask_value(cha_perm);
11int perm_chb = pubnub_get_grant_bit_mask_value(chb_perm);
12int perm_chc = pubnub_get_grant_bit_mask_value(chc_perm);
13int perm_chd = pubnub_get_grant_bit_mask_value(chd_perm);
14
15int perm_cgb = pubnub_get_grant_bit_mask_value(cgb_perm);
show all 30 lines

Grant an authorized client read access to multiple channels using RegEx

The code below grants my-authorized-uuid read access to all channels that match the channel-[A-Za-z0-9] RegEx pattern.

1char* author_uuid = "my-authorized-uuid";
2struct pam_permission pat_ch_perm = {pat_ch_perm.read=true };
3
4int perm_ch_pat = pubnub_get_grant_bit_mask_value(pat_ch_perm);
5
6int ttl_minutes = 15;
7char perm_obj[2000];
8sprintf(perm_obj,"{\"ttl\":%d, \"uuid\":\"%s\", \"permissions\":{\"resources\":{\"channels\":{ }, \"groups\":{ }, \"uuids\":{ }}, \"patterns\":{\"channels\":{ \"channel-[A-Za-z0-9]\":%d }, \"groups\":{ }, \"uuids\":{ }},\"meta\":{ }}}", ttl_minutes, author_uuid, perm_ch_pat);
9pubnub::futres futgres = gb.grant_token(perm_obj);
10res = futgres.await();
11std::string tkn = "";
12if (PNR_OK == res) {
13 tkn = gb.get_grant_token();
14 std::cout << "Grant Token done! token = " << tkn << std::endl;
15}

Grant an authorized client different levels of access to various resources and read access to channels using RegEx in a single call

The code below grants the my-authorized-uuid:

  • Read access to channel-a, channel-group-b, and get to uuid-c.
  • Read/write access to channel-b, channel-c, channel-d, and get/update to uuid-d.
  • Read access to all channels that match the channel-[A-Za-z0-9] RegEx pattern.
1char* author_uuid = "my-authorized-uuid";
2struct pam_permission cha_perm = {cha_perm.read=true };
3struct pam_permission cgb_perm = {cgb_perm.read=true };
4struct pam_permission uidc_perm = {uidc_perm.get=true };
5
6struct pam_permission chb_perm = {chb_perm.read=true, chb_perm.write=true };
7struct pam_permission chc_perm = {chc_perm.read=true, chc_perm.write=true };
8struct pam_permission chd_perm = {chd_perm.read=true, chd_perm.write=true };
9struct pam_permission uidd_perm = {uidd_perm.get=true, uidd_perm.update=true };
10
11struct pam_permission pat_ch_perm = {pat_ch_perm.read=true };
12
13int perm_cha = pubnub_get_grant_bit_mask_value(cha_perm);
14int perm_chb = pubnub_get_grant_bit_mask_value(chb_perm);
15int perm_chc = pubnub_get_grant_bit_mask_value(chc_perm);
show all 34 lines

Error responses

If you submit an invalid request, the server returns HTTP 400 with a message that identifies the missing or incorrect argument. Causes can include a RegEx issue, an invalid timestamp, or incorrect permissions.

Parse token

The parse_token() method decodes an existing token and returns the object containing permissions embedded in that token. The client can use this method for debugging to check the permissions to the resources or find out the token's ttl_minutes details.

Method(s)

1std::string parse_token(std::string const& token)
* required
ParameterDescription
token *
Type: std::string const&
Current token with embedded permissions.

Sample code

1std::string cbor_data = gb.parse_token("p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI");

Returns

1{
2 "version":2,
3 "timestamp":1619718521,
4 "ttl":15,
5 "resources":{
6 "user-id":{
7 "create":true,
8 "read":true,
9 "write":true,
10 "manage":true,
11 "delete":true
12 },
13 "space-id":{
14 "create":true,
15 "read":true,
show all 63 lines

Error Responses

If you receive an error while parsing the token, it may suggest that the token is damaged. In that case, request the server to issue a new one.

Set token

The set_auth_token() method is used by the client devices to update the authentication token granted by the server.

Method(s)

1void set_auth_token(std::string const& token)
* required
ParameterDescription
token *
Type: std::string const&
Current token with embedded permissions.

Sample code

1pb.set_auth_token("p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI")

Returns

This method doesn't return any response value.

Last updated on