Access Manager v3 API for Windows C SDK
Access Manager isn't enabled by default
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 (
readtochannel1andwritetochannel2).
You can add the author_uuid parameter to the grant request to restrict the token usage to only one client with a given UUID. Only this author_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 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)
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.
- Permissions
- TTL (time to live)
- RegEx patterns
- Authorized UUID
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:
| Resource | Permissions |
|---|---|
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.
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 expressions (RegEx) to specify permissions by pattern instead of listing each resource. Define RegEx permissions for a given resource type in the grant request.
For more details, see RegEx in Access Manager v3.
Setting an author_uuid in the token specifies which client should use this token in every request to PubNub. If you do not set author_uuid during the grant request, the token can be used by any client with any UUID. Restrict tokens to a single author_uuid to prevent impersonation.
For more details, see Authorized UUID in Access Manager v3.
Method(s)
1enum pubnub_res pubnub_grant_token(pubnub_t* pb, char const* perm_obj)
| Parameter | Description |
|---|---|
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 = {.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_author_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) {
9res = pubnub_await(gtp);
10if (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{"data":{"message":"Success","token":"p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI"},"service":"Access Manager","status":200}