Access Manager v3 API for C-Core SDK (legacy)
New SDK available
C-Core (legacy) is still supported. If you are starting a new project, use the new C SDK.
Access Manager isn't enabled by default
Access Manager lets you enforce security controls for client access to resources on the PubNub platform. With Access Manager v3, your servers (that use a PubNub instance configured with a secret key) can grant clients tokens with embedded permissions that provide access to individual PubNub resources:
- For a limited time period.
- Through resource lists or regular expression (RegEx) patterns.
- In one API request, even when permissions differ (for example,
readtochannel1andwritetochannel2).
You can add the authorized_uuid parameter to restrict token usage to 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. See Enable the features your app calls.
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 pubnub_grant_token() method generates a time-limited authorization token with an embedded access control list. The token defines time to live (ttl_minutes), author_uuid, and a set of permissions giving access to one or more resources:
channelsgroupsuuids(other users' object metadata, such as their names or avatars)categories(list all channel oruuidmetadata on the subscribe key, not one named resource)
Only this author_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 clients access to resources on the PubNub platform. Each resource type supports a specific set of operations:
| Resource | Permissions |
|---|---|
channels | read, write, get, manage, update, join, delete |
groups | read, manage |
uuids | get, update, delete |
categories (channels, uuids) | get only |
In perm_obj, add a categories object to permissions. Set its channels key to 32 to list all channel metadata, and its uuids key to 32 to list all uuid metadata. 32 is the get bit. You can leave out resources and patterns when you grant only categories.
- A category permission lets a token list all metadata of one resource type on your subscribe key. Valid categories are
channelsanduuids. The only permission isget(32). - A
getpermission on one named channel or user, or on a pattern, does not grant a category. - A token can contain only categories. It needs no resource or pattern.
- When enumeration enforcement is enabled for a keyset, App Context list requests from a token or auth key without the matching category permission return a
403error.
For permissions and API operations mapping, refer to Manage Permissions with Access Manager v3.
#
The ttl_minutes (time to live) parameter defines how many minutes the permissions remain valid. After expiration, the client must get a new token to maintain access. ttl_minutes is required for every grant call. There is no default value. The maximum value is 43,200 (30 days).
For more details, see TTL in Access Manager v3.
#
Use regular expression (RegEx) patterns to set permissions without listing each resource. Define RegEx permissions for a given resource type in the grant request. Patterns are evaluated on the server using RE2-style syntax; backreferences and lookaround assertions are not supported.
For more details, see RegEx in Access Manager v3.
Method(s)
1enum pubnub_res pubnub_grant_token(
2 pubnub_t* pb,
3 char const* perm_obj
4)
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), or set categories.
Sample code
1struct pam_permission 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);
7res = pubnub_grant_token(gtp, perm_obj);
8if (PNR_STARTED == res) {
9 res = pubnub_await(gtp);
10 if (PNR_OK == res) {
11 pubnub_chamebl_t grant_token_resp = pubnub_get_grant_token(gtp);
12 printf("pubnub_grant_token() Response from Pubnub: %s\n", grant_token_resp.ptr);
13 }
14}
Returns
1{
2 "data": {
3 "message": "Success",
4 "token": "p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI"
5 },
6 "service": "Access Manager",
7 "status": 200
8}
Other examples
Grant an authorized client access to list all channel and uuid metadata
The code below grants a uuid the get category permission for both channels and uuids, so the client can list all channel and uuid metadata on the subscribe key.
1// Grants a token that can list all channel metadata and all uuid metadata.
2// The categories permissions need no resources, `32` is the only valid value (`get`).
3static enum pubnub_res grant_token_categories(pubnub_t* pb)
4{
5 char const* perm_obj =
6 "{\"ttl\":60, \"uuid\":\"my_authorized_uuid\", \"permissions\":{"
7 "\"categories\":{\"channels\":32, \"uuids\":32}}}";
8 enum pubnub_res res = pubnub_grant_token(pb, perm_obj);
9
10 if (PNR_STARTED == res) {
11 res = pubnub_await(pb);
12 }
13 if (PNR_OK == res) {
14 pubnub_chamebl_t grant_token = pubnub_get_grant_token(pb);
15 printf("pubnub_grant_token() Response from Pubnub: %s\n", grant_token.ptr);
show all 24 linesGrant 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 touuid-c. - Read/write access to
channel-b,channel-c,channel-d, and get/update touuid-d.
1char* author_uuid = "my-authorized-uuid";
2struct pam_permission cha_perm = {.read=true };
3struct pam_permission cgb_perm = {.read=true };
4struct pam_permission uidc_perm = {.get=true };
5
6struct pam_permission chb_perm = {.read=true, .write=true };
7struct pam_permission chc_perm = {.read=true, .write=true };
8struct pam_permission chd_perm = {.read=true, .write=true };
9struct pam_permission uidd_perm = {.get=true, .update=true };
10
11int perm_cha = pubnub_get_grant_bit_mask_value(cha_perm);
12int perm_chb = pubnub_get_grant_bit_mask_value(chb_perm);
13int perm_chc = pubnub_get_grant_bit_mask_value(chc_perm);
14int perm_chd = pubnub_get_grant_bit_mask_value(chd_perm);
15
show all 31 linesGrant 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 = {.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);
9res = pubnub_grant_token(gtp, perm_obj);
10if (PNR_STARTED == res) {
11res = pubnub_await(gtp);
12if (PNR_OK == res) {
13 pubnub_chamebl_t grant_token_resp = pubnub_get_grant_token(gtp);
14 printf("pubnub_grant_token() Response from Pubnub: %s\n", grant_token_resp.ptr);
15}
show all 16 linesGrant 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 touuid-c. - Read/write access to
channel-b,channel-c,channel-d, and get/update touuid-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 = {.read=true };
3struct pam_permission cgb_perm = {.read=true };
4struct pam_permission uidc_perm = {.get=true };
5
6struct pam_permission chb_perm = {.read=true, .write=true };
7struct pam_permission chc_perm = {.read=true, .write=true };
8struct pam_permission chd_perm = {.read=true, .write=true };
9struct pam_permission uidd_perm = {.get=true, .update=true };
10
11struct pam_permission 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 35 linesError 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.
Revoke token
Requires Access Manager add-on
This method requires that the Access Manager add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
Enable token revoke
To revoke tokens, you must first enable this feature on the Admin Portal. To do that, navigate to your app's keyset and mark the Revoke v3 Token checkbox in the ACCESS MANAGER section.
The pubnub_revoke_token() method allows you to disable an existing token and revoke all permissions embedded within. You can only revoke a valid token previously obtained using the pubnub_grant_token method.
Use this method for tokens with ttl less than or equal to 30 days. If you need to revoke a token with a longer ttl, contact support.
For more information, refer to Revoke permissions.
Method(s)
1pubnub_revoke_token(char const* token)
tokenrequired#- Existing token with embedded permissions.
Sample code
1pbresult = pubnub_grant_token(ctx, "p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenV");
2if (PNR_STARTED == res) {
3 pbresult = pubnub_await(ctx);
4 if (PNR_OK == res) {
5 printf("Access token permissions revoked! Response from Pubnub: %s\n",
6 pubnub_get_revoke_token_response(ctx).ptr);
7 }
8}
Rest response from server
1{
2 "data": {
3 "message": "Success"
4 },
5 "service": "Access Manager",
6 "status": 200
7}
Error Responses
If you submit an invalid request, the server returns an error status code with a descriptive message informing which of the provided arguments is missing or incorrect. Depending on the root cause, this operation may return the following errors:
400 Bad Request403 Forbidden503 Service Unavailable
Parse token
The pubnub_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)
1char* pubnub_parse_token(
2 pubnub_t* pb,
3 char const* token
4)
Sample code
1char* cbor_data = pubnub_parse_token(<pubnub_context>, "p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI");
Returns
The server returns the permissions in CBOR format. You can Base 64 decode it, and parse with any CBOR parser.
A token with category permissions has a cat object in the output. Its chan key is 32 when the token can list all channel metadata, and its uuid key is 32 when it can list all uuid metadata. Only the keys the token carries appear.
1{
2 "v":2,
3 "t":1619718521,
4 "ttl":15,
5 "res":{
6 "usr":{},
7 "spc":{},
8 "chan":{{"ch1":19}},
9 "grp":{}
10 },
11 "pat":{
12 "usr":{},
13 "spc":{},
14 "chan":{},
15 "grp":{}
show all 19 linesThe code below parses a token and reads its category permissions.
1// Prints the category permissions of a token, `cat` section of the parsed token.
2// Each value is the permission bit mask, 32 is the `get` permission.
3static void print_token_categories(pubnub_t* pb, char const* token)
4{
5 char* parsed = pubnub_parse_token(pb, token);
6 if (NULL == parsed) {
7 printf("pubnub_parse_token() failed\n");
8 return;
9 }
10
11 struct pbjson_elem parsed_elem = { parsed, parsed + strlen(parsed) };
12 struct pbjson_elem categories;
13 struct pbjson_elem channels;
14 struct pbjson_elem uuids;
15
show all 31 linesError 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 pubnub_set_auth_token() method is used by the client devices to update the authentication token granted by the server.
Method(s)
1void pubnub_set_auth_token(
2 pubnub_t* pb,
3 char const* token
4)
Sample code
1pubnub.pubnub_set_auth_token(<pubnub_context>, "p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI")
Returns
This method doesn't return any response value.